0
votes

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.

2
Could you provide your whole code, or just parts where paused and playing are first defined and usedkaktus_car
It appears my code is a bit too long to comment here. Is there a way I can post the entire thing? I don't see a way to attach anything other than comments.JWilliams12117
You can use pasteubuntu for example and put the link in the commentkaktus_car

2 Answers

0
votes

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()
0
votes

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)```