If you are using a tile map, then you should do it by what each tile equals. Your map should be loaded into an array (most likely you would load it from a text file). Once you do that there is going to be a value for each part of the array. Ok lets say you want you map to be 20x20, so you make a text file that is 20 characters wide and 20 characters long. For simplicity sake lets say that you only want grass, trees, and bushes as apart of your map (and of course your player). So you make the map look something like this: [btw] this is going to be a lot more info than you are looking for, but it is very useful info[/btw]
Code:
T T T T T T T T T T T G G G G B T T T
T T T T T T T T T T T G G G G B T T T
T T T T T T T T T T G G T G G B T T T
T T T T T T T T T T T G G G G B T T T
T T T T T T T T T T T G G G G B T T T
except it's going to be four times longer
now this is how you would load the map:
Code:
#define MAX_X 20
#define MAX_Y 20
#define GRASS 0
#define TREE 1
#define BUSH 2
#define PLAYER 3
#define VALID 1 /*since this number is one that means that the player can only move onto grass tiles*/
char test[MAX_X][MAX_Y]; /*you only need this array if you use letters as your characters instead of numbers in the map.txt file*/
int grid{MAX_X][MAX_Y];
void load_map()
{
ifstream filemap("map.txt");
for(int b = 0; b < MAX_Y; b++)
{
for(int z = 0; z < MAX_X; z++)
{
filemap >> test[z][b];
if(test[z][b] == 'G')
{
grid[z][b] = GRASS;
}
else if(test[z][b] == 'T')
{
grid[z][b] = TREE;
}
else if(test[z][b] == 'B')
{
grid[z][b] = BUSH;
}
}
}
filemap.close();
grid[5][5] = PLAYER: /*this is just to set that part of the map as the player but you should find the player's position by loading it from the map.txt file but you should be able to figure all that out*/
}
Now I'm going to show you how you would do the collision detection
Code:
struct CHARACTER
{
int pos_x, pos_y;
}player;
if(key[KEY_UP] && player.pos_y > 0 && grid[player.pos_x][player.pos_y-1] < VALID) /*notice I did -1 on the y-coord. that's needed because you are going to move 1 up*/
{
grid[player.pos_x][player.pos_y] = GRASS;
player.pos_y--;
grid[player.pos_x][player.pos_y] = PLAYER:
}
else if(key[KEY_RIGHT] && player.pos_x < MAX_X - 1 && grid[player.pos_x+1][player.pos_y] < VALID)
{
grid[player.pos_x][player.pos_y] = GRASS;
player.pos_x++;
grid[player.pos_x][player.pos_y] = PLAYER:
}
Well, that's about all you have to do if you are going to use a tile map, which I recommend for most games. In fact, there aren't too many 2D games that you wouldn't want to use a tile map.
Again, btw, this did show you how to do side scrolling because that's just a little bit more complex.