Making a map for a 2D game.

Started by
11 comments, last by farmdve 10 years, 8 months ago


I read the map from a file being just numbers that represented a different sprite but one couldn't describe an object on those tiles.

One way to do this: instead of using the numbers to refer to images, use it to refer to an object description. Don't think of tiles as something that is not an object. Just think of tiles as objects whose positions are snapped to a grid. That means the ground brick is an object, the question brick is an object, and so on.

For example, I can have a map description file like this (ini-like format):


[level 1]
tiles =
A
A
A  B
AAAA
 
A = "brick"
B = "player"

In this example, the value of "tiles" describes the placement of objects in the map. The symbols 'A' and 'B' are tiles, and they point to an object description, not an image file. When loading the map, just read in the symbols of "tiles" and see what object each of those symbols refer to, and use the refered object description to load in the object, and set the object's position according to some loop state variables you keep.

Now how do I parse this? I can read the file and translate those letters to tiles, however this does not help me translate the position of those letters to position of tiles in the world.

For instance, this is the map I have


XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXZXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
AXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB

And while I can instruct my code to treat the letters as tiles, at the moment I do not know how to position the tiles as per the map.

Advertisement


I read the map from a file being just numbers that represented a different sprite but one couldn't describe an object on those tiles.

One way to do this: instead of using the numbers to refer to images, use it to refer to an object description. Don't think of tiles as something that is not an object. Just think of tiles as objects whose positions are snapped to a grid. That means the ground brick is an object, the question brick is an object, and so on.

For example, I can have a map description file like this (ini-like format):


[level 1]
tiles =
A
A
A  B
AAAA
 
A = "brick"
B = "player"

In this example, the value of "tiles" describes the placement of objects in the map. The symbols 'A' and 'B' are tiles, and they point to an object description, not an image file. When loading the map, just read in the symbols of "tiles" and see what object each of those symbols refer to, and use the refered object description to load in the object, and set the object's position according to some loop state variables you keep.

Now how do I parse this? I can read the file and translate those letters to tiles, however this does not help me translate the position of those letters to position of tiles in the world.

For instance, this is the map I have


XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXZXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
AXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB

And while I can instruct my code to treat the letters as tiles, at the moment I do not know how to position the tiles as per the map.

Well, just use the position of the symbol, and multiply that by the tile size.

For example:

Let's say A is your player. The symbol A in this map description is in position zeroth column, fifth row(0, 5).

Let's say each tile in this map description is of width 16, height 16.

And let's say the coordinate system is a cartesion one, reflected over the y axis (top is negative y, bottom is positive y).

Then the coordinate the top left corner of the player is simply (16 * 0, 16 * 5).

As for how to find out the position of a symbol inside the map description, you will have to make use of a loop. Iterate through every symbol, keeping the current row and column of the symbol as you do so.

EDIT 2: I retracted my previous statement. I finally parsed the map properly.

This topic is closed to new replies.

Advertisement