jeudi 25 mars 2021

Re-Running a turtle program

I have made a simple python program that creates randomly generated spirals using the turtle module. I am still pretty new at python and i was wondering how i could restart the program after the turtle is pretty far away from the center and the spiral is done. Here is my code:

import turtle
import random

root = turtle.Screen()
turtle = turtle.Turtle()

colors = ["yellow", "gold", "orange", "red", "maroon", "violet", "magenta", "purple", "navy", "blue", "skyblue", "cyan", "turquoise", "lightgreen", "green", "darkgreen", "chocolate", "brown", "gray", "white"]

def spiral():
    endColors = [random.choice(colors), random.choice(colors), random.choice(colors)]
    angle = random.randint(60, 300)
    distance = turtle.distance(0, 0)
    
    print("Colors used are:", endColors)
    print("The turning angle is: ", angle, "deg")
    print("The distance is: ", distance)

    turtle.speed(0)
    turtle.hideturtle()
    root.bgcolor("black")
    for i in range(2000):
        turtle.forward(i)
        turtle.right(angle)
        turtle.color(endColors[i%3])
    root.mainloop()
spiral()

I have tried using the turtle.distance function in an if statement but it dident work.




Aucun commentaire:

Enregistrer un commentaire