首页 > 技术文章 > python绘制动态图形代码

ylzchs 2020-06-30 09:06 原文

import turtle as t
def main():
#设置窗口信息
t.title("数据驱动的动态路径绘制")#建立标题
t.setup(800,600,0,0)#设置大小为800*600的窗口,python坐标原点默认设置在窗口中心
#设置画笔
pen=t.Turtle()#通过turtle()建立画笔pen,其默认位置是窗口原点
pen.color("red")#设置画笔颜色为红色
pen.width(5)#设置画笔宽度为5
pen.shape("turtle")#设置画笔形状为turtle
pen.speed(1)#设置绘画速度为5
#读取数据文件到列表result[]中
result=[]
file=open("data.txt","r")
for line in file:
result.append(list(map(float,line.split(','))))
#将数据文件的每行数据都存放在result[]中
print(result)
#动态绘制
for i in range(len(result)):
pen.color((result[i][3],result[i][4],result[i][5]))
#每一行的第4-6位来定义画笔颜色
pen.fd(result[i][0])#向前爬行的距离
#判断语句如为1则为真,0则为假,如果该行的第二个数据是0则画笔向左,如为1画笔向右
if result[i][1]:
pen.rt(result[i][2])
#小乌龟向右爬行并旋转角度为第三个数据
else:
pen.lt(result[i][2])
pen.goto(0,0)#画笔回到原点
main()

 


推荐阅读