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

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

源码:

#根据数据文件在窗口中动态路径绘制
import turtle 
 
def main():
    #设置窗口信息
    turtle.title('数据驱动的动态路径绘制')
    turtle.setup(800, 600, 0, 0)
    #设置画笔
    pen = turtle.Turtle()
    pen.color("red")
    pen.width(5)
    pen.shape("turtle")
    pen.speed(5)
    #读取文件
    result=[]
    file = open("data.txt","r")
    for line in file:
        result.append(list(map(float, line.split(','))))
    print(result)
    #动态绘制
    for i in range(len(result)):
        pen.color((result[i][3],result[i][4],result[i][5]))
        pen.forward(result[i][0])
        if result[i][1]:
            pen.rt(result[i][2])
        else:
            pen.lt(result[i][2])
    pen.goto(0,0)
 
 
if __name__ == '__main__':
    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



原文地址: http://www.icourse163.org/learn/BIT-268001?tid=1002001005#/learn/content?type=detail&id=1002613035&cid=1002856098

上一篇:python编程:文件操作


下一篇:python编程:合并文本_利用列表或字典将两个通讯录文本合并