在python3.4.3中移动多个海龟以形成“Tusi夫妇”?

2024-04-18 13:39:18 发布

您现在位置:Python中文网/ 问答频道 /正文

有没有办法让multiple海龟在same time移动?(至少看起来是这样)

我正试图在Python 3.4.3中使用Turtle制作一个"tusi couple"(如果您不知道它是什么,请查找GIF)。不过,我不太确定如何让多只海龟在same time.处移动smoothly

我已经试过让我的乌龟移动了in turn,而且几乎用我能想到的所有方法,甚至试过合并几个子例程,但是我试过的每件事都会妥协smoothnessspeed,或者它不起作用

这是我开始使用的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为我完成。你知道吗

这绝对是一个有趣的挑战,直到我意识到我不知道如何使这项工作。你知道吗

谢谢你的帮助。你知道吗

编辑:如果你看到这个GIF最好:http://intothecontinuum.tumblr.com/post/57654628209/each-of-the-white-circles-are-really-just-moving


Tags: multipledotdistance海龟p2c2shapeturtle
1条回答
网友
1楼 · 发布于 2024-04-18 13:39:18

Python龟是令人钦佩的没有速度恶魔当谈到动画。要想从中获得任何速度,关键是花时间去理解它是如何工作的,并要求它尽可能少地做。下面是我实现的“每个白圈都在移动”的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()

enter image description here

如果要查看圆在哪些行上来回移动,请在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)

相关问题 更多 >