2
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ "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. \$\endgroup\$ – PeterT Jul 28 '13 at 11:09
  • \$\begingroup\$ If you say so... I'm quite sure I did it just the way described there. \$\endgroup\$ – SemanticError Jul 28 '13 at 19:59
1
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ 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! \$\endgroup\$ – SemanticError Jul 28 '13 at 19:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.