简体   繁体   中英

Pygame Tile Based Movement Speed

Thanks for taking the time to read this. Right now I'm making a really basic tile based game. The map is a large amount of 16x16 tiles, and the character image is 16x16 as well. My character has its own class that is an extension of the sprite class, and the x and y position is saved in terms of the tile position.

To note I am fairly inexperienced with pygame.

My question is, I am planning to have character movement restricted to one tile at a time, and I'm not sure how to make it so that, even if the player hits the directional key dozens of time quickly, (WASD or arrow keys) it will only move from tile to tile at a certain speed. How could I implement this generally with pygame? (Similar to game movement of like Pokemon or NexusTk). One movement would result in a player being in a tile. They couldn't stop halfway between tiles for example.

Thanks for your time! Ryan

You store your characters location as a grid coordinate. So if he's at (2,0) he is rendered at (32,0). The game then animates him moving between tiles, but, he's either on one or the other. While in the move state, you render an (x,y) offset between 0 to tilewidth.

It sounds like you want one move per keypress, if time elapsed / animation has completed. So:

  1. On keypress, toggle to: animating state
  2. Set destination tile coordinate
  3. draw offset, between 0 and tilewidth , depending on time elapsed. offset = (elapsed_ms / 1000.) * tile_w would scale between 0 to 16 if time is less<= 1 second.
  4. Once time elapsed is >= animation length (I chose 1000. above), switch to stationary state.

If keypress happens while in animation state, ignore it.

Pygame example: using numpy for map array.

pygame演示截图

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM