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 相关文章推荐
详解Django中的form库的使用
Jul 18 Python
常用python编程模板汇总
Feb 12 Python
Python利用Nagios增加微信报警通知的功能
Feb 18 Python
Python实现的计算马氏距离算法示例
Apr 03 Python
tensorflow使用神经网络实现mnist分类
Sep 08 Python
python pip安装包出现:Failed building wheel for xxx错误的解决
Dec 25 Python
Python垃圾回收机制三种实现方法
Apr 27 Python
Keras自定义实现带masking的meanpooling层方式
Jun 16 Python
解决python打开https出现certificate verify failed的问题
Sep 03 Python
Python CategoricalDtype自定义排序实现原理解析
Sep 11 Python
Python 虚拟环境工作原理解析
Dec 24 Python
Python接口自动化之文件上传/下载接口详解
Apr 05 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
也谈截取首页新闻 - 范例
2006/10/09 PHP
PHP编程中八种常见的文件操作方式
2006/11/19 PHP
PHP 源代码压缩小工具
2009/12/22 PHP
zf框架的校验器使用使用示例(自定义校验器和校验器链)
2014/03/13 PHP
PHP框架Laravel插件Pagination实现自定义分页
2020/04/22 PHP
PHP中类与对象功能、用法实例解读
2020/03/27 PHP
jQuery数组处理代码详解(含实例演示)
2012/02/03 Javascript
js 固定悬浮效果实现思路代码
2013/08/02 Javascript
setInterval()和setTimeout()的用法和区别示例介绍
2013/11/17 Javascript
讲解JavaScript的Backbone.js框架的MVC结构设计理念
2016/02/14 Javascript
JavaScript+canvas实现七色板效果实例
2016/02/18 Javascript
js实现String.Fomat的实例代码
2016/09/02 Javascript
详解vue 中使用 AJAX获取数据的方法
2017/01/18 Javascript
jquery实现弹窗功能(窗口居中显示)
2017/02/27 Javascript
Vue 2.0+Vue-router构建一个简单的单页应用(附源码)
2017/03/14 Javascript
重新理解JavaScript的六种继承方式
2017/03/24 Javascript
Node.js简单入门前传
2017/08/21 Javascript
动手写一个angular版本的Message组件的方法
2017/12/16 Javascript
ES6下子组件调用父组件的方法(推荐)
2018/02/23 Javascript
vue调用本地摄像头实现拍照功能
2020/08/14 Javascript
编程语言Python的发展史
2014/09/26 Python
Python实现堆排序的方法详解
2016/05/03 Python
Python基本数据结构与用法详解【列表、元组、集合、字典】
2019/03/23 Python
Python3.6+Django2.0以上 xadmin站点的配置和使用教程图解
2019/06/04 Python
Python检查 云备份进程是否正常运行代码实例
2019/08/22 Python
python range实例用法分享
2020/02/06 Python
PyCharm刷新项目(文件)目录的实现
2020/02/14 Python
Python单元测试及unittest框架用法实例解析
2020/07/09 Python
芝加哥牛排公司:Chicago Steak Company
2018/10/31 全球购物
公务员职务工作的自我评价
2013/11/01 职场文书
班组安全员工作职责
2014/02/01 职场文书
岗位职责范本大全
2015/02/26 职场文书
通知的写法
2015/04/23 职场文书
2015年基建工作总结范文
2015/05/23 职场文书
数据库的高级查询六:表连接查询:外连接(左外连接,右外连接,UNION关键字,连接中ON与WHERE的不同)
2021/04/05 MySQL
postgreSQL数据库基础知识介绍
2022/04/12 PostgreSQL