【发布时间】:2021-06-22 13:25:42
【问题描述】:
我制作了一个简单的 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 函数,但它不起作用。
【问题讨论】:
标签: python random turtle-graphics python-turtle