python pygame暂停功能 [英] python pygame pause function
问题描述
我是初学者,我的代码有问题.在这里你可以看到我的代码的简短摘录.
I am beginner and have a problem with my code. Here you can see a short excerpt of my code.
这是我创建的一个简单的蛇游戏,但我试图添加一个暂停.我知道了,但是当我开始暂停时,我无法关闭它.
It's a simple snake game I created but I was trying to add a pause. I got it but when I start the pause I am not able to close it.
可能我的代码中有一个基本错误,所以我无法前进.我希望你能帮助我.
Possibly there is a basic mistake in my code so I couldn't advance. I hope you can help me.
先谢谢你!
def checkquit(e):
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 = False
while pause:
#running = False
pause = True
red = (255,0,0)
screen = pygame.display.set_mode((800,500))
screen.fill((0,0,0))
my_font = pygame.font.SysFont("monospace", 50)
my_font_two = pygame.font.SysFont("monospace", 10)
text1 = myfont.render("Pause!", 100, red)
text2 = myfont.render("Please restart the game", 100, red)
screen.blit(text2, (10, 200))
screen.blit(text1, (230, 100))
pygame.display.update()
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
推荐答案
暂停屏幕显示在单独的应用程序循环中.您也必须在该循环中获取事件.请注意,在您的代码中,e
的内容在暂停"循环中永远不会改变:
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:
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
似乎是全局命名空间中的一个变量.你必须使用 global
语句 来改变它的状态.此外,在暂停"循环中重新创建窗口表面是多余的.
runnung
seems to be a variable in global namespace. You've to use the global
statement to change its state.
Furthermore it is superfluous to recreate the window surface in the "pause" loop.
screen = pygame.display.set_mode((800,500))
我建议更改游戏流程.使用 1 个应用程序循环.例如:
I recommend to change the game process. Use 1 application loop. e.g.:
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()
这篇关于python pygame暂停功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!