Tile-Based Map

Started by
5 comments, last by flump9 12 years, 3 months ago
i'm making the snake game,but i dont know how to make a tile based map in c++ and allegro.Any help?
Advertisement
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...
GameDev Journal: http://www.gamedev.n...-rooks-journal/

OpenChess - 1.0 done!

Classic RPG #1 - Task 9 -> January 1st 2013

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
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.
GameDev Journal: http://www.gamedev.n...-rooks-journal/

OpenChess - 1.0 done!

Classic RPG #1 - Task 9 -> January 1st 2013

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?
Why not use Visual Studio Express? Unless I'm wrong, you should be able to use Visual C++ 2010 Express fully with Allegro 5.

Download Visual C++ 2010 Express here:

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

Download the Allegro 5 files here:

http://www.allegro.c...s/thread/608749

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

My linkhttp://wiki.allegro.cc/index.php?title=Allegro_5_Tutorial

I hope this helps!
GameDev Journal: http://www.gamedev.n...-rooks-journal/

OpenChess - 1.0 done!

Classic RPG #1 - Task 9 -> January 1st 2013

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

Download Visual C++ 2010 Express here:

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

Download the Allegro 5 files here:

http://www.allegro.c...s/thread/608749

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

My linkhttp://wiki.allegro.cc/index.php?title=Allegro_5_Tutorial

I hope this helps!


Thanks, i downloaded it!

This topic is closed to new replies.

Advertisement