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中使用smtplib和email模块发送邮件实例
Apr 22 Python
python进阶教程之文本文件的读取和写入
Aug 29 Python
python3实现读取chrome浏览器cookie
Jun 19 Python
Python实现一个转存纯真IP数据库的脚本分享
May 21 Python
Python通过matplotlib绘制动画简单实例
Dec 13 Python
numpy实现合并多维矩阵、list的扩展方法
May 08 Python
为什么你还不懂得怎么使用Python协程
May 13 Python
Python如何将字符串转换为日期
Jul 31 Python
Pytest如何使用skip跳过执行测试
Aug 13 Python
使用PyCharm官方中文语言包汉化PyCharm
Nov 18 Python
PyTorch 如何检查模型梯度是否可导
Jun 05 Python
Python爬虫入门案例之爬取二手房源数据
Oct 16 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实现二分查找算法代码分享
2011/06/24 PHP
支持中文的php加密解密类代码
2011/11/27 PHP
深入HTTP响应状态码速查表的详解
2013/06/07 PHP
php实现快速排序的三种方法分享
2014/03/12 PHP
php获取随机数组列表的方法
2014/11/13 PHP
PHP随机获取未被微信屏蔽的域名(微信域名检测)
2017/03/19 PHP
Code:loadScript( )加载js的功能函数
2007/02/02 Javascript
js 返回时间戳所对应的具体时间
2010/07/20 Javascript
URL地址中的#符号使用说明
2011/02/12 Javascript
javascript获取作用在元素上面的样式属性代码
2012/09/20 Javascript
JavaScript 函数参数是传值(byVal)还是传址(byRef) 分享
2013/07/02 Javascript
javascript 手动给表增加数据的小例子
2013/07/10 Javascript
JS动态日期时间的获取方法
2015/09/28 Javascript
JS获取地址栏参数的两种方法(简单实用)
2016/06/14 Javascript
探究JavaScript中的五种事件处理程序方式
2016/12/07 Javascript
Node.js和Express简单入门介绍
2017/03/24 Javascript
vue+node+webpack环境搭建教程
2017/11/05 Javascript
了解前端理论:rscss和rsjs
2019/05/23 Javascript
简单了解常用的JavaScript 库
2020/07/16 Javascript
在vue中对数组值变化的监听与重新响应渲染操作
2020/07/17 Javascript
详解python的ORM中Pony用法
2018/02/09 Python
Python字典取键、值对的方法步骤
2020/09/30 Python
CSS3 旋转立方体问题详解
2020/01/09 HTML / CSS
利用HTML5+css3+jquery+weui实现仿微信聊天界面功能
2018/01/08 HTML / CSS
护理专业学生的求职信范文
2013/12/11 职场文书
办理护照介绍信
2014/01/16 职场文书
保护环境演讲稿
2014/05/10 职场文书
咖啡店创业计划书范文
2014/09/15 职场文书
大学生考试作弊检讨书
2014/09/21 职场文书
2014最新毕业证代领委托书
2014/09/26 职场文书
公司保洁员岗位职责
2015/02/13 职场文书
2015年高三班主任工作总结
2015/05/21 职场文书
2015年十月一日放假通知
2015/08/18 职场文书
执行力心得体会范文
2016/01/11 职场文书
Pytorch 使用tensor特定条件判断索引
2021/04/08 Python
Python中文分词库jieba(结巴分词)详细使用介绍
2022/04/07 Python