I have a problem with my pygame code, which seems correct but doesn't work as it should.
I want to do different actions depending the key pressed; when I put two cases, only one works.
I put my code below :
gamestate.draw(window)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.type == pygame.K_ESCAPE):
finis = True
if (event.type == pygame.KEYDOWN and event.key == pygame.K_UP):
print("ok")
pygame.display.update()
Here, my code print "ok" in the terminal when I press the UP key, but doesn't quit when I keep the escape key pressed.
It isn't this particular case that doesn't work, before it quit when i pressed escape and didn't print "ok" when i pressed the up key.
Do you have any idea to solve my issue ? Thanks a lot !
pygame.K_ESCAPE
is not an event type but you check for event.type
:
... and event.type == pygame.K_ESCAPE
It should be:
... and event.key == pygame.K_ESCAPE
Also usually if you have to check for multiple keys on pygame.KEYDOWN
event, then use nested if
s:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
finis = True
if event.key == pygame.K_UP:
print('ok')
Also you need to call pygame.display.update
only ONCE in the loop (most likely at the end) NOT twice