Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (789)
Games in Android Showcase (234)
games submitted by our members
Games in WIP (864)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  How to make maps in a simple 2D game?  (Read 4367 times)
0 Members and 1 Guest are viewing this topic.
Offline mashikag

Senior Newbie

« Posted 2013-02-26 01:00:37 »

I haven't got much experience in game programming and especially in 2D platformers. I am wondering how do you make such maps because I can see them as one long spritesheet but this does not seem to me quite right as I cant see people making such long maps in one spritesheet. Another way I could imagine is split it into smaller images which joined together one after another would make up whole map or another way is generating the enviroment on the run but this would be quite difficult.
Offline nhmllr

Senior Devvie

Medals: 1
Projects: 3

slow and steady...

« Reply #1 - Posted 2013-02-26 04:00:44 »

What a lot of people do for simple tile-based games is they store levels as images where each pixel corresponds to one tile.

Colors are stores in images as integers, with a component for the red, green, and blue values of the colors (more here

You can make these images in something simple like MS Paint or GIMP or Paint.NET. For example, a floor tile could be represented as a white pixel, while a wall tile could be a black pixel. A health pickup could be red, an enemy could be green, and so on. You can load this image into your game and have the computer make an array of ints, where you then say, "Okay, if this pixel here is white, put a floor tile in the game world at these coordinates. If it's black, make a wall" and so on.

What's nice about this is that you can pretty easily edit levels on the fly by just opening up the paint program.

Here's a method I made for one of my games to get the int[][] of colors out of the original image:

//this is what's supposed to load the RGB values from the .png into my map[][] array
   public int[][] loadRGBMap(String directory){  
      int[][] map;
      BufferedImage img = ResourceLoader.loadImage(directory);

      int w = img.getWidth();
      int h = img.getHeight();
      int[] pixels = new int[w * h];
      img.getRGB(0, 0, w, h, pixels, 0, w);
      map = new int[w][h];
      levelMap = new boolean[w][h];
      //this channels the 1 dimensional pixel[] into the two dimensional map[][]
      for(int row = 0; row < h; row++){
         for(int col = 0; col < w; col++){
            map[col][row] = pixels[row*w+col];
      return map;

"directory" is just where the map is stored, ie "levels/level1.png"
"ResourceLoader" happens to be my own class, but all it does it load a BufferedImage

Hopefully that should get you started
Offline Agro
« Reply #2 - Posted 2013-02-26 04:07:15 »

Couple of different ways. Like ^ says, you can use tile maps(text files that use characters to represent objects).

For example,
#        m #
#    p     #
#          #

p represents a player, and m represents a monster, and # represents walls.

Another alternative is color maps. This gives you MANY MANY different possible objects(char tilemaps only give you around 256 i think). How many colors there are are the number of objects you can use.

The last alternative is a binary file. You can do something like this:

[object id] [xcord] [ycord]

There are many other ways to do it, but these are jsut a few

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline jonjava
« Reply #3 - Posted 2013-02-26 04:28:10 »

You might find this article interesting:

Offline relminator
« Reply #4 - Posted 2013-02-26 15:43:41 »

I just used text files edited in Notepad++ due to lack of time in my latest platformer.

Non-java code:

For large and bigger projects, I usually use the game engine to make a dedicated map editor.
Offline mashikag

Senior Newbie

« Reply #5 - Posted 2013-02-27 01:24:06 »

Thanks, I have found all of the above replies really helpful.  Smiley
Offline mashikag

Senior Newbie

« Reply #6 - Posted 2013-02-27 01:24:55 »

I just used text files edited in Notepad++ due to lack of time in my latest platformer.

Non-java code:

For large and bigger projects, I usually use the game engine to make a dedicated map editor.

That's so cool, just out of curiosity how long have u spent on this game?
Offline relminator
« Reply #7 - Posted 2013-02-27 01:36:39 »

Two months worth of my free time. ;*)

Basically I don't code daily but if I have to average, it's probably an hour a day doing it everyday for two months.
Pages: [1]
  ignore  |  Print  

hadezbladez (2606 views)
2018-11-16 13:46:03

hadezbladez (932 views)
2018-11-16 13:41:33

hadezbladez (2560 views)
2018-11-16 13:35:35

hadezbladez (501 views)
2018-11-16 13:32:03

EgonOlsen (3679 views)
2018-06-10 19:43:48

EgonOlsen (4032 views)
2018-06-10 19:43:44

EgonOlsen (2442 views)
2018-06-10 19:43:20

DesertCoockie (3248 views)
2018-05-13 18:23:11

nelsongames (3294 views)
2018-04-24 18:15:36

nelsongames (4265 views)
2018-04-24 18:14:32
Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!