I am not even sure is that is what it really is. I am using allegro and i want to make it so certain areas on a back ground bitmap are off limits u can't bring the sprite over top of them. I have no idea how to do this????
thanx?
Printable View
I am not even sure is that is what it really is. I am using allegro and i want to make it so certain areas on a back ground bitmap are off limits u can't bring the sprite over top of them. I have no idea how to do this????
thanx?
what I want is there is a sprite which represents a person. I have a bitmap that I load as the map. I only want the sprite to be able to walk on the road portions of the map.
Preseumeably you have coordinates for your player, that you alter when the player presses the move keys.
So you can prevent him from moving to a certain area by putting conditional statements in the code that deals with the player input.
For eample say x and y are the variables holding the player position, and when the up key is pressed you decrease y by 10. If there was a portion of the screen you didnt want him to walk over.
You would have code that looks something like
now your player cannot move forward past y=25.Code:if (Up-key)
{
y -= 10;
if (y < 25)
y = 25;
}
Now that only works if your map is stationary.
If you have a scrolling map, the easiest way to do it is through colour coding; you line your road with pixels of a specific colour and then in your movement code you only alter the player coordinates if they wont put the player on the road border colour.
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]
now this is how you would load the map: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 I'm going to show you how you would do the collision detectionCode:
#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*/
}
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.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:
}
Again, btw, this did show you how to do side scrolling because that's just a little bit more complex.