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使用Redis实现作业调度系统(超简单)
Mar 22 Python
Pyhton中单行和多行注释的使用方法及规范
Oct 11 Python
python通过cookie模拟已登录状态的初步研究
Nov 09 Python
Python实现屏幕截图的两种方式
Feb 05 Python
pandas DataFrame数据转为list的方法
Apr 11 Python
对python for 文件指定行读写操作详解
Dec 29 Python
详解python的四种内置数据结构
Mar 19 Python
Python字典推导式将cookie字符串转化为字典解析
Aug 10 Python
tesserocr与pytesseract模块的使用方法解析
Aug 30 Python
django 中使用DateTime常用的时间查询方式
Dec 03 Python
python 字典访问的三种方法小结
Dec 05 Python
Python-OpenCV教程之图像的位运算详解
Jun 21 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的静态成员函数效率更高的原因
2014/06/13 PHP
phpstorm编辑器乱码问题解决
2014/12/01 PHP
nginx下安装php7+php5
2016/07/31 PHP
laravel5表单唯一验证的实例代码
2019/09/30 PHP
PHP使用openssl扩展实现加解密方法示例
2020/02/20 PHP
使用JS操作页面表格,元素的一些技巧
2007/02/02 Javascript
在JavaScript中获取请求的URL参数[正则]
2010/12/25 Javascript
jquery里的正则表达式说明
2011/08/03 Javascript
javascript 实现 秒杀,团购 倒计时展示的记录 分享
2013/07/12 Javascript
js如何判断用户是在PC端和还是移动端访问
2014/04/24 Javascript
jQuery实现美观的多级动画效果菜单代码
2015/09/06 Javascript
基于VuePress 轻量级静态网站生成器的实现方法
2018/04/17 Javascript
JS实现根据指定值删除数组中的元素操作示例
2018/08/02 Javascript
JS实现可针对算术表达式求值的计算器功能示例
2018/09/04 Javascript
js监听html页面的上下滚动事件方法
2018/09/11 Javascript
手把手教你写一个微信小程序(推荐)
2018/10/17 Javascript
JS根据Unix时间戳显示发布时间是多久前【项目实测】
2019/07/10 Javascript
vue遍历对象中的数组取值示例
2019/11/07 Javascript
Js代码中的span拼接问题解决
2019/11/22 Javascript
javaScript中indexOf用法技巧
2019/11/26 Javascript
详解微信小程序「渲染层网络层错误」的解决方法
2021/01/06 Javascript
[35:29]Secret vs VG 2018国际邀请赛淘汰赛BO3 第三场 8.23
2018/08/24 DOTA
Python3实现连接SQLite数据库的方法
2014/08/23 Python
python开发中range()函数用法实例分析
2015/11/12 Python
python difflib模块示例讲解
2017/09/13 Python
python  创建一个保留重复值的列表的补码
2018/10/15 Python
python 公共方法汇总解析
2019/09/16 Python
sklearn和keras的数据切分与交叉验证的实例详解
2020/06/19 Python
Python远程linux执行命令实现
2020/11/11 Python
Chicco婴儿用品美国官网:汽车座椅、婴儿推车、高脚椅等
2018/11/05 全球购物
医务工作者先进事迹材料
2014/01/26 职场文书
项目委托协议书(最新)
2014/09/13 职场文书
Nginx使用X-Accel-Redirect实现静态文件下载的统计、鉴权、防盗链、限速等
2021/04/04 Servers
MySQL面试题讲解之如何设置Hash索引
2021/11/01 MySQL
React四级菜单的实现
2022/04/08 Javascript
Elasticsearch 聚合查询和排序
2022/04/19 Python