Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can I loop back to the beginning, when my player hit one of the white squares? I have tried to loop, but I don’t know where I can set the while loop, to restart the game from Scratch. Loop back, and start to move from the beginning. In other words, restart the game.

import turtle
import math

wn = turtle.Screen()
wn.bgcolor("blue")
wn.tracer(3)

player = turtle.Turtle()
player.color("red")
player.shape("circle")
player.penup()
player.speed(0)
player.setposition(-225, 50)

square = turtle.Turtle()
square.color("white")
square.shape("square")
square.penup()
square.speed(0)
square.setposition(-200, 100)

square_2 = turtle.Turtle()
square_2.color("white")
square_2.shape("square")
square_2.penup()
square_2.speed(0)
square_2.setposition(-250, 50)

speed = 0.4

def turnleft():
    player.left(90)

def turnright():
    player.right(90)

turtle.listen()
turtle.onkey(turnleft, "Left")
turtle.onkey(turnright, "Right")

while True:
    player.forward(speed)

    d = math.sqrt(math.pow(player.xcor()-square.xcor(),2) + math.pow(player.ycor()-square.ycor(),2))
    if d < 20:
        square.q()

    d = math.sqrt(math.pow(player.xcor()-square_2.xcor(),2) + math.pow(player.ycor()-square_2.ycor(),2))
    if d < 20:
       square_2.q()


What I have tried:

I have tried to set the code into a loop. But I didn’t figure out, how to loop from the beginning of the code.
Posted
Comments
The Other John Ingram 16-Apr-21 19:18pm    
while (some condition)
<your code="">

or something like this look up the while statement
Alexander Fagerthun 2021 16-Apr-21 19:33pm    
Its while True:
<my code="«»">

But it don’t work, to set the hole code inside it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900