Reputation: 477
This is my first pygame code not much, pretty straight forward. When I want to move my player it works but I would love to have the movement steadily. Like when I press left I want it to move left. At the moment I have to press every time the left button so the player moves to the left. Do you have any suggestions how to do that?
import pygame, sys
pygame.init()
window_size = ( 400, 400 )
white = ( 255, 255, 255 )
class Player():
image = pygame.image.load( 'foo.png')
rect = image.get_rect()
player = Player()
screen = pygame.display.set_mode( window_size )
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
move = (-10, 0 )
player.rect = player.rect.move(move)
if event.key == pygame.K_RIGHT:
move = ( 10, 0 )
player.rect = player.rect.move(move)
if event.key == pygame.K_UP:
move = ( 0,-10 )
player.rect = player.rect.move(move)
if event.key == pygame.K_DOWN:
move = ( 0, 10 )
player.rect = player.rect.move(move)
screen.fill( white )
screen.blit( player.image, player.rect )
pygame.display.flip()
Edit My approach to Aarons answer:
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pressed = pygame.key.get_pressed()
if pressed[pygame.K_LEFT]:
move = ( 2, 0 )
player.rect = player.rect.move(move)
if pressed[pygame.K_RIGHT]:
move = ( 2, 0 )
player.rect = player.rect.move(move)
if pressed[pygame.K_UP]:
move = ( 0,-2 )
player.rect = player.rect.move(move)
if pressed[pygame.K_DOWN]:
move = ( 0, 2 )
player.rect = player.rect.move(move)
Upvotes: 1
Views: 3135
Reputation: 2335
Looking at Sloth's answer on a similar question. It seems that you have two potential approaches, either making use of set_repeat or get_pressed.
set_repeat
When the keyboard repeat is enabled, keys that are held down will generate multiple pygame.KEYDOWN events. The delay is the number of milliseconds before the first repeated pygame.KEYDOWN will be sent. After that another pygame.KEYDOWN will be sent every interval milliseconds. If no arguments are passed the key repeat is disabled. Source
Something like this would do the job:
# set_repeat(delay, interval)
pygame.key.set_repeat(1, 10)
# A delay of 0 disables the repeat.
# The lower the interval, the faster the movement would be.
get_pressed
Returns a sequence of boolean values representing the state of every key on the keyboard. Use the key constant values to index the array. A True value means the that button is pressed. Getting the list of pushed buttons with this function is not the proper way to handle text entry from the user. You have no way to know the order of keys pressed, and rapidly pushed keys can be completely unnoticed between two calls to pygame.key.get_pressed(). There is also no way to translate these pushed keys into a fully translated character value. See the pygame.KEYDOWN events on the event queue for this functionality. Source
Something like this should fit into your code nicely:
pressed = pygame.key.get_pressed()
if pygame.K_LEFT in pressed:
# move left
if pygame.K_RIGHT in pressed:
# move right
Solution implemented into @ZedsWhatSheSaid's code, also added PEP-8 formatting:
import pygame
import sys
pygame.init()
window_size = (400, 400)
white = (255, 255, 255)
class Player():
image = pygame.image.load('foo.png')
rect = image.get_rect()
player = Player()
screen = pygame.display.set_mode(window_size)
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pressed = pygame.key.get_pressed()
if pygame.K_LEFT in pressed:
move = (-10, 0)
player.rect = player.rect.move(move)
if pygame.K_RIGHT in pressed:
move = (10, 0)
player.rect = player.rect.move(move)
if pygame.K_UP in pressed:
move = (0, -10)
player.rect = player.rect.move(move)
if pygame.K_DOWN in pressed:
move = (0, 10)
Upvotes: 2
Reputation: 2549
You could track both KEYUP
and KEYDOWN
events then store a pressed state for each of the keys you are tracking.
Now instead of moving the player whenever the user presses a key, you test the state of the key to see if it is pressed and apply movement to the player if it is.
As a simplified example:
pressed_keys = {
'left': false,
...define other keys...
}
while not done:
# Check for key events
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
pressed_keys['left'] = True
...check other keys...
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
pressed_keys['left'] = False
...check other keys...
# Move the player
if pressed_keys['left']:
...move player left...
Upvotes: 3