简体   繁体   中英

Pygame start menu working, but not Pause menu

I'm trying to integrate a "pause" menu and "main" menu in pygame, along with a screen that will show during the game. My code first loads the intro, or main menu, which works fine, and then handles a button click to call the main game loop function. The problem arises when, within that loop, I try to call the function for the pause menu. I use the same loops to do this so I'm not sure why one works and the other doesn't. Below I've added code snippets for reference.

The main menu (which is called first in the program):

def intro_screen():
    intro = True
    while intro:

        # Event listeners
        for event in pygame.event.get():

            main_window.fill(tan)

... I also have other buttons etc. which are unimportant, but to get out of the intro, a mouse event sets intro to false and calls the function game_loop:

# Handle the click event
if click[0] == 1:
    intro = False
    game_loop()

The game_loop function has a similar while loop:

def game_loop():
    playing = True
    paused = False

    while playing:
        # This is the main loop

        # Load in the play screens...
        main_window.fill(white)
        play_screen = PlayScreen(main_window)

And buttons etc... But when I set playing to False and paused to True, which calls the pause_screen() function, it doesn't pull up any new screen. It just stays on the game menu as if no event was received.

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                playing = False

            # Listen for the escape key and bring up the pause menu
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    playing = False
                    paused = True

        pygame.display.update()

    while paused:
        pause_menu()

For reference, these are all contained in functions. The last line of my code simply calls intro_screen(), which works fine.

I can provide the rest of the code if that would be helpful. The only thing I could think of is that the mouse event works and the keydown event doesn't, but my syntax looks to be correct from what I can tell.

Looks like your while loop for the pause menu is out of the game loop. Try this:

def game_loop():
    playing = True
    paused = False

while playing:
    # This is the main loop

    # Load in the play screens...
    main_window.fill(white)
    play_screen = PlayScreen(main_window)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            playing = False

        # Listen for the escape key and bring up the pause menu
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                playing = False
                paused = True

    pygame.display.update()

    while paused:
        pause_menu()

Why don't you create a separate function for your pause menu? This will be easier as you can then call it properly. For example:

    pause = True
    while pause:

        # Event listeners
        for event in pygame.event.get():

            main_window.fill(tan)```

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM