Python+Turtle动态绘制一棵树实例分享


Posted in Python onJanuary 16, 2018

本文实例主要是对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+Turtle动态绘制一棵树实例分享的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

Python 相关文章推荐
python选择排序算法的实现代码
Nov 21 Python
对于Python异常处理慎用“except:pass”建议
Apr 02 Python
利用Python为iOS10生成图标和截屏
Sep 24 Python
基于Python的关键字监控及告警
Jul 06 Python
PyQt5每天必学之QSplitter实现窗口分隔
Apr 19 Python
python实时监控cpu小工具
Jun 21 Python
Python使用pandas对数据进行差分运算的方法
Dec 22 Python
python字典setdefault方法和get方法使用实例
Dec 25 Python
tensorflow转换ckpt为savermodel模型的实现
May 25 Python
在python中使用pyspark读写Hive数据操作
Jun 06 Python
python中的django是做什么的
Jul 31 Python
python 获取计算机的网卡信息
Feb 18 Python
Python实现七彩蟒蛇绘制实例代码
Jan 16 #Python
答题辅助python代码实现
Jan 16 #Python
python实现百万答题自动百度搜索答案
Jan 16 #Python
Python数据结构之双向链表的定义与使用方法示例
Jan 16 #Python
python+pillow绘制矩阵盖尔圆简单实例
Jan 16 #Python
Python面向对象编程之继承与多态详解
Jan 16 #Python
Python基于socket实现简单的即时通讯功能示例
Jan 16 #Python
You might like
一拳超人中怪人协会钦定! S级别最强四人!
2020/03/02 日漫
几个学习PHP的网址
2006/11/25 PHP
php计算数组不为空元素个数的方法
2014/01/27 PHP
php检查函数必传参数是否存在的实例详解
2017/08/28 PHP
php简单计算权重的方法示例【适合抽奖类应用】
2019/06/10 PHP
详解PHP中curl_multi并发的实现
2020/06/08 PHP
mouse_on_title.js
2006/08/25 Javascript
JS的replace方法详细介绍
2012/11/09 Javascript
Ajax提交与传统表单提交的区别说明
2014/02/07 Javascript
jQuery标签编辑插件Tagit使用指南
2015/04/21 Javascript
基于jQuery实现多层次的手风琴效果附源码
2015/09/21 Javascript
简单理解vue中el、template、replace元素
2016/10/27 Javascript
JavaScript 数组的深度复制解析
2016/11/02 Javascript
webpack学习--webpack经典7分钟入门教程
2017/06/28 Javascript
解决iView中时间控件选择的时间总是少一天的问题
2018/03/15 Javascript
vue-cli开发环境实现跨域请求的方法
2018/04/07 Javascript
highCharts提示框中显示当前时间的方法
2019/01/18 Javascript
详解vue 图片上传功能
2019/04/30 Javascript
详解小程序input框失焦事件在提交事件前的处理
2019/05/05 Javascript
微信小程序获取地理位置及经纬度授权代码实例
2019/09/18 Javascript
jQuery实现颜色打字机的完整代码
2020/03/19 jQuery
Node.JS如何实现JWT原理
2020/09/18 Javascript
Python和perl实现批量对目录下电子书文件重命名的代码分享
2014/11/21 Python
Python爬豆瓣电影实例
2018/02/23 Python
详解python while 函数及while和for的区别
2018/09/07 Python
django 实现编写控制登录和访问权限控制的中间件方法
2019/01/15 Python
使用django的objects.filter()方法匹配多个关键字的方法
2019/07/18 Python
在tensorflow中设置保存checkpoint的最大数量实例
2020/01/21 Python
使用Python实现Wake On Lan远程开机功能
2020/01/22 Python
Numpy 多维数据数组的实现
2020/06/18 Python
Myprotein法国官网:欧洲第一运动营养品牌
2019/03/26 全球购物
执行力心得体会
2013/12/31 职场文书
扩大国家免疫规划实施方案
2014/03/21 职场文书
小兵张嘎观后感300字
2015/06/03 职场文书
第一书记观后感
2015/06/08 职场文书
2016年安康杯竞赛活动总结
2016/04/05 职场文书