0

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

2 Answers 2

1

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()
Sign up to request clarification or add additional context in comments.

3 Comments

I do like how you structure your data in your dictionary. The issue is with PyGame. It steps through my dictionary, where each step the player rolls once spacebar is hit, but on the last step after the player rolls, the code immediately moves outside of the dictionary for-loop and the last player is unable to see their roll. I want the game to pause and wait for spacebar before clearing the screen and moving onto next part of game.
@Casey It looks like it does show the final player's dice, but pygame.display.update() kicks off too quickly after it drops out of the loop on the final run. I'm not sure it's a complete solution, but if you delete that statement and put it at the end of your loop, just after on_roll() is called, the dice for the final player does make it on screen long enough to be seen.
Thanks for your help! You might be interested in answering this second question of mine. stackoverflow.com/questions/51911734/…
0

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.

1 Comment

My game does hang but it doesn't continue after receiving input of any kind.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.