python - Pygame Keyup/Keydown -



python - Pygame Keyup/Keydown -

i'm making little mario school's computer club. (well, part of team.) anyway, i'm having problem "keyup/keydown" commands. here's code:

# 1 - import library import pygame pygame.locals import * # 2 - initialize game pygame.init() width, height = 1280, 1000 screen=pygame.display.set_mode((width, height)) keys = [false, false, false, false] playerpos=[100,100] # 3 - load images player = pygame.image.load("images/totallynotgodzilla.png") # 3.1 - load sound music = pygame.mixer.sound("audio/skyrim.wav") # 4 - maintain looping through while 1: # 5 - clear screen before drawing 1 time again screen.fill(0) # 6 - draw screen elements screen.blit(player, playerpos) # 7 - update screen pygame.display.flip() # 8 - loop through events event in pygame.event.get(): if event.type == pygame.keydown: if event.key==k_w: keys[0]=true elif event.key==k_a: keys[1]=true elif event.key==k_s: keys[2]=true elif event.key==k_d: keys[3]=true if event.type == pygame.keyup: if event.key==pygame.k_w: keys[0]=false elif event.key==pygame.k_a: keys[1]=false elif event.key==pygame.k_s: keys[2]=false elif event.key==pygame.k_d: keys[3]=false # 9 - move player if keys[0]: playerpos[1]-=5 elif keys[2]: playerpos[1]+=5 if keys[1]: playerpos[0]-=5 elif keys[3]: playerpos[0]+=5

basically, problem when press key down, waits keyup command happen before moving again. have rapidly press downwards buttons move.

i deleted of code, if missing, allow me know , i'll tell whether or not have it.

indenting problem. need test key states in main game loop not in event loop. need unindent keystate test 1 level.

while 1: # init stuff screen.fill(0) # .... (all main loop init stuff here) event in pygame.event.get(): # test events, set key states if event.type == pygame.keydown: if event.key==k_w: keys[0]=true # .... (all event stuff) # indent moves main game loop # test key states here... if keys[0]: playerpos[1]-=5 elif keys[2]: playerpos[1]+=5 # .... (and on)

python pygame keydown keyup

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -