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正则表达式中的括号匹配问题
Dec 14 Python
python单例模式实例分析
Apr 08 Python
python实现线程池的方法
Jun 30 Python
[原创]使用豆瓣提供的国内pypi源
Jul 02 Python
Python编程实现控制cmd命令行显示颜色的方法示例
Aug 14 Python
python基础教程项目二之画幅好画
Apr 02 Python
python如何使用unittest测试接口
Apr 04 Python
matplotlib.pyplot绘图显示控制方法
Jan 15 Python
pandas计数 value_counts()的使用
Jun 24 Python
pycharm新建一个python工程步骤
Jul 16 Python
Python 实现日志同时输出到屏幕和文件
Feb 19 Python
Python中sys模块功能与用法实例详解
Feb 26 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边学边教》(02.Apache+PHP环境配置――下篇)
2006/12/13 PHP
微信公众号开发之文本消息自动回复php代码
2016/08/08 PHP
php set_include_path函数设置 include_path 配置选项
2016/10/30 PHP
深入理解 PHP7 中全新的 zval 容器和引用计数机制
2018/10/15 PHP
php的命名空间与自动加载实现方法
2019/08/25 PHP
jQuery中读取json文件示例代码
2013/05/10 Javascript
兼容主流浏览器的JS复制内容到剪贴板
2014/12/12 Javascript
js实现分享到随页面滚动而滑动效果的方法
2015/04/10 Javascript
禁止按回车键提交表单的方法
2015/06/11 Javascript
jqTransform美化表单
2015/10/10 Javascript
实例详解Nodejs 保存 payload 发送过来的文件
2016/01/14 NodeJs
EasyUI 结合JS导出Excel文件的实现方法
2016/11/10 Javascript
详解用原生JavaScript实现jQuery的某些简单功能
2016/12/19 Javascript
angular实现spa单页面应用实例
2017/07/10 Javascript
Vue计算属性的使用
2017/08/04 Javascript
js的函数的按值传递参数(实例讲解)
2017/11/16 Javascript
详解Node.js模板引擎Jade入门
2018/01/19 Javascript
Vue框架里使用Swiper的方法示例
2018/09/20 Javascript
vue权限管理系统的实现代码
2019/01/17 Javascript
vue组件定义,全局、局部组件,配合模板及动态组件功能示例
2019/03/19 Javascript
解决vue项目刷新后,导航菜单高亮显示的位置不对问题
2019/11/01 Javascript
vue+ts下对axios的封装实现
2020/02/18 Javascript
在Python下尝试多线程编程
2015/04/28 Python
Python中字符串的常见操作技巧总结
2016/07/28 Python
Nginx+Uwsgi+Django 项目部署到服务器的思路详解
2020/05/08 Python
python批量修改文件名的示例
2020/09/27 Python
浅谈CSS3 动画卡顿解决方案
2019/01/02 HTML / CSS
Sneaker Studio捷克:购买运动鞋
2018/07/08 全球购物
Coggles美国/加拿大:高级国际时装零售商
2018/10/23 全球购物
欧洲著名的二手奢侈品网站:Vestiaire Collective
2020/03/07 全球购物
印度电子产品购物网站:Vijay Sales
2021/02/16 全球购物
请说出你所知道的线程同步的方法
2013/04/19 面试题
12岁生日感言
2014/01/21 职场文书
教师继续教育反思周记
2015/06/25 职场文书
2015年基层党建工作汇报材料
2015/06/25 职场文书
nginx代理实现静态资源访问的示例代码
2022/07/07 Servers