有没有办法让multiple乌龟在same time上移动?(至少看起来是这样)
我正在尝试用海龟在Python 3.4.3中做一个"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,如果解决方案很简单/容易理解,那么我不想为我做entire thing。
这绝对是一个有趣的挑战,直到我意识到我对如何工作一无所知。
谢谢你的帮助。
编辑:最好,如果你看到这个图片:http://intothecontinuum.tumblr.com/post/57654628209/each-of-the-white-circles-are-really-just-moving
发布于 2019-01-20 15:42:07
无可否认,在动画方面,巨蟒乌龟并不是速度上的恶魔。获得任何速度的诀窍是花时间理解它是如何工作的,并要求它做的尽可能少。下面是我实现的'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)https://stackoverflow.com/questions/54270876
复制相似问题