problem with a very simple tile based game
Posted
by
newbieguy
on Stack Overflow
See other posts from Stack Overflow
or by newbieguy
Published on 2011-01-15T15:19:22Z
Indexed on
2011/01/15
15:53 UTC
Read the original article
Hit count: 195
Hello, 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 :(
© Stack Overflow or respective owner