Adding a play again option to python game

后端 未结 1 1134
無奈伤痛
無奈伤痛 2021-01-29 12:51

I\'m working on making a game for my programming class using python. I don\'t know how to give the option to the player again when they lose, or quit the game. I am using python

相关标签:
1条回答
  • 2021-01-29 13:34

    First of, one suggestion. People will be more likely to help you if you show them what you've tried.

    anyhow.

    What I would do is wrap your entire game loop in a function. Like so:

    def game_loop():
        while True:
        # check for the QUIT event
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
                ....
    

    next when ever your game ends(I'm assuming it does have a condition that makes it end), call a game over screen function. In your game over screen function, you could first fill the window with a color, then blit any text to the screen you wan to show the user, such as "Press any key to play again". Then you would check if any key was pressed and if so call the game loop function. You also need to check if the user is trying to close the window.

    Then call the game over screen function like so:

    #pseudo code
    if game_has_ended == True:
       game_over_screen()
    
    0 讨论(0)
提交回复
热议问题