【发布时间】: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