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相关的问题
Jun 30 Python
Python标准库之Sys模块使用详解
May 23 Python
python简单分割文件的方法
Jul 30 Python
简单实现python爬虫功能
Dec 31 Python
Python 遍历子文件和所有子文件夹的代码实例
Dec 21 Python
Django objects.all()、objects.get()与objects.filter()之间的区别介绍
Jun 12 Python
Windows系统下多版本pip的共存问题详解
Oct 10 Python
Python数据可视化正态分布简单分析及实现代码
Dec 04 Python
Python cv2 图像自适应灰度直方图均衡化处理方法
Dec 07 Python
详解PyCharm+QTDesigner+PyUIC使用教程
Jun 13 Python
在python里使用await关键字来等另外一个协程的实例
May 04 Python
Pytorch 图像变换函数集合小结
Feb 01 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伪造referer实例代码
2008/09/20 PHP
PHP动态分页函数,PHP开发分页必备啦
2011/11/07 PHP
php数组中删除元素之重新索引的方法
2014/09/16 PHP
PHP使用memcache缓存技术提高响应速度的方法
2014/12/26 PHP
javascript 进度条 实现代码
2009/07/30 Javascript
ext 列表页面关于多行查询的办法
2010/03/25 Javascript
Javascript 按位与赋值运算符 (&=)使用介绍
2014/02/04 Javascript
jquery的ajax跨域请求原理和示例
2014/05/08 Javascript
js中的getAttribute方法使用示例
2014/08/01 Javascript
jQuery多文件异步上传带进度条实例代码
2016/08/16 Javascript
gulp-uglify 与gulp.watch()配合使用时报错(重复压缩问题)
2016/08/24 Javascript
懒加载实现的分页&&网站footer自适应
2016/12/21 Javascript
vue2.0中goods选购栏滚动算法的实现代码
2017/05/17 Javascript
JavaScript中利用Array filter() 方法压缩稀疏数组
2018/02/24 Javascript
使用Three.js实现太阳系八大行星的自转公转示例代码
2019/04/09 Javascript
在Vue项目中用fullcalendar制作日程表的示例代码
2019/08/04 Javascript
细述Javascript的加法运算符的具体使用
2019/10/18 Javascript
如何在Vue中抽离接口配置文件
2019/10/31 Javascript
js判断在哪个浏览器打开项目的方法
2020/01/21 Javascript
微信小程序仿淘宝热搜词在搜索框中轮播功能
2020/01/21 Javascript
vue 使用v-for进行循环的实例代码详解
2020/02/19 Javascript
[04:07]显微镜下的DOTA2第八期——英雄复活动作
2014/06/24 DOTA
使用Python生成XML的方法实例
2017/03/21 Python
解决pip install的时候报错timed out的问题
2018/06/12 Python
python百行代码自制电脑端网速悬浮窗的实现
2020/05/12 Python
Python持续监听文件变化代码实例
2020/07/22 Python
阿玛瑞酒店中文官方网站:Amari.com
2018/02/13 全球购物
英国著名药妆店:Superdrug
2021/02/13 全球购物
《玩具柜台前的孩子》教学反思
2014/02/13 职场文书
经典婚礼主持开场白
2014/03/13 职场文书
团结演讲稿范文
2014/05/23 职场文书
IT工程师岗位职责
2014/07/04 职场文书
校运动会广播稿(100篇)
2014/09/12 职场文书
党支部三严三实对照检查材料思想汇报
2014/09/29 职场文书
技术员个人工作总结
2015/03/03 职场文书
javaScript Array api梳理
2021/03/31 Javascript