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中for循环详解
Jan 17 Python
Python管理Windows服务小脚本
Mar 12 Python
通过Pandas读取大文件的实例
Jun 07 Python
解决Mac安装scrapy失败的问题
Jun 13 Python
对python模块中多个类的用法详解
Jan 10 Python
python对文件目录的操作方法实例总结
Jun 24 Python
Python字符串大小写转换拼接删除空白
Sep 19 Python
Python装饰器用法与知识点小结
Mar 09 Python
在python中修改.properties文件的操作
Apr 08 Python
python删除某个目录文件夹的方法
May 26 Python
python 删除excel表格重复行,数据预处理操作
Jul 06 Python
Python开发.exe小工具的详细步骤
Jan 27 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 将excel导入mysql
2009/11/09 PHP
php中批量替换文件名的实现代码
2011/07/20 PHP
linux iconv方法的使用
2011/10/01 PHP
PHP图片自动裁切应付不同尺寸的显示
2014/10/16 PHP
php批量删除超链接的实现方法
2015/10/19 PHP
PHP实现多级分类生成树的方法示例
2017/02/07 PHP
PHP面向对象五大原则之开放-封闭原则(OCP)详解
2018/04/04 PHP
ThinkPHP框架使用redirect实现页面重定向的方法实例分析
2018/04/12 PHP
js原型链原理看图说明
2012/07/07 Javascript
js多级树形弹出一个小窗口层(非常好用)实例代码
2013/03/19 Javascript
javascript中with()方法的语法格式及使用
2014/08/04 Javascript
深入探寻javascript定时器
2015/01/02 Javascript
又一枚精彩的弹幕效果jQuery实现
2016/07/25 Javascript
JS字符串按逗号和回车分隔的方法
2017/04/25 Javascript
Vue.js实现可配置的登录表单代码详解
2018/03/29 Javascript
vue 动态绑定背景图片的方法
2018/08/10 Javascript
什么时候不能在 Node.js 中使用 Lock Files
2019/06/24 Javascript
JavaScript 斐波那契数列 倒序输出 输出100以内的质数代码实例
2019/09/11 Javascript
微信小程序页面渲染实现方法
2019/11/06 Javascript
深入理解 TypeScript Reflect Metadata
2019/12/12 Javascript
Vue2.0 $set()的正确使用详解
2020/07/28 Javascript
Vue select 绑定动态变量的实例讲解
2020/10/22 Javascript
[00:32]2018DOTA2亚洲邀请赛VG出场
2018/04/03 DOTA
python实现获取序列中最小的几个元素
2014/09/25 Python
Python中文分词工具之结巴分词用法实例总结【经典案例】
2017/04/15 Python
Python实现的字典排序操作示例【按键名key与键值value排序】
2018/12/21 Python
Python根据指定文件生成XML的方法
2020/06/29 Python
Python私有属性私有方法应用实例解析
2020/09/15 Python
HTML5 Web Database 数据库的SQL语句的使用方法
2012/12/09 HTML / CSS
欧洲最大的滑雪假期供应商之一:Sunweb Holidays
2018/01/06 全球购物
2014年教师节活动总结
2014/08/29 职场文书
门市房租房协议书
2014/12/04 职场文书
2019年浪漫婚礼证婚词
2019/06/27 职场文书
2019年世界儿童日宣传标语
2019/11/22 职场文书
python实现自动清理文件夹旧文件
2021/05/10 Python
Django分页器的用法你都了解吗
2021/05/26 Python