再运行一个海龟程序

Re-Running a turtle program

我制作了一个简单的 python 程序,它使用 turtle 模块创建随机生成的螺旋。我在 python 还是个新手,我想知道在乌龟离中心很远并且螺旋完成后如何重新启动程序。这是我的代码:

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()

我试过在 if 语句中使用 turtle.distance 函数,但它没有用。

您可以通过使用一个全局标志变量并安装一个海龟 timer 来实现这一点,该海龟调用一个函数,该函数通过查看变量定期检查螺旋线是否已完成绘制——如果是的话, 重置事物并绘制另一个。在下面的代码中,执行此操作的函数名为 check_status()。我还在每个完成之前添加了一个短暂的停顿,以使其保持足够长的可见时间以供欣赏。 ;¬)

import random
import time
import turtle

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"]

MAX_DIST = 100
PAUSE = 2  # Secs


def spiral():
    global spiral_done
    spiral_done = False  # Global flag.

    endColors = [random.choice(colors), random.choice(colors), random.choice(colors)]
    angle = random.randint(60, 300)

    print("Colors used are:", endColors)
    print("The turning angle is: ", angle, "deg")

    turtle.speed(0)
    turtle.hideturtle()
    root.bgcolor("black")
    for i in range(2000):
        turtle.forward(i)
        turtle.right(angle)
        turtle.color(endColors[i%3])
        if turtle.distance(0, 0) > MAX_DIST:  # Stop if far away.
            break

    time.sleep(PAUSE)
    spiral_done = True


def check_status():
    if spiral_done:
        root.reset()
        spiral()  # Draw another.
    root.ontimer(check_status, 250)  # Check every 1/4 second.


def main():
    global spiral_done
    spiral_done = True  # Initialize global flag.
    check_status()  # Start watching its value.
    spiral()
    root.mainloop()


if __name__ == '__main__':
    main()

我的解决方案与@martineau 类似,只是我使用 turtle.reset() 而不是 screen.reset() 和更简单的时序逻辑:

from turtle import Screen, Turtle
from random import choice, randint
from itertools import count

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

RADIUS = 100

def spiral():
    turtle.hideturtle()
    turtle.speed('fastest')

    triColors = [choice(COLORS), choice(COLORS), choice(COLORS)]
    angle = randint(60, 300)

    print("Colors used are:", triColors)
    print("The turning angle is: ", angle, "deg")
    print("The radius is: ", RADIUS)

    for distance in count():  # spiral forever until a specific radius is achieved
        turtle.pencolor(triColors[distance % 3])
        turtle.forward(distance)

        if turtle.distance(0, 0) > RADIUS:
            break

        turtle.right(angle)

    turtle.reset()
    screen.ontimer(spiral)

screen = Screen()
screen.bgcolor('black')

turtle = Turtle()

spiral()

screen.mainloop()