这是我的 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(),所以我需要让它只在释放空格键时才停止执行,否则如果用户按住空格键超过一秒钟,它将不断在真假之间切换。
任何想法为什么我的程序在运行时变得无响应?我知道这与等待空格键被释放的位有关,因为当我删除那段代码时,我的程序运行良好(但显然暂停功能不起作用)。