【问题标题】:using turtle to make animation使用海龟制作动画
【发布时间】:2020-07-06 21:15:33
【问题描述】:

我正在python中使用turtle包模拟动画。 我想要达到的效果就像我在这里附上的.gif链接:https://i.stack.imgur.com/prCyQ.gif

(我的圈子里有200个同心壳。圆的半径随着我自己导入的数据集而变化,每个壳的半径按比例变化,每个壳的蓝色略有不同,但每个壳也会随着时间的推移而变化我的数据集。)

这是我生成的代码。

import csv
from turtle import *

def colorConvert(digit:float):
    if digit == 10:
        digit = 'A'
    elif digit == 11:
        digit = 'B'
    elif digit == 12:
        digit = 'C'
    elif digit == 13:
        digit = 'D'
    elif digit == 14:
    digit = 'E'
    elif digit == 15:
        digit = 'F'
    return digit

bgcolor("black")

radius=[]
with open('D:/SURA/Archive/water-loss/output-sorption.csv') as loss_radius:
    reader = csv.reader(loss_radius,delimiter=',')
    for row in reader:
        radius.append(float(row[1]))
conc = []                    # convert to float when accessing elements
with open('D:/SURA/Archive/output-water-concentration.csv') as water:
    reader = csv.reader(water, delimiter=',')
    conc = [list(map(float,row)) for row in reader]

for i in range(301):
    for j in range(200):
        conc[i][j] -= 140

shell_radius=[[0]*200]*301
max=200
radius_max=radius[0]
for i in range(0,301,30):
    for j in range(0,200,20):
        radius_max = radius[i]
        shell_radius[i][j] = radius_max * ((max - j) / max)
        digit5 = int(float(conc[i][j]) // 16)
        digit6 = int(((float(conc[i][j]) / 16) - int(float(conc[i][j]) / 16)) * 16)
        color_set = '#0000'+str(colorConvert(digit5))+str(colorConvert(digit6))
        up()
        goto(0,-shell_radius[i][j]*0.05)
        down()
        pencolor(color_set)    
        fillcolor(color_set)
        begin_fill()
        circle(shell_radius[i][j]*0.05)
        end_fill()

exitonclick()

(最后嵌套的for应该从0到301和0到200,我缩短它们以节省时间以显示最终的视觉效果~)

它可以工作,但我要优化的是,在一个图形外部 for 循环执行完成后,图形可能会消失,并开始下一个 out for 循环。此外,有没有办法只在每个外部 for 循环执行完成后显示最终图表?这样demo就可以和我的gif一样了(没有绘制过程显示)。

提前致谢!!

【问题讨论】:

    标签: python python-turtle


    【解决方案1】:

    您可以在绘制时更改海龟的速度 (turtle speed),使动画在每一帧结束时变慢。

    您还可以在每一帧之间使用 Python 的 sleep() 函数暂停一段时间。

    我猜你可以以最快的速度即时绘制,并使用tracer(False)hideturtle() 隐藏它。

    【讨论】:

    • 你能解释一下在哪里使用示踪剂(假)吗?我在每个 out for 循环的末尾都尝试过,但它似乎不起作用。谢谢!
    • 在答案中,有一个很好的解释。如果你没有从海龟进口*,那么你必须使用turtle.tracer(False)。 stackoverflow.com/questions/19619858/…
    猜你喜欢
    • 1970-01-01
    • 2017-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多