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 字符串操作方法大全
Mar 11 Python
详解Python装饰器由浅入深
Dec 09 Python
Python 反转字符串(reverse)的方法小结
Feb 20 Python
Django自定义过滤器定义与用法示例
Mar 22 Python
python将一个英文语句以单词为单位逆序排放的方法
Dec 20 Python
对Python 中矩阵或者数组相减的法则详解
Aug 26 Python
Python 元组操作总结
Sep 18 Python
TensorFlow——Checkpoint为模型添加检查点的实例
Jan 21 Python
使用Pycharm(Python工具)新建项目及创建Python文件的教程
Apr 26 Python
Python通过队列来实现进程间通信的示例
Oct 14 Python
解决pytorch读取自制数据集出现过的问题
May 31 Python
pytorch中的torch.nn.Conv2d()函数图文详解
Feb 28 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
PHP5 安装方法
2007/01/15 PHP
PHP基于mssql扩展远程连接MSSQL的简单实现方法
2016/10/08 PHP
深入理解PHP中mt_rand()随机数的安全
2017/10/12 PHP
Laravel框架实现利用中间件进行操作日志记录功能
2018/06/06 PHP
node.js中的events.emitter.listeners方法使用说明
2014/12/10 Javascript
浅谈JavaScript中的作用域和闭包问题
2015/07/07 Javascript
jQuery实现简单隔行变色的方法
2016/02/20 Javascript
Angular 通过注入 $location 获取与修改当前页面URL的实例
2017/05/31 Javascript
老生常谈JavaScript面向对象基础与this指向问题
2017/10/16 Javascript
vue 点击按钮实现动态挂载子组件的方法
2018/09/07 Javascript
微信小程序实现购物页面左右联动
2019/02/15 Javascript
vue项目部署到nginx/tomcat服务器的实现
2019/08/26 Javascript
layer弹窗在键盘按回车将反复刷新的实现方法
2019/09/25 Javascript
jQuery-App输入框实现实时搜索
2020/11/19 jQuery
[00:43]魔廷新尊——痛苦女王至宝捆绑包
2020/06/12 DOTA
Python使用稀疏矩阵节省内存实例
2014/06/27 Python
Python实现全角半角字符互转的方法
2016/11/28 Python
Python删除Java源文件中全部注释的实现方法
2017/08/30 Python
用python处理MS Word的实例讲解
2018/05/08 Python
python操作mysql代码总结
2018/06/01 Python
Python定义一个跨越多行的字符串的多种方法小结
2018/07/19 Python
django实现用户注册实例讲解
2019/10/30 Python
基于梯度爆炸的解决方法:clip gradient
2020/02/04 Python
Python获取、格式化当前时间日期的方法
2020/02/10 Python
TensorFlow实现checkpoint文件转换为pb文件
2020/02/10 Python
python Paramiko使用示例
2020/09/21 Python
python中用ggplot绘制画图实例讲解
2021/01/26 Python
加拿大健康、婴儿和美容产品在线购物:Well.ca
2016/11/30 全球购物
Weblogic和WebSphere不同特点
2012/05/09 面试题
电子信息科学专业自荐信
2014/01/30 职场文书
2014年人事部工作总结
2014/12/03 职场文书
客房部经理岗位职责
2015/02/02 职场文书
反邪教警示教育活动总结
2015/05/09 职场文书
受欢迎的自荐信,就这么写!
2019/04/19 职场文书
JS异步堆栈追踪之为什么await胜过Promise
2021/04/28 Javascript
IDEA使用SpringAssistant插件创建SpringCloud项目
2021/06/23 Java/Android