Python+Turtle动态绘制一棵树实例分享
2018-01-16 14:28
941 查看
本文实例主要是对turtle的使用,实现Python+turtle动态绘制一棵树的实例,具体代码:
# drawtree.py from turtle import Turtle, mainloop def tree(plist, l, a, f): """ plist is list of pens l is length of branch a is half of the angle between 2 branches f is factor by which branch is shortened from level to level.""" if l > 5: # lst = [] for p in plist: p.forward(l)#沿着当前的方向画画Move the turtle forward by the specified distance, in the direction the turtle is headed. q = p.clone()#Create and return a clone of the turtle with same position, heading and turtle properties. p.left(a) #Turn turtle left by angle units q.right(a)# turn turtle right by angle units, nits are by default degrees, but can be set via the degrees() and radians() functions. lst.append(p)#将元素增加到列表的最后 lst.append(q) tree(lst, l*f, a, f) def main(): p = Turtle() p.color("green") p.pensize(5) #p.setundobuffer(None) p.hideturtle() #Make the turtle invisible. It's a good idea to do this while you're in the middle of doing some complex drawing, #because hiding the turtle speeds up the drawing observably. #p.speed(10) # p.getscreen().tracer(1,0)#Return the TurtleScreen object the turtle is drawing on. p.speed(10) #TurtleScreen methods can then be called for that object. p.left(90)# Turn turtle left by angle units. direction 调整画笔 p.penup() #Pull the pen up – no drawing when moving. p.goto(0,-200)#Move turtle to an absolute position. If the pen is down, draw line. Do not change the turtle's orientation. p.pendown()# Pull the pen down – drawing when moving. 这三条语句是一个组合相当于先把笔收起来再移动到指定位置,再把笔放下开始画 #否则turtle一移动就会自动的把线画出来 #t = tree([p]实现效果:, 200, 65, 0.6375) t = tree([p], 200, 65, 0.6375) main()
总结
以上就是本文关于Python+Turtle动态绘制一棵树实例分享的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!
您可能感兴趣的文章:
相关文章推荐
- python之matplotlib学习绘制动态更新图实例代码
- Python 绘图—使用Turtle库绘制动态钟表
- 【Python基础】1.4.3 Turtle库绘制多边形实例
- python+matplotlib实现动态绘制图片实例代码(交互式绘图)
- python 类对象和实例对象动态添加方法(分享)
- Python中动态创建类实例
- Python中pygal绘制雷达图代码分享
- Python中动态创建类实例
- python连接mysql实例分享
- python+matplotlib绘制饼图散点图实例代码
- python读取文本绘制动态速度曲线
- python使用turtle库与random库绘制雪花
- python+pillow绘制矩阵盖尔圆简单实例
- python动态加载变量示例分享
- 一则通过Android Canvas动态绘制图形的实例介绍
- 准备工作-用python绘制金融数据曲线的进阶实例
- Python的Socket编程过程中实现UDP端口复用的实例分享
- Python爬虫DOTA排行榜爬取实例(分享)
- python matplotlib画图实例代码分享
- Python turtle库绘制七段数码管