How to make maps in a simple 2D game?

Hello,
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.

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 http://en.wikipedia.org/wiki/RGB_color_model)

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

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

You might find this article interesting: http://www.tonypa.pri.ee/tbw/start.html

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

Non-java code:

http://www.rel.phatcode.net/MyProgs/PyromaxDax.zip

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

Thanks, I have found all of the above replies really helpful. :slight_smile:

That’s so cool, just out of curiosity how long have u spent on this game?

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.