[英]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.
我希望你能帮助我。
Thank you in advance!先感谢您!
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
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
的内容在“暂停”循环中永远不会改变:
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()
I editet my code to this one:我将我的代码编辑为这个:
def checkquit(e): running = True pause = False for ev in e: if ev.type == pygame.QUIT: exit(0) running = False pause = False def checkquit(e): running = True pause = False for ev in e: if ev.type == pygame.QUIT: exit(0) running = False pause = False
if ev.type == pygame.KEYDOWN and ev.key == pygame.K_ESCAPE:
quit(0)
running = False
pause = False
if ev.type == pygame.KEYDOWN and ev.key == pygame.K_p:
pause = True
running = False
while pause:
pause = True
red = (255,0,0)
screen = pygame.display.set_mode((800,800))
screen.fill((0,0,0))
myfont=pygame.font.SysFont("monospace",50)
myfonttwo=pygame.font.SysFont("monospace",10)
myfonttwo=pygame.font.SysFont("monospace",10)
text1=myfont.render("Pause!",100,red)
text2=myfont.render("Please resume your game!",100,red)
text3=myfont.render("Game starts in 10 seconds!",100,red)
screen.blit(text2,(50,200))
screen.blit(text1,(300,100))
screen.blit(text3,(0,300))
pygame.display.update()
pygame.time.delay(4500)
if ev.type == pygame.KEYDOWN and ev.key == pygame.K_p:
pause = False
And it works very good!而且效果很好! Thanks for your advices!
谢谢你的建议!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.