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使用post提交数据到远程url的方法
Apr 29 Python
Python批量重命名同一文件夹下文件的方法
May 25 Python
PyCharm代码回滚,恢复历史版本的解决方法
Oct 22 Python
如何用python写一个简单的词法分析器
Dec 18 Python
利用Python实现微信找房机器人实例教程
Mar 10 Python
python爬虫 urllib模块url编码处理详解
Aug 20 Python
Python和Anaconda和Pycharm安装教程图文详解
Feb 04 Python
python 伯努利分布详解
Feb 25 Python
利用matplotlib为图片上添加触发事件进行交互
Apr 23 Python
解决python中显示图片的plt.imshow plt.show()内存泄漏问题
Apr 24 Python
python函数中将变量名转换成字符串实例
May 11 Python
requests在python中发送请求的实例讲解
Feb 17 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
php基础知识:类与对象(2) 自动加载对象
2006/12/13 PHP
组合算法的PHP解答方法
2012/02/04 PHP
PHP中feof()函数实例测试
2014/08/23 PHP
PHP实现动态压缩js与css文件的方法
2018/05/02 PHP
Javascript load Page,load css,load js实现代码
2010/03/31 Javascript
Js 时间间隔计算的函数(间隔天数)
2011/11/15 Javascript
Firefox/Chrome/Safari的中可直接使用$/$$函数进行调试
2012/02/13 Javascript
js实现通用的微信分享组件示例
2014/03/10 Javascript
javascript制作的网页侧边弹出框思路及实现代码
2014/05/21 Javascript
图片放大镜jquery.jqzoom.js使用实例附放大镜图标
2014/06/19 Javascript
javascript+HTML5的canvas实现七夕情人节3D玫瑰花效果代码
2015/08/04 Javascript
NodeJS连接MongoDB数据库时报错的快速解决方法
2016/05/13 NodeJs
AngularJS ng-repeat指令中使用track by子语句解决重复数据遍历错误问题
2017/01/21 Javascript
详解bootstrap-fileinput文件上传控件的亲身实践
2019/03/21 Javascript
js图片无缝滚动插件使用详解
2020/05/26 Javascript
[51:44]2018DOTA2亚洲邀请赛 4.3 突围赛 Optic vs iG 第二场
2018/04/04 DOTA
[57:41]Secret vs Serenity 2018国际邀请赛小组赛BO2 第一场 8.16
2018/08/17 DOTA
编写Python脚本批量下载DesktopNexus壁纸的教程
2015/05/06 Python
python中zip()方法应用实例分析
2016/04/16 Python
python多进程和多线程究竟谁更快(详解)
2017/05/29 Python
解决pandas无法在pycharm中使用plot()方法显示图像的问题
2018/05/24 Python
Python multiprocessing多进程原理与应用示例
2019/02/28 Python
Python+pyplot绘制带文本标注的柱状图方法
2019/07/08 Python
django 捕获异常和日志系统过程详解
2019/07/18 Python
使用Python3 poplib模块删除服务器多天前的邮件实现代码
2020/04/24 Python
Python 判断时间是否在时间区间内的实例
2020/05/16 Python
将不规则的Python多维数组拉平到一维的方法实现
2021/01/11 Python
彼得罗夫美国官网:Peter Thomas Roth美国(青瓜面膜)
2017/11/05 全球购物
美国玩具公司:U.S.Toy
2018/05/19 全球购物
COSETTE官网:奢华,每天
2020/03/22 全球购物
农民工工资发放承诺书
2014/03/31 职场文书
经贸日语专业个人求职信范文
2014/04/29 职场文书
中学教师暑期培训方案
2014/08/27 职场文书
法人代表授权委托书范文
2014/09/10 职场文书
Python绘制分类图的方法
2021/04/20 Python
OpenCV-Python 实现两张图片自动拼接成全景图
2021/06/11 Python