Take the 2-minute tour ×

I'm doing a tile based 2d rpg in pygame. The problem I'm working on right now is character movement... I am already able to create a map by using multidimensional arrays and a 'camera' follows the player. My problem is now the collision detection... The player should only move one tile at the time so I can easily get his position in tile coordinates and check if he's collided. But I don't know how to do that! At the moment, if a key is pressed, the player moves four pixels in a direction every frame. But that makes tile based collision detection nearly impossible. The player has the size of one tile and yeah, my question is, how can I make the player move only one tile (but NOT every frame since then he would move 32 pixels every single frame.) I know this question was asked before : Pygame Tile Based Character movement speed but this wouldn't work for me, because you had to press the key EVERY TIME you want to move one tile. I use booleans so the player moves as long as the key is pressed.

share|improve this question
"because you had to press the key EVERY TIME you want to move one tile". That's not true at all, I think you are misunderstanding the code sample. –  PeterT Jul 28 '13 at 11:09
If you say so... I'm quite sure I did it just the way described there. –  SemanticError Jul 28 '13 at 19:59
add comment

1 Answer

up vote 1 down vote accepted

To solve your problem check every frame if the button is pressed, if so increment the position.

To have the snapping effect you need to round down your position to tileX position like with this code (int)(PosX/TileSizeX).

If your player can move backwards just decrement the position if the other button is pressed.

share|improve this answer
Whoa right, I should have noticed that!! Works now, thanks. It will look even smoother with animations. I even had variables storing the player's position - but I didn't use them to blit him on the screen! –  SemanticError Jul 28 '13 at 19:02
add comment

Your Answer


