【问题标题】:How to sync animation using turtle-graphics module?如何使用海龟图形模块同步动画?
【发布时间】:2017-08-30 16:40:11
【问题描述】:

我编写了以下 Python 代码来使用 Python Turtle 创建旋转图的动画。我面临的问题是,动画没有正确同步。我不想向人们展示事情是如何旋转的。所以,我必须仔细选择turtle.tracer 命令中的帧速率。好吧,如果你仔细观察,r 的每个值都会创建一个旋转,所以我必须在每个r 的末尾更新屏幕。对于r 的每个值,r 的循环内有 1202 次迭代。但这并没有产生预期的效果。

我必须做些什么来纠正这个问题?

import turtle
import math

am = turtle
am.ht()
am.tracer(1202,0)

for r in range(0,600):
    #axes
    am.pu()
    am.setpos(0,500)
    am.pd()
    am.setpos(0,-500)
    am.pu()
    am.setpos(-650,0)
    am.pd()
    am.setpos(0,0)
    am.write("0",align="right",font=("Times New Roman",14,"normal"))
    am.setpos(650,0)
    am.pu()
    am.setpos(-300*math.cos(r*math.pi/100),300*math.sin(r*math.pi/100))
    am.pd()
    #axes
    am.pencolor("red")
    for x in range(-300,301):
        g=math.sin(x)
        t=math.cos(x)
        y =100*g*t*math.sin(2*(x**2)*math.pi/100)
        am.setpos(x*math.cos(r*math.pi/100)+y*math.sin(r*math.pi/100),-x*math.sin(r*math.pi/100)+y*math.cos(r*math.pi/100))
    #if(x%4==0):
       #am.write(x)
    am.pu()
    am.setpos(-300*math.sin(r*math.pi/100),-300*math.cos(r*math.pi/100))
    am.pd()
    am.pencolor("blue")
    for y in range(-300,301):
        c=math.sin(y)
        d=math.cos(y)
        x =100*c*d*math.cos(2*(y**2)*math.pi/100)
        am.setpos(x*math.cos(r*math.pi/100)+y*math.sin(r*math.pi/100),-x*math.sin(r*math.pi/100)+y*math.cos(r*math.pi/100))
    am.reset()

am.exitonclick()

【问题讨论】:

  • 你想精确同步什么?如果您不想准确显示事物是如何旋转的,那么您希望显示什么? Turtle-graphics 是一种绘制动画帧的相当慢的方法,绘制图形的每个“帧”需要几秒钟,因此使用它制作真正的动画即使不是不可能也很困难。文档中提到的“动画”与绘制线条和移动海龟的速度有关(这些都非常慢,即使设置为允许的最快速度也是如此)。

标签: python python-3.x turtle-graphics


【解决方案1】:

我相信以下内容会如您所愿。当您准备好向用户展示某些内容时,我已将跟踪器逻辑更改为更加手动,并使用明确的.update()。我还将轴绘图与主循环分开,因为我们真的不需要每次都清除和重绘它:

import math
from turtle import Turtle, Screen

screen = Screen()
screen.tracer(0)

# axes
axes = Turtle(visible=False)
axes.pu()
axes.setpos(0, 500)
axes.pd()
axes.setpos(0, -500)
axes.pu()
axes.setpos(-650, 0)
axes.pd()
axes.setpos(0, 0)
axes.write("0", align="right", font=("Times New Roman", 14, "normal"))
axes.setpos(650, 0)

am = Turtle(visible=False)

for r in range(0, 600):

    am.pu()
    am.setpos(-300 * math.cos(r * math.pi / 100), 300 * math.sin(r * math.pi / 100))
    am.pd()
    am.pencolor("red")

    for x in range(-300, 301):
        g = math.sin(x)
        t = math.cos(x)
        y = 100 * g * t * math.sin(2 * x**2 * math.pi / 100)
        am.setpos(x * math.cos(r * math.pi / 100) + y * math.sin(r * math.pi / 100), -x * math.sin(r * math.pi / 100) + y * math.cos(r * math.pi / 100))

    am.pu()
    am.setpos(-300 * math.sin(r * math.pi / 100), -300 * math.cos(r * math.pi / 100))
    am.pd()
    am.pencolor("blue")

    for y in range(-300, 301):
        c = math.sin(y)
        d = math.cos(y)
        x = 100 * c * d * math.cos(2 * y**2 * math.pi / 100)
        am.setpos(x * math.cos(r * math.pi / 100) + y * math.sin(r * math.pi / 100), -x * math.sin(r * math.pi / 100) + y * math.cos(r * math.pi / 100))

    screen.update()
    am.reset()
    am.hideturtle()  # made visible by reset()

screen.exitonclick()

最后,这很可能是一个错误:

am = turtle

并没有做你认为的那样(模块名称别名而不是实际的海龟。)

【讨论】:

    猜你喜欢
    • 2017-08-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多