Python 而循环未更改为False?
在我的游戏中,我有一个mainloop变量,当它运行时执行游戏的主要部分,当游戏结束时,它应该更改为False。我已经试着去实现它,所以当我的生活在另一个循环中时,我开始了。在这个循环中,有一个按钮可以打开主循环,但是因为它从来没有变为false,所以我不能再打开它Python 而循环未更改为False?,python,python-3.x,pygame,Python,Python 3.x,Pygame,在我的游戏中,我有一个mainloop变量,当它运行时执行游戏的主要部分,当游戏结束时,它应该更改为False。我已经试着去实现它,所以当我的生活在另一个循环中时,我开始了。在这个循环中,有一个按钮可以打开主循环,但是因为它从来没有变为false,所以我不能再打开它 MainLoop = True while MainLoop: # Setting fps clock.tick(60) for event in pygame.event.get(): if
MainLoop = True
while MainLoop:
# Setting fps
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
MainLoop = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
MainLoop = False
# Getting keys pressed
keys = pygame.key.get_pressed()
#Walking controls for player
if keys[pygame.K_LEFT] and man.x > 0:
man.x -= man.vel
man.left = True
man.right = False
elif keys[pygame.K_RIGHT] and man.x < 500 - man.vel - man.width:
man.x += man.vel
man.left = False
man.right = True
else:
man.left = False
man.right = False
man.walkCount = 0
redrawgamewindow()
for rockk in my_list:
rockk.draw(win)
rockk.move()
#Controlling man lives and game over screen
if man.hit == 1:
man.lives -=1
elif man.hit ==2:
man.lives -=1
elif man.lives <= 0:
Mainloop = False
GameOver = True
collided(rockk.x, rockk.y, man.x, man.y)
pygame.display.flip()
print (GameOver)
print (MainLoop)
while GameOver:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
GameOver = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
GameOver = False
win.blit(GameO, (0, 0))
button1 = pygame.Rect(200, 400, 100, 50)
pygame.draw.rect(win, [255, 0, 0], button1)
if event.type == pygame.MOUSEBUTTONDOWN:
mouse_pos = event.pos
if button1.collidepoint(mouse_pos):
print ('amazing ')
Text = pygame.font.Font('freesansbold.ttf', 20)
TextSurf, TextRect = text_objects("Restart?", Text)
TextRect.center = ((250), (425))
win.blit(TextSurf, TextRect)
pygame.display.update()
MainLoop=True
而主循环:
#设置fps
时钟滴答(60)
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
MainLoop=False
elif event.type==pygame.KEYDOWN:
如果event.key==pygame.K_退出:
MainLoop=False
#按键
keys=pygame.key.get_pressed()
#游戏者的行走控制
如果键[pygame.K_LEFT]和man.x>0:
man.x-=man.vel
左=真
对=错
elif键[pygame.K_RIGHT]和man.x<500-man.vel-man.width:
man.x+=man.vel
左=假
正确的
其他:
左=假
对=错
man.walkCount=0
重画游戏窗口()
对于my_列表中的rockk:
洛克平局(胜利)
rock.move()
#在屏幕上控制人的生命和游戏
如果man.hit==1:
人的生命-=1
elif man.hit==2:
人的生命-=1
elif man.lifes您在获取pygame.events的过程中陷入困境。
你已经从这个循环中挣脱出来了,你还可以从外循环中挣脱出来
请尝试:
for event in pygame.event.get():
if event.type == pygame.QUIT:
MainLoop = False
break
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
MainLoop = False
break
if not MainLoop:
break
请同时更改以下部分
while GameOver:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
GameOver = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
GameOver = False
到
为什么投反对票的人从来不解释原因?我该如何改进?请重试。我强化了答案。还有一个部分,您必须中断。在GameOver:
循环中更改后是否重试?
while GameOver:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
GameOver = False
break
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
GameOver = False
break