Need some help with 2D tile map

Started by
1 comment, last by Crazyfool 15 years, 2 months ago
I'm not much of a programmer, just a hobbyist. I've recently got my program to load images, draw them to the screen. But now I want to make a 2D tile map, and I'm having trouble. I don't know what to do next. -Load Data from a map file? (How should this data look?) -What do I store the data as? multi-dimensional array? -How do I code my program so that it knows what/where to draw? Yes this is pretty vague help, but hopefully someone can give me few ideas/hints on what to do ;-) I've looked for tutorials, but most of them are for isometric not the traditional square.
Advertisement
Not sure what language you are using, but I could recommend this series to you:

Tile Engine Tutorials

It is a video series that captures most of the essentials of creating a tile engine/tile game and a little bit of WinForm stuff. It is for XNA and C#, although, fundamentally it can be used to learn tile/2d-side scroller types of programming.

Hope that helps!
Alright, I'll try to sum this up as easily as possible.

1. You'll want to have a designated data format for your map. This could be based on XML or your own system. My game uses XML; google XML map formats to get a taste of it. You can even use some free tile map editors such as this one: here or I can send you a link with instructions to the format for my own (both use .NET).

2. I created a class called Tile which keeps information about the texture index (of the tilesheet, top left tile is 0 and as you move right increase by 1), the tilesheet of the tile (I have support for multiple tilesheets per map), pathing value (can it be walked on?), and any other information you might need about the tile. then I have a World class that has a 2d array of tiles. when you load a map, you should specify a start position for the x/y.

For instance, loadMap(int x, int y, std::string filename) could be the function, and the x and y variables could be the position in the 2d array of which the loader starts placing the map's data.

3. This all depends if you're drawing things in world space or screen space. World space you can probably just draw it exactly at the index its at.. i.e. the x/y index of the array. Since this is 2d, you'll probably be dealing with screen space. Here's where my solution is probably not the best, but it works.

I basically found the center of screen (visually pleasing center, not truly the 'center') which is where I'll always draw my character. I'd draw the tile at the position where the character is at right there as well (if player is at 4,5 then the tile at 4,5 will be drawn in same exact spot). Then for all other tiles within sight range, I would find the difference of the tile's position and the player's position and then multiply it by the size of the tile and then draw it there. To find whats on screen space, you just need to figure out how many tiles are seen at any one time horizontally and vertically. Then you'd use two for loops for drawing (for(int x = player.x - HOR_TILES_MAX / 2; x < player.x + HOR_TILES_MAX/ 2; x++) etc.).

I hope that makes sense.

This topic is closed to new replies.

Advertisement