Search code examples
pythonpygamegame-physicskeyboard-events

Pygame: Movement code doesn't work


I have a problem with character movement in pygame. What I want to do is to be able to hold a key down, and have the sprite move. But, I have to keep on tapping the key to make the sprite move. Any help would be greatly appreciated! Here's my code:

key_state = pygame.key.get_pressed()
if key_state [K_LEFT]:
   world.move(xx)
   doom.move(xx)
if key_state [K_RIGHT]:
   world.move(xx)
   doom.move(xx)
if key_state[K_UP]
   player.jump(jump_speed)

Thanks!


Solution

  • I quote from: http://pygame.org/ftp/contrib/input.html

    Keyboard Control

    The keyboard is probably the simplest input device. Keys are represented by their key id value. The only real control you have over the keyboard is setting repeat rates. By default, pygame sends a single KEYDOWN and KEYUP event for every keypress. You can enable key repeating with pygame.key.set_repeat(). The defaults are usually fine, but you can fine tune the repeat behavior. When keys are repeating, you will receive multiple KEYDOWN events for as long as the key is held, and a final KEYUP when it is released.