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正则表达式介绍
Aug 06 Python
python使用xlrd模块读写Excel文件的方法
May 06 Python
python发送邮件功能实现代码
Jul 15 Python
Python3.6连接Oracle数据库的方法详解
May 18 Python
Python实现的爬取小说爬虫功能示例
Mar 30 Python
PyQt4实时显示文本内容GUI的示例
Jun 14 Python
python模拟菜刀反弹shell绕过限制【推荐】
Jun 25 Python
简单了解python元组tuple相关原理
Dec 02 Python
pytorch:model.train和model.eval用法及区别详解
Feb 20 Python
Python extract及contains方法代码实例
Sep 11 Python
PyQt实现计数器的方法示例
Jan 18 Python
Python实现byte转integer
Jun 03 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之第七天
2006/10/09 PHP
php htmlspecialchars()与shtmlspecialchars()函数的深入分析
2013/06/05 PHP
php数组比较实现查找连续数的方法
2015/07/29 PHP
php 浮点数比较方法详解
2017/05/05 PHP
浅谈PHP错误类型及屏蔽方法
2017/05/27 PHP
php分享朋友圈的实现代码
2019/02/18 PHP
js类中获取外部函数名的方法
2007/08/19 Javascript
JQuery 常用操作代码
2010/03/14 Javascript
js 文件引入实现代码
2010/04/23 Javascript
jquery 使用简明教程
2014/03/05 Javascript
js获取当前日期前七天的方法
2015/02/28 Javascript
JavaScript简单实现鼠标移动切换图片的方法
2016/02/23 Javascript
javascript cookie的简单应用
2016/02/24 Javascript
微信小程序 支付简单实例及注意事项
2017/01/06 Javascript
vue-cli与webpack处理静态资源的方法及webpack打包的坑
2018/05/15 Javascript
postman自定义函数实现 时间函数的思路详解
2019/04/17 Javascript
jQuery实现的记住帐号密码功能完整示例
2019/08/03 jQuery
layui关闭弹窗后刷新主页面和当前更改项的例子
2019/09/06 Javascript
javascript的delete运算符知识点总结
2019/11/19 Javascript
基于postman获取动态数据过程详解
2020/09/08 Javascript
简述Python中的面向对象编程的概念
2015/04/27 Python
Python实现模拟登录及表单提交的方法
2015/07/25 Python
python中退出多层循环的方法
2018/11/27 Python
浅谈python中真正关闭socket的方法
2018/12/18 Python
tensorflow入门:TFRecordDataset变长数据的batch读取详解
2020/01/20 Python
Python urllib2运行过程原理解析
2020/06/04 Python
is_file和file_exists效率比较
2021/03/14 PHP
印度购物网站:TATA CLiQ
2017/11/23 全球购物
一份创业计划书范文
2014/02/08 职场文书
大三学生学年自我鉴定
2014/09/12 职场文书
机关单位工作失职检讨书
2014/11/20 职场文书
求职导师推荐信范文
2015/03/27 职场文书
消防宣传标语大全
2015/08/03 职场文书
goland 清除所有的默认设置操作
2021/04/28 Golang
微软官方消息,在 2023 年 4 月 11 日之后微软将不再为 Office 2013 和 Skype for Business 2015 提供安全更新
2022/04/21 数码科技
JavaScript原型链中函数和对象的理解
2022/06/16 Javascript