The pause screen is displayed in a separate application loop.
(暂停屏幕显示在单独的应用程序循环中。)
You've to get the events in that loop, too. (您还必须在该循环中获取事件。)
Note, in your code, the content of e
never changes in the "pause" loop: (注意,在您的代码中, e
的内容在“ pause”循环中永远不会改变:)
def checkquit(e):
global running
running = True
pause = False
for ev in e:
if ev.type == pygame.QUIT:
exit(0)
running = True
if ev.type == pygame.KEYDOWN and ev.key == pygame.K_ESCAPE:
quit(0)
running = True
if ev.type == pygame.KEYDOWN and ev.key == pygame.K_p:
pause = True
while pause:
# [...]
# get the new events
e = pygame.event.get()
# handle the events in the loop
for ev in e:
if ev.type == pygame.QUIT:
pause = False
if ev.type == pygame.KEYDOWN and ev.key == pygame.K_ESCAPE:
pause = False
if ev.type == pygame.KEYDOWN and ev.key == pygame.K_p:
pause = True
runnung
seems to be a variable in global namespace.
(runnung
似乎是全局名称空间中的变量。)
You've to use the global
statement to change its state. (您必须使用global
语句来更改其状态。)
Furthermore it is superfluous to recreate the window surface in the "pause" loop. (此外,在“暂停”循环中重新创建窗口表面是多余的。)
screen = pygame.display.set_mode((800,500))
I recommend to change the game process.
(我建议更改游戏过程。)
Use 1 application loop. (使用1个应用程序循环。)
eg: (例如:)
myfont=pygame.font.SysFont("monospace",50)
myfonttwo=pygame.font.SysFont("monospace",10)
text1=myfont.render("Pause!",100,red)
text2=myfont.render("Please restart the game",100,red)
def checkquit(e):
global running, pause
for ev in e:
if ev.type == pygame.QUIT:
exit(0)
running = True
if ev.type == pygame.KEYDOWN and ev.key == pygame.K_ESCAPE:
if pause:
pause = False
else:
quit(0)
running = True
if ev.type == pygame.KEYDOWN and ev.key == pygame.K_p:
pause = not pause
running, pause = True, False
while running:
events = pygame.event.get()
checkquit(events)
screen.fill((0,0,0))
if pause:
# draw pause screen
screen.blit(text2,(10,200))
screen.blit(text1,(230,100))
else:
# draw game
# [...]
pygame.display.update()
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…