python - constant movement in pygame -
this first pygame code not much, pretty straight forward. when want move player works love have movement steadily. when press left want move left. @ moment have press every time left button player moves left. have suggestions how 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: 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 approach aarons answer:
while not done: 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)
looking @ sloth's answer on similar question. seems have 2 potential approaches, either making use of set_repeat or get_pressed.
set_repeat
when keyboard repeat enabled, keys held down generate multiple pygame.keydown events. delay number of milliseconds before first repeated pygame.keydown sent. after pygame.keydown sent every interval milliseconds. if no arguments passed key repeat disabled. source
something job:
# set_repeat(delay, interval) pygame.key.set_repeat(1, 10) # delay of 0 disables repeat. # lower interval, faster movement be.
get_pressed
returns sequence of boolean values representing state of every key on keyboard. use key constant values index array. true value means button pressed. getting list of pushed buttons function not proper way handle text entry user. have no way know order of keys pressed, , rapidly pushed keys can unnoticed between 2 calls pygame.key.get_pressed(). there no way translate these pushed keys translated character value. see pygame.keydown events on event queue functionality. source
something should fit 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 @zedswhatshesaid's code, 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: 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)
Comments
Post a Comment