在 Python 3.4.3 中移动多个 Turtle 来制作 "Tusi Couple"?
Moving multiple Turtles in Python 3.4.3 to make a "Tusi Couple"?
有没有办法让 multiple
海龟在 same time
处移动? (或者至少看起来是这样)
我正在尝试使用 Python 3.4.3
中的 Turtle 制作一个 "tusi couple"
(如果您不知道它是什么,请查找 GIF
)。但是我不太确定如何让多只海龟在 same time.
处移动 smoothly
我已经尝试过让我的海龟移动 in turn
几乎所有我能想到的方法,甚至尝试合并这些子程序中的几个,但我尝试的一切都妥协了 smoothness
或speed
,否则无效
这是我开始使用的initial code
:
import turtle
def dot1():
p1, p2 = turtle.Turtle(), turtle.Turtle()
p1.goto(-300,0) #p = point
p2.goto(300,0)
c1, c2 = p1.position(), p2.position() #c = coordinate
dot = turtle.Turtle()
p1.penup()
p2.penup()
p1.shape("circle")
p2.shape("circle")
dot.shape("circle")
dot.turtlesize(3)
dot.penup()
dot.hideturtle()
dot.goto(c1)
dot.showturtle()
dot.speed(0)
dot.setheading(dot.towards(c2)) #Towards second dot
while dot.distance(c2) > 6:
if dot.distance(c1) <300:
dot.fd((dot.distance(c1)+0.1)/30)
else:
dot.fd(dot.distance(c2)/30)
dot.setheading(dot.towards(c1)) #Towards first dot
while dot.distance(c1) > 6:
if dot.distance(c1) >300:
dot.fd((dot.distance(c2)+0.1)/30)
else:
dot.fd(dot.distance(c1)/30)
dot1()
从这里你可以看出我想把这个子程序的multiple copies
变成运行但是at different angles
。这些乌龟的minimum being 3
。
总的来说,我对 python 还很陌生,所以如果这是一个需要解决的简单问题,请给我一个 tip
我应该研究的问题,如果解决方案是simple/comprehensible 那我不想 entire thing
为我完成。
这绝对是一个有趣的挑战,直到我意识到我对如何完成这项工作一无所知。
感谢您的帮助。
编辑:最好看到这个 GIF:http://intothecontinuum.tumblr.com/post/57654628209/each-of-the-white-circles-are-really-just-moving
Python 乌龟在动画方面公认不是速度恶魔。获得任何速度的诀窍是花时间了解它是如何工作的,并要求它尽可能少地做。下面是我对你的 'each-of-the-white-circles-are-really-just-moving' 动画 GIF 图片的实现:
from math import pi, cos
from itertools import cycle
from turtle import Screen, Turtle
CIRCLES = 8
DIAMETER = 300
RADIUS = DIAMETER / 2
CURSOR_SIZE = 20
screen = Screen()
screen.tracer(False)
turtle = Turtle(visible=False)
turtle.dot(DIAMETER + CURSOR_SIZE)
turtles = []
for n in range(CIRCLES):
angle = n * pi / CIRCLES
circle = Turtle('circle')
circle.radians()
circle.color('red')
circle.penup()
circle.setheading(angle) # this stays fixed
# stash a couple of our values with the turtle
circle.angle = angle # this will change
circle.sign = 1 if cos(angle) > 0 else -1
turtles.append(circle)
circles = cycle(turtles)
while True: # really should use timer event but we're going for maximum speed!
circle = next(circles)
cosine = cos(circle.angle)
circle.forward(cosine * RADIUS - circle.sign * circle.distance(0, 0))
# update the values we stashed with the turtle
circle.sign = 1 if cosine > 0 else -1
circle.angle -= 0.1
screen.update()
screen.tracer(True)
screen.mainloop()
如果您想查看圆圈前后移动的线条,请在 turtles = []
行之前添加以下代码:
turtle.radians()
turtle.color('white')
for n in range(CIRCLES):
turtle.setheading(n * pi / CIRCLES)
turtle.forward(RADIUS)
turtle.backward(DIAMETER)
turtle.forward(RADIUS)
有没有办法让 multiple
海龟在 same time
处移动? (或者至少看起来是这样)
我正在尝试使用 Python 3.4.3
中的 Turtle 制作一个 "tusi couple"
(如果您不知道它是什么,请查找 GIF
)。但是我不太确定如何让多只海龟在 same time.
smoothly
我已经尝试过让我的海龟移动 in turn
几乎所有我能想到的方法,甚至尝试合并这些子程序中的几个,但我尝试的一切都妥协了 smoothness
或speed
,否则无效
这是我开始使用的initial code
:
import turtle
def dot1():
p1, p2 = turtle.Turtle(), turtle.Turtle()
p1.goto(-300,0) #p = point
p2.goto(300,0)
c1, c2 = p1.position(), p2.position() #c = coordinate
dot = turtle.Turtle()
p1.penup()
p2.penup()
p1.shape("circle")
p2.shape("circle")
dot.shape("circle")
dot.turtlesize(3)
dot.penup()
dot.hideturtle()
dot.goto(c1)
dot.showturtle()
dot.speed(0)
dot.setheading(dot.towards(c2)) #Towards second dot
while dot.distance(c2) > 6:
if dot.distance(c1) <300:
dot.fd((dot.distance(c1)+0.1)/30)
else:
dot.fd(dot.distance(c2)/30)
dot.setheading(dot.towards(c1)) #Towards first dot
while dot.distance(c1) > 6:
if dot.distance(c1) >300:
dot.fd((dot.distance(c2)+0.1)/30)
else:
dot.fd(dot.distance(c1)/30)
dot1()
从这里你可以看出我想把这个子程序的multiple copies
变成运行但是at different angles
。这些乌龟的minimum being 3
。
总的来说,我对 python 还很陌生,所以如果这是一个需要解决的简单问题,请给我一个 tip
我应该研究的问题,如果解决方案是simple/comprehensible 那我不想 entire thing
为我完成。
这绝对是一个有趣的挑战,直到我意识到我对如何完成这项工作一无所知。
感谢您的帮助。
编辑:最好看到这个 GIF:http://intothecontinuum.tumblr.com/post/57654628209/each-of-the-white-circles-are-really-just-moving
Python 乌龟在动画方面公认不是速度恶魔。获得任何速度的诀窍是花时间了解它是如何工作的,并要求它尽可能少地做。下面是我对你的 'each-of-the-white-circles-are-really-just-moving' 动画 GIF 图片的实现:
from math import pi, cos
from itertools import cycle
from turtle import Screen, Turtle
CIRCLES = 8
DIAMETER = 300
RADIUS = DIAMETER / 2
CURSOR_SIZE = 20
screen = Screen()
screen.tracer(False)
turtle = Turtle(visible=False)
turtle.dot(DIAMETER + CURSOR_SIZE)
turtles = []
for n in range(CIRCLES):
angle = n * pi / CIRCLES
circle = Turtle('circle')
circle.radians()
circle.color('red')
circle.penup()
circle.setheading(angle) # this stays fixed
# stash a couple of our values with the turtle
circle.angle = angle # this will change
circle.sign = 1 if cos(angle) > 0 else -1
turtles.append(circle)
circles = cycle(turtles)
while True: # really should use timer event but we're going for maximum speed!
circle = next(circles)
cosine = cos(circle.angle)
circle.forward(cosine * RADIUS - circle.sign * circle.distance(0, 0))
# update the values we stashed with the turtle
circle.sign = 1 if cosine > 0 else -1
circle.angle -= 0.1
screen.update()
screen.tracer(True)
screen.mainloop()
如果您想查看圆圈前后移动的线条,请在 turtles = []
行之前添加以下代码:
turtle.radians()
turtle.color('white')
for n in range(CIRCLES):
turtle.setheading(n * pi / CIRCLES)
turtle.forward(RADIUS)
turtle.backward(DIAMETER)
turtle.forward(RADIUS)