2017-08-30 73 views
1

我已经写了下面的python代码来创建使用python turtle的旋转图形的动画。我面临的问题是,动画不能正确同步。我不想让人们知道事情是如何旋转的。所以,我必须仔细选择turtle.tracer命令中的帧速率。那么,如果你仔细观察,每个值r创建一个旋转,所以我必须更新每个r结束时的屏幕。对于r的每个值,r的循环内有1202次迭代。但是这不会产生预期的效果。如何使用turtle-graphics模块同步动画?

我该怎么做才能纠正这个问题?

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() 
+0

你想要完全同步什么?如果你不想确切地显示事物的旋转方向,你想显示什么_do_? Turtle-graphics是绘制动画帧的一种相当慢的方式,需要花费几秒钟来绘制图形的每个“帧”,因此如果不是不可能的话,使用它生成真正的动画将很困难。文件中提到的“动画”与绘制线条和移动乌龟的速度有关(即使速度设置得尽可能快,速度都很慢)。 – martineau

回答

2

我相信下面会做你的愿望。当您准备向用户显示某些内容时,我已将示踪器逻辑更改为更明确的.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 

和不是做你认为的事(模块名称别名,而不是实际的乌龟)。