python编程:读取文件动态绘制图形-6

125 阅读1分钟

python编程:读取文件动态绘制图形-6
\

\

源码:

  1. #根据数据文件在窗口中动态路径绘制
  2. import turtle 
  3.  
  4. def main():
  5.     #设置窗口信息
  6.     turtle.title('数据驱动的动态路径绘制')
  7.     turtle.setup(800, 600, 0, 0)
  8.     #设置画笔
  9.     pen = turtle.Turtle()
  10.     pen.color("red")
  11.     pen.width(5)
  12.     pen.shape("turtle")
  13.     pen.speed(5)
  14.     #读取文件
  15.     result=[]
  16.     file = open("data.txt","r")
  17.     for line in file:
  18.         result.append(list(map(float, line.split(','))))
  19.     print(result)
  20.     #动态绘制
  21.     for i in range(len(result)):
  22.         pen.color((result[i][3],result[i][4],result[i][5]))
  23.         pen.forward(result[i][0])
  24.         if result[i][1]:
  25.             pen.rt(result[i][2])
  26.         else:
  27.             pen.lt(result[i][2])
  28.     pen.goto(0,0)
  29.  
  30.  
  31. if __name__ == '__main__':
  32.     main()

\

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

\

\

原文地址: www.icourse163.org/learn/BIT-2…

\