Python+Turtle动态绘制一棵树实例分享

作者:wangsiting123 时间:2021-10-03 00:33:20 

本文实例主要是对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动态绘制一棵树实例分享

来源:https://www.cnblogs.com/wangsiting/p/7236327.html

标签:python,turtle,实例
0
投稿

猜你喜欢

  • Python中easy_install 和 pip 的安装及使用

    2023-08-24 13:34:54
  • pycharm新建一个python工程步骤

    2023-08-22 17:42:54
  • IE10增强对HTML5和CSS3的支持

    2011-09-16 20:16:28
  • python学习print中format的用法示例

    2023-06-03 14:21:57
  • python爬虫判断招聘信息是否存在的实例代码

    2022-09-14 20:38:04
  • 基于javascript实现全国省市二级联动下拉选择菜单

    2023-09-14 06:08:52
  • SQL Server命令行导数据的2种方式

    2010-07-26 14:48:00
  • Python判断变量名是否合法的方法示例

    2022-07-31 19:05:12
  • python 时间处理之月份加减问题

    2023-11-08 01:05:42
  • python根据用户需求输入想爬取的内容及页数爬取图片方法详解

    2022-12-21 16:31:19
  • PHP 实现多服务器共享 SESSION 数据

    2023-11-19 12:43:24
  • 简单的淡入淡出图片轮换效果

    2009-05-22 18:38:00
  • PHP中soap的用法实例

    2023-11-14 09:40:24
  • python 检测nginx服务邮件报警的脚本

    2023-08-04 17:34:10
  • asp如何实现网络打印?

    2010-05-24 18:31:00
  • mysql创建master/slave详细步骤

    2011-05-21 15:29:00
  • 编写一个asp代码执行器

    2007-09-24 16:05:00
  • IE6 升级要不要?

    2009-07-23 20:44:00
  • PHP 进程锁定问题分析研究

    2023-11-21 18:14:10
  • 用书的概念理解小网站结构

    2007-10-31 18:08:00
  • asp之家 网络编程 m.aspxhome.com