繁体   English   中英

重新运行一个海龟程序

[英]Re-Running a turtle program

我制作了一个简单的 python 程序,该程序使用海龟模块创建随机生成的螺旋。 我在 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 function 但它不起作用。

您可以通过使用全局标志变量并安装一个调用 function 的海龟计时器来做到这一点,该计时器通过查看变量定期检查螺旋是否已完成绘制 - 如果是,则重置并绘制另一个。 在名为check_status()的 function 下面的代码中。 我还在每个完成后添加了一个短暂的停顿,以使其保持可见足够长的时间以供欣赏。 ;¬)

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM