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抓取最新博客内容并生成Rss
May 17 Python
浅谈Python中的闭包
Jul 08 Python
在Django的URLconf中进行函数导入的方法
Jul 18 Python
python数据分析数据标准化及离散化详解
Feb 26 Python
Python3.4 tkinter,PIL图片转换
Jun 21 Python
解决python中画图时x,y轴名称出现中文乱码的问题
Jan 29 Python
Flask框架重定向,错误显示,Responses响应及Sessions会话操作示例
Aug 01 Python
Python帮你识破双11的套路
Nov 11 Python
Python编译成.so文件进行加密后调用的实现
Dec 23 Python
Pycharm安装Qt Design快捷工具的详细教程
Nov 18 Python
python基于爬虫+django,打造个性化API接口
Jan 21 Python
如何用python爬取微博热搜数据并保存
Feb 20 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的access操作类
2008/04/09 PHP
php统计文章排行示例
2014/03/04 PHP
php网页标题中文乱码的有效解决方法
2014/03/05 PHP
PHP缓存机制Output Control详解
2014/07/14 PHP
smarty实现多级分类的方法
2014/12/05 PHP
用js实现in_array的方法
2013/11/05 Javascript
js中自定义方法实现停留几秒sleep
2014/07/11 Javascript
js网页滚动条滚动事件实例分析
2015/05/05 Javascript
Bootstrap 3的box-sizing样式导致UEditor控件的图片无法正常缩放的解决方案
2016/09/15 Javascript
nodejs使用express获取get和post传值及session验证的方法
2017/11/09 NodeJs
JS函数节流和函数防抖问题分析
2017/12/18 Javascript
ES6中let 和 const 的新特性
2018/09/03 Javascript
JavaScript indexOf()原理及使用方法详解
2020/07/09 Javascript
javascript实现京东登录显示隐藏密码
2020/08/02 Javascript
Vue中强制组件重新渲染的正确方法
2021/01/03 Vue.js
python中ConfigParse模块的用法
2014/09/29 Python
python通过zlib实现压缩与解压字符串的方法
2014/11/19 Python
Python中运行并行任务技巧
2015/02/26 Python
python抽象基类用法实例分析
2015/06/04 Python
使用Python发送各种形式的邮件的方法汇总
2015/11/09 Python
Django自定义插件实现网站登录验证码功能
2017/04/19 Python
matlab中实现矩阵删除一行或一列的方法
2018/04/04 Python
python 通过logging写入日志到文件和控制台的实例
2018/04/28 Python
深入浅析Python中list的复制及深拷贝与浅拷贝
2018/09/03 Python
使用python对文件中的数值进行累加的实例
2018/11/28 Python
django实现HttpResponse返回json数据为中文
2020/03/27 Python
打印tensorflow恢复模型中所有变量与操作节点方式
2020/05/26 Python
python def 定义函数,调用函数方式
2020/06/02 Python
英文翻译的自我评价语句
2013/10/04 职场文书
高中毕业生自我鉴定例文
2013/12/29 职场文书
幼儿园小班教师寄语
2014/04/03 职场文书
保护环境倡议书300字
2014/05/19 职场文书
护士自我推荐信范文
2015/03/24 职场文书
「Manga Time Kirara MAX」2022年5月号封面公开
2022/03/21 日漫
vue 把二维或多维数组转一维数组
2022/04/24 Vue.js
Python使用Beautiful Soup(BS4)库解析HTML和XML
2022/06/05 Python