Question

I am trying to create a pacman-like game. I have an array that looks like this:

array:

1111111111111
1000000000001
1111110111111
1000000000001
1111111111111

1 = Wall, 0 = Empty space

I use this array to draw tiles that are 16x16 in size. The Game character is 32x32.

Initially I represented the character's position in array indexes, [1,1] etc.

I would update his position if array[character.new_y][charater.new_x] == 0

Then I translated these array coordinates to pixels, [y*16, x*16] to draw him.

He was lining up nicely, wouldn't go into walls, but I noticed that since I was updating him by 16 pixels each, he was moving very fast.

I decided to do it in reverse, to store the game character's position in pixels instead, so that he could use less than 16 pixels per move.

I thought that a simple if statement such as this:

if array[(character.new_pixel_y)/16][(character.new_pixel_x)/16] == 0

would prevent him from going into walls, but unfortunately he eats a bit of the bottom and right side walls.

Any ideas how would I properly translate pixel position to the array indexes? I guess this is something simple, but I really can't figure it out :(

Was it helpful?

Solution

you appear to have transposed your 2D array. Was that on purpose? (is it array[x][y] or array[y][x]?)

also, a game character of double the tile size would not fit in the example map array given!

edit:

if your character eats into the bottom and right by exactly half a tile and he/she doesn't overlap the top & right, then you need to offset your character by half a tile. (+/- 8 pixels to both x & y)

so when you, "translated these array coordinates to pixels, [y*16, x*16] to draw him." you should change this to: [(y*16)-8, (x*16)-8] to draw him when he is 32x32.

keep storing his position as [y*16, x*16], to follow the 16x convention.

this would be easier for you if you kept everything 16x16 first!

you should also store the tile_size_x & tile_size_y as a constant.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top