GlerG
GlerG

Reputation: 65

pygame.key.get_pressed() appears to be frozen

I'm trying to make a menu system for my game where you start at the main menu, then you can go to the tutorial by pressing space, then to the game by pressing space again. The problem is I needed to make sure the player has let go of space before letting them exit the tutorial but pygame.key.get_pressed() reports that the same keys that were being pressed when you exited the menu are still being pressed even if they aren't. Here's my code.

def menu(normal=True):
    global keys
    while True:
        pygame.time.wait(33)
        keys = pygame.key.get_pressed()
        if keys[pygame.K_SPACE]:
            return True
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                return False

#Main menu
displayMenuText(0)
menu()

#Tutorial
displayMenuText(1)
while pygame.key.get_pressed()[pygame.K_SPACE]:
    pygame.time.wait(33)
menu()

The menu() function returning a boolean value is used elsewhere in the code.

Upvotes: 1

Views: 201

Answers (1)

Matteo Fiorina
Matteo Fiorina

Reputation: 73

You could use the pygame.KEYUP event to check for key press.

events = pygame.event.get()
for event in events:
    if event.type == pygame.KEYUP:
       if event.key == pygame.K_SPACE:
           return True

Upvotes: 2

Related Questions