使用乌龟制作动画

using turtle to make animation

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

(我的圆圈里有200个同心圆shell,圆的半径随时间变化,用我自己导入的数据集,每个shell的半径按比例变化,每个shell 具有略微不同的蓝色,但根据我的数据集,每种颜色也会随时间变化。)

这是我生成的代码。

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 循环执行完成后只显示最终图形吗?这样demo就可以和我的gif一样了(没有画图过程显示)

提前致谢!!

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

您也可以使用 Python 的 sleep() 功能,在每一帧之间暂停一会儿。

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