添加更多武器
但是如果我们想要一些看起来有点不同的东西——比如螺旋星系或银河系呢?我们首先需要添加更多的武器。
为此,我们将创建多个Turtle
import turtle as t
t.tracer(10,1)
t1=t.Turtle()
t2=t.Turtle()
t1.setheading(0) # Looks to the right
t2.setheading(180) # Looks to the right
for x in range(360):
radius = x
angle = 1
t1.circle(radius,angle)
t2.circle(radius,angle)
t.update()
在上面的代码中,t1和t2是最初使用setheading命令分别设置为向右和向左看的两只Turtle

两只乌龟现在都开始朝着自己的方向发展。现在让我们看看如何使其更具可配置性
import turtle as t
t.tracer(10,1)
N = 10
angle = 1
turtles = []
for position in range(N):
look_at = 360/N*position
new = t.Turtle()
new.setheading(look_at)
turtles.append(new)
for radius in range(360):
for my in turtles:
my.circle(radius, angle)
t.update()
我们只是更改了代码,这样可以通过更改创建任意数量的Turtle N,并且它们都以对称的方式看向不同的方向。

有趣的是,旋臂似乎相交。我们可以通过进行一些调整来防止这种情况发生
import turtle as t
t.tracer(10,1)
N = 10
angle = 30
turtles = []
for position in range(N):
look_at = 360/N*position
new = t.Turtle()
new.setheading(look_at)
turtles.append(new)
for radius in range(360):
for my in turtles:
my.circle(radius*radius, angle)
t.update()
我们设置angle为30并平方radius
通过使每次迭代的转弯角度更大并增加螺旋增加的速率(通过平方半径 - radius*radius),我们可以防止螺旋相交。(请注意,这是我无意中发现的)

耶!