Casey
Casey

Reputation: 552

Wait for keydown before continuing in Pygame

I want my program to cycle through each player, and this code does that. However, on the last player, it displays the info then instantly clears it. I want it to wait for a user to press a keydown (like space or enter) before clearing the screen. I tried implementing this with event = pygame.event.wait() but now my program just hangs when it reaches that declaration.

players = {}
            for player in range(1, int(num_players)+1):
                name = ask(DISPLAYSURF, "Player " + str(player) + "'s name")
                player_roll = None
                while player_roll is None:
                    for event in pygame.event.get():
                        if event.type == pygame.KEYDOWN:
                            pygame.event.clear()
                            while event.type != pygame.KEYDOWN:
                                event = pygame.event.wait()
                                DISPLAYSURF.fill(WHITE)
                                FIRST_DICE = roll_a_dice()
                                SECOND_DICE = roll_a_dice()
                                player_roll = FIRST_DICE + SECOND_DICE
                                players[name] = player_roll
                                display_dice(FIRST_DICE, SECOND_DICE)
                                our_roll(name)

My full code is here: https://github.com/Legitimate/Street-Dice/blob/master/main.py

Here is a video of the issue: https://youtu.be/ChxZq0bY4wk

Upvotes: 0

Views: 1029

Answers (2)

Tim Pozza
Tim Pozza

Reputation: 548

It took a few minutes to understand what you meant, but after refactoring the list of players as the code below shows, the rest sort of rolled itself: https://github.cm/rebelclause/python_legs/blob/master/pygame/roll_the_dice.py. If it works for you, buy me a beer ;)

players = {'you': {'rolltotal': None, 'otherstuff': None }, 'me': {'rolltotal': None, 'otherstuff': None}}

def reviewvals():
    print('Number of players: ', len(players)) # only counts the primary keys, the players                
    for player, attribdict in players.items():
        for key, value in attribdict.items():
            print(player, key, value)

reviewvals()

Upvotes: 1

Tanmay jain
Tanmay jain

Reputation: 814

I haven't used Pygame before so I don't know if there is more efficient/ or right way of doing what you have asked for but anyway try this

Check this out Pygame waiting the user to keypress a key

Also from docs -- this is why your program doesn't respond( which seem like it got hanged/ stuck but its not)..when it reaches event.wait() https://www.pygame.org/docs/ref/event.html#comment_pygame_event_wait

pygame.event.wait() wait for a single event from the queue wait() -> EventType instance

Returns a single event from the queue. If the queue is empty this function will wait until one is created. The event is removed from the queue once it has been returned. While the program is waiting it will sleep in an idle state. This is important for programs that want to share the system with other applications.

Upvotes: 0

Related Questions