Public Group

# Tile-Based Map

This topic is 2854 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

i'm making the snake game,but i dont know how to make a tile based map in c++ and allegro.Any help?

##### Share on other sites
The way I've been using for a simple game is as follows:

 Create your array. int Map[5][5] = { {1, 1, 1, 1, 1}, {1, 0, 0, 0, 0}, {1, 0, 0, 0, 0}, {1, 0, 0, 0, 0}, {1, 1, 1, 1, 1} }; BITMAP *Graphics[2]; (Load the code for images) Under your draw function you will need two for loops! for (int x = 0; x < 5; x++) { for (int y = 0; y < 5; y++) { al_draw_bitmap(Graphics[Map[y][x]], x * Tile_Width, y * Tile_Height, 0); } } 

So pretty much what happens here, is that the Array is checked with X and Y. If the value = 1, or 2 that Graphics[VALUE] will draw. It will then space out each tile to the width and height values set. If you fail to multiply, it will draw 1 pixel from the last one, ect...

##### Share on other sites

The way I've been using for a simple game is as follows:

 Create your array. int Map[5][5] = { {1, 1, 1, 1, 1}, {1, 0, 0, 0, 0}, {1, 0, 0, 0, 0}, {1, 0, 0, 0, 0}, {1, 1, 1, 1, 1} }; BITMAP *Graphics[2]; (Load the code for images) Under your draw function you will need two for loops! for (int x = 0; x < 5; x++) { for (int y = 0; y < 5; y++) { al_draw_bitmap(Graphics[Map[y][x]], x * Tile_Width, y * Tile_Height, 0); } } 

So pretty much what happens here, is that the Array is checked with X and Y. If the value = 1, or 2 that Graphics[VALUE] will draw. It will then space out each tile to the width and height values set. If you fail to multiply, it will draw 1 pixel from the last one, ect...

what allegro are you using,because i'm using 4.2.3.
and when i put it in it doesnt work.

Here is my code:

#include <allegro.h> int Map[5][5] = { {1, 1, 1, 1, 1}, {1, 0, 0, 0, 0}, {1, 0, 0, 0, 0}, {1, 0, 0, 0, 0}, {1, 1, 1, 1, 1} }; BITMAP *Graphics[2]; for (int x = 0; x < 5; x++) { for (int y = 0; y < 5; y++) { al_draw_bitmap(Graphics[Map[y][x]], x * Tile_Width, y * Tile_Height, 0); } } int main() { allegro_init(); install_keyboard(); install_mouse(); set_color_depth(16); set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0); readkey(); return 0; } END_OF_MAIN() 

It says
expected unqualified id
expected constructor,destructor or conversion before '<' token
expected constructor,destructor or conversion before '++' token

##### Share on other sites
My example was for Allegro 5. If you apply the same principles, it will work.

In Allegro 4 the draw function is:

draw_sprite()

If the code you pasted is your actual program, you're using allegro wrong. You cannot draw to the screen before allegro is initialized. You also did not load any graphics. I would suggest learning how to use the library properly before attempting any projects to limit further confusion.

Your main should look something like this:

 int main() { Allegro_Setup(); // Load Buffer Load_Buffer(); // Game Loop Game_Active = true; while (Game_Active == true) { Input(); Draw(); Check(); } Destroy(); return 0; } END_OF_MAIN() 

You want to be drawing the map in your Draw() function.

##### Share on other sites

My example was for Allegro 5. If you apply the same principles, it will work.

In Allegro 4 the draw function is:

draw_sprite()

If the code you pasted is your actual program, you're using allegro wrong. You cannot draw to the screen before allegro is initialized. You also did not load any graphics. I would suggest learning how to use the library properly before attempting any projects to limit further confusion.

Your main should look something like this:

 int main() { Allegro_Setup(); // Load Buffer Load_Buffer(); // Game Loop Game_Active = true; while (Game_Active == true) { Input(); Draw(); Check(); } Destroy(); return 0; } END_OF_MAIN() 

You want to be drawing the map in your Draw() function.

I tried to upgrade to allegro 5,but i dont know how to with code::blocks,is there any over good ide's?

##### Share on other sites
Why not use Visual Studio Express? Unless I'm wrong, you should be able to use Visual C++ 2010 Express fully with Allegro 5.

http://www.microsoft...ual-cpp-express

You might want to take advantage of Wiki for Allegro 5:

I hope this helps!

##### Share on other sites

Why not use Visual Studio Express? Unless I'm wrong, you should be able to use Visual C++ 2010 Express fully with Allegro 5.

http://www.microsoft...ual-cpp-express

You might want to take advantage of Wiki for Allegro 5:

I hope this helps!

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 13
• 30
• 9
• 16
• 12