what part of the array is a character on

i am using allegro 5 and i have drawn a map. i got a fairly simple tile set. now i am trying to figure out how to add collision to specific tiles to have an outcome like make the player not able to walk on water or through walls.

what i think would be a good idea is draw an invisible square corresponding to each number in the map and test if when the character presses a key if that square has the same x and y as the new coordinates of the character before it moves. how would i go about doing that?

this is basically what the map is. except more complex.
1
2
3
int map[] = {1, 1, 1, 1, 1,
             1, 0, 0, 0, 1,
             1, 1, 1, 1, 1};


i have drawn the tiles using this:
1
2
3
4
for(int i = 0; i < mapSize; i++)
{
al_draw_bitmap_region(bgSheet, tileSize * map[i], 0, tileSize, tileSize, xoff + tileSize * (i % mapColums), yoff + tileSize * (i / mapColums), 0);
}


my entire program can be found here:
https://sites.google.com/site/temprpgquestion/go-here-for-the-program

keeping things simple, if your map was stored like this...

1
2
3
4
5
6
7

int map[3][5] = { 
	1, 1, 1, 1, 1,
	1, 0, 0, 0, 1,
	1, 1, 1, 1, 1 
};


3 being down, 5 being across...

Store the x (across) , y (down) of player, when player goes to move use the x and y of player to access array and check if its a wall or water.. assuming 1 is a wall...

1
2
3
4
5

if (map[playerY][playerX] == 1) {
	// we hit a wall, don't allow him to move
}
so using map[3][5]

how would i need to adjust the draw tiles statement?

because if i leave it as map[i] i will for sure get an error. right?

i will have to look to see what number is at map[mapx][mapy] right?

Adjusting the tiles statement depends on how you have your tiles arranged.

An example will help answer your questions on how you could draw the map:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50



#include <iostream>
using namespace std;

// example map
int map[3][5] = {
	1, 1, 1, 1, 1,
	0, 1, 1, 0, 1,
	0, 1, 1, 0, 1
};

// function declarations
void doMap();

int main()
{

	doMap();

	return 0;

}

void doMap()
{
	int tileId;
	int tilex = 0, tiley = 0;		// screen position start

	// assuming your tiles are 64 x 64 pixels

	for (int down = 0; down < 3; down++)
	{
		for (int across = 0; across < 5; across++)
		{
			// we now have down and across, so... 
			tileId = map[down][across];
			
			// draw tile of tileId at tilex, tiley 

			tilex += 64;	// move screen pos over for next tile

		}
		tiley += 64;	// move screen pos down for next tile.
		tilex = 0;	// reset x

	}
}


EDIT: spotted a typo, had a += when it should have been =
Last edited on
thank you so much!
Your welcome
Topic archived. No new replies allowed.