有谁知道如何重新启动程序,例如,当玩家达到5分时,在文本“玩家a获胜”之后,它会给出一定的时间延迟,然后直接从0 0重新启动游戏。我创建了分数很重要,但是一旦达到 5 分就卡在重新启动程序了吗?
我一开始就导入了
turtle
作为游戏。
if (scoreboard_a > 4):
win.write("Player A WINS!", font=textfont2)
game.reset()
elif (scoreboard_b > 4):
win.write("Player B WINS!", font=textfont2)
game.reset()
您可以使用
while
循环。将所有代码(可能不是导入)放入while
循环中:
import turtle
from time import sleep
while True:
# All your game code here
scoreboard_a = 0
scoreboard_b = 0
if (scoreboard_a > 4):
win.write("Player A WINS!", font=textfont2)
elif (scoreboard_b > 4):
win.write("Player B WINS!", font=textfont2)
sleep(2)
如果您的游戏已经处于
while
循环中,请在游戏结束时使用 break
:
import turtle
from time import sleep
while True:
# All your game code here
scoreboard_a = 0
scoreboard_b = 0
while True:
# All your game code here
if (scoreboard_a > 4):
win.write("Player A WINS!", font=textfont2)
sleep(2)
break
elif (scoreboard_b > 4):
win.write("Player B WINS!", font=textfont2)
sleep(2)
break
您可以像这样重新启动整个程序:
import os
import sys
import time
time.sleep(2)
os.execl(sys.executable, sys.executable, *sys.argv)
或者,您可以重置分数和其他元素,例如:
time.sleep(2)
# reset variables