Reputation: 13
Does anyone know how to restart the program like, for example, when a player reaches 5 points, after the text "Player a Wins" it will give a certain amount of time delay and then directly restart the game from 0 0. i created the score thingy but got stuck on restarting the program once it reaches 5 points?
I have imported turtle
as game in the beginning.
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()
Upvotes: 1
Views: 1382
Reputation: 27577
You can use a while
loop. Put all your code, expect maybe not the imports, into a while
loop:
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)
If your game is already in a while
loop, use break
when the game ends:
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
Upvotes: 2
Reputation: 142
You can restart the whole program like this:
import os
import sys
import time
time.sleep(2)
os.execl(sys.executable, sys.executable, *sys.argv)
Or, you can reset the score(s) and other elements, like:
time.sleep(2)
# reset variables
Upvotes: 0