[英]Why isn't this while loop reitterating?
For some odd reason(I'm sort of new to python/pygame) this while loop isn't loading the other one. 由于一些奇怪的原因(我是python / pygame的新手),这个while循环不会加载另一个。 I have it so that there is two players. 我有两个球员。 It starts on player one in a while loop and then once player one is done it turns the second while loop to true and so on so forth. 它从一个循环的播放器中开始,然后在一个循环播放器完成后将第二个循环变为true,依此类推。 But once the second loop has finished it doesn't turn the first one to true just leaves ends the program. 但是,一旦第二个循环完成,它就不会将第一个循环变为true,只会使程序结束。
Loop one: 循环一:
game = True
while game:
for event in pygame.event.get():
pygame.event.get()
if event.type == pygame.KEYDOWN and event.key == pygame.K_a:
count1 = pygame.draw.circle(window, (255, 255, 255),(200, countY - 100), 25, 0)
game = False
game2 = True
print("Test")
pygame.display.update()
Loop two(This should turn the first loop to true when "J" is pushed): 循环二(按下“ J”时,这将使第一个循环为真):
while game2:
for event in pygame.event.get():
pygame.event.get()
if event.type == pygame.KEYDOWN and event.key == pygame.K_j:
count3 = pygame.draw.circle(window, (255, 255, 255),(500, countY - 100), 25, 0)
game2 = False
game = True
print("Test third")
pygame.display.update()
You'd need another loop above the two while game
and while game2
loops: 您需要在两个while game
和while game2
循环之上的另一个循环:
while True:
while game:
#
while game2:
#
Now when game2
ends, the outermost while
loop goes back up to the first while game
loop. 现在当game2
结束时,最外面的while
循环返回到第一个while game
循环。
Ending the game would require you to break out of the while True
loop; 结束游戏需要您退出while True
循环; you could use the break
keyword for that, executed outside either of the while game
or while game2
loops. 您可以为此使用break
关键字,该关键字可以在while game
或while game2
循环之外执行。
while
loops in Python do not constantly check for their condition to be True
. Python中的while
循环不会经常检查其条件是否为True
。
The game
while loop is executed, as you have said, before the game2
loop. 该game
while循环执行,正如你所说,在之前game2
循环。
while game:
# do stuff
while game2:
# do stuff
When game2
sets game
to True, this doesn't mean the while game
loop will start executing again. 当game2
将game
设置为True时,这并不意味着while game
循环将再次开始执行。 The body of a while
loop and its recursions are only executed once. while
循环的主体及其递归仅执行一次。 Python will continue executing the rest of the script and terminate. Python将继续执行脚本的其余部分并终止。 Without any other control flow, there is no way for the while game
loop to be executed again. 没有任何其他控制流程,就无法再次执行while game
循环。
Another alternative is to turn game
and game2
into functions and call them in the other functions when their games are supposed to start. 另一种选择是将game
和game2
转换为函数,并在应该开始游戏时在其他函数中调用它们。
If execution of game2
is supposed to end once game
begins, then something like this would suffice: 如果执行game2
应该结束,一旦game
开始,然后像这样就足够了:
def game():
while True:
for event in pygame.event.get():
pygame.event.get()
if event.type == pygame.KEYDOWN and event.key == pygame.K_a:
count1 = pygame.draw.circle(window, (255, 255, 255),(200, countY - 100), 25, 0)
game2() # Begin player 2 game
print("Test")
pygame.display.update()
return # Stop execution of game()
def game2():
while True:
for event in pygame.event.get():
pygame.event.get()
if event.type == pygame.KEYDOWN and event.key == pygame.K_j:
count3 = pygame.draw.circle(window, (255, 255, 255),(500, countY - 100), 25, 0)
game() # Begin player 1 game
print("Test third")
pygame.display.update()
return
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.