简体   繁体   English

Pygame 开始菜单工作,但不暂停菜单

[英]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.我正在尝试在 pygame 中集成“暂停”菜单和“主”菜单,以及在游戏期间显示的屏幕。 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.我的代码首先加载介绍或主菜单,它工作正常,然后处理按钮单击以调用主游戏循环 function。 The problem arises when, within that loop, I try to call the function for the pause menu.当我在该循环中尝试调用 function 以获取暂停菜单时,就会出现问题。 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: ...我还有其他不重要的按钮等,但要退出介绍,鼠标事件将介绍设置为 false 并调用 function 游戏循环:

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

The game_loop function has a similar while loop: game_loop function 有一个类似的 while 循环:

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.还有按钮等...但是当我将播放设置为 False 并暂停为 True 时,它调用了 pause_screen() function,它不会拉出任何新屏幕。 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.我的代码的最后一行简单地调用了 intro_screen(),它工作正常。

I can provide the rest of the code if that would be helpful.如果有帮助,我可以提供代码的 rest。 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.我能想到的唯一一件事是鼠标事件有效而 keydown 事件无效,但据我所知,我的语法看起来是正确的。

Looks like your while loop for the pause menu is out of the game loop.看起来暂停菜单的 while 循环不在游戏循环中。 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?为什么不为暂停菜单创建一个单独的 function? 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)```

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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