0

这是我的 check_for_pause() 函数:

#Check if the user is trying to pause the game
def check_for_pause():
   keys=pygame.key.get_pressed() #Get status of all keys
   if keys[K_SPACE]: #The space bar is held down
       global paused #Make global so it can be edited
       if paused==True: #It was paused, so unpause it
           paused=False
       elif paused==False: #It was playing, so pause it
           paused=True

        #Don't let the main loop continue until the space bar has been released again, otherwise the variable will flicker between True and False where the loop runs so fast!
        space_bar_pressed=keys[K_SPACE]
        while space_bar_pressed: #Repeat this loop until space_bar_pressed is False
           keys=pygame.key.get_pressed()
           if not keys[K_SPACE]: #Space bar has been released so set space_bar_pressed to False
              space_bar_pressed=False

但是,每当我尝试暂停它时,这都会使我的程序变得无响应!基本上,我希望变量“暂停”为真或假。按下空格键时,它应该更改为当前不是的那个。因为我在另一个永无止境的循环中使用了 check_for_pause(),所以我需要让它只在释放空格键时才停止执行,否则如果用户按住空格键超过一秒钟,它将不断在真假之间切换。

任何想法为什么我的程序在运行时变得无响应?我知道这与等待空格键被释放的位有关,因为当我删除那段代码时,我的程序运行良好(但显然暂停功能不起作用)。

4

2 回答 2

6

你有一个可能看起来像这样的主循环......

while True:
    check_for_pause()
    # Update the game
    # Draw the game

当您检查暂停并按下空格键时,暂停设置为 True。然后,你有这个循环......

space_bar_pressed=keys[K_SPACE]
while space_bar_pressed: #Repeat this loop until space_bar_pressed is False
   keys=pygame.key.get_pressed()
   if not keys[K_SPACE]: #Space bar has been released so set space_bar_pressed to False
      space_bar_pressed=False

此循环的问题在于您假设 pygame.key.get_pressed() 将继续返回最新信息。但是,查看pygame 源代码,它似乎使用了 SDL_GetKeyState,这是其文档的一部分。

Note: Use SDL_PumpEvents to update the state array.

换句话说,重复调用 pygame.key.get_pressed() 如果你没有额外调用类似的东西,它不会给你更新的键状态pygame.event.pump(),它实际上用新的键状态更新 pygame。因此,您可以通过将泵功能引入循环来快速解决此问题,因为目前它只是永远运行。

话虽这么说:不要这样做。如果您这样做,您的游戏在暂停时将无法执行任何操作:这包括显示“暂停”屏幕、继续在后台播放音乐等。您想要做的是跟踪是否游戏已暂停,如果是,请更改游戏的更新方式。

在您更新内容的游戏部分中,某些项目仅在游戏暂停时才会发生。就像是...

paused = False
while True:
    # This function should just return True or False, not have a loop inside of it.
    paused = check_for_paused()

    if not paused:
        # This function moves your enemies, do physics, etc.
        update_game()

    draw_game()

在这种情况下,主循环仍然会发生,游戏会继续绘制,输入会继续处理。但是,敌人和玩家不会移动,因此游戏可以说是“暂停”了。

最后,还有一个事实是您依赖于 get_key_pressed(),您可能不想这样做。请参阅我给出的其他类似答案,以了解您应该使用事件队列的原因。

于 2013-09-23T20:39:04.717 回答
3

即使它暂停了,你也不应该停止在游戏中运行你的游戏循环。暂停通常也是通过事件来处理的。例如看这段代码:

import pygame, sys
from pygame.locals import *
pygame.init()
pygame.display.set_mode((400,400))

paused = False # global

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == KEYDOWN:
            if event.key == K_SPACE:
                paused = not paused

    if paused:
        continue # skip this iteration if paused

    # add your game code here
    print 'game code running'

在上面的代码中,我每次按空格键时都会暂停。如果您只想在按住空格键时暂停,请执行以下操作:

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        elif event.type in (KEYDOWN, KEYUP): # checks membership in tuple
            if event.key == K_SPACE:
                paused = not paused

    if paused:
        continue # skip this iteration if paused

    # add your game code here
    print 'game code running'

作为一般说明,您应该始终处理来自事件队列的事件,否则 Pygame 只会哭泣和抱怨并且什么都不做(没有响应)。因此,即使您暂停了游戏,也永远不要停止游戏循环。

编辑:或者,正如 abarnert 在评论中指出的那样,您可以使用相等比较来确保您永远不会在 KEYDOWN 和 KEYUP 事件之间发生冲突:

paused = event.type == KEYDOWN

这样,您就不会遇到“同步问题”,即paused每当您实际释放空格键时代码意外设置为 True。如果连续发生 2 个 KEYDOWN 事件或连续发生 2 个 KEYUP 事件(而不是像 KEYDOWN、KEYUP、KEYDOWN、KEYUP 等平滑交替序列),则会发生这种情况。最好不要假设所有事件队列都提供 100% 准确的事件。

于 2013-09-23T20:44:27.283 回答