自动轨迹绘制
需求:1、读取文件
2、解析数据
3、绘制图形
文本data.txt的内容:
300,0,144,1,0,0
300,0,144,0,1,0
300,0,144,0,0,1
300,0,144,1,1,0
300,0,108,0,1,1
184,0,72,1,0,1
184,0,72,0,0,0
184,0,72,0,0,0
184,0,72,0,0,0
184,1,72,1,0,1
184,1,72,0,0,0
184,1,72,0,0,0
184,1,72,0,0,0
184,1,72,0,0,0
184,1,720,0,0,0
第一个数据表示绘制的长度
第二个数据0表示向左转,1表示向右转
第三个数据表示(左转或右转)所转动的角度
最后三个数据表示绘制的颜色
import turtle as t
t.title('自动轨迹绘制')
t.setup(800, 600, 0, 0)
t.pencolor("red")
t.pensize(5)
# 读取数据
datals = []
f = open('data.txt')
for line in f:
line = line.replace('\n', "")
datals.append(list(map(eval, line.split(","))))
f.close()
# 自动绘制
for i in range(len(datals)):
t.pencolor(datals, datals, datals)
t.fd(datals)
if datals:
t.right(datals)
else:
t.left(datals)
t.done()
页:
[1]