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

yipeiwu_com6年前Python基础

本文实例主要是对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动态绘制一棵树实例分享的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

相关文章

75条笑死人的知乎神回复,用60行代码就爬完了

75条笑死人的知乎神回复,用60行代码就爬完了

读:知乎神回复都有些什么特点呢?其实爬取知乎神回复很简单,这篇文章我们就来揭晓一下背后的原理。 我们先来观察一下:   大家看出什么规律了么?短小精辟有没有?赞同很多有没有?...

python打开使用的方法

python打开使用的方法

python怎么打开使用? 1、首先需要打开电脑的桌面,如图所示,并按开始的快捷键点击安装好的python3.6程序进入。 2、然后点击进入之后,如图所示,可以看到页面上面的三个大于号...

天翼开放平台免费短信验证码接口使用实例

对于目前众多的验证码解决方案来说,这个API有着中国电信这个重量级的运营商为靠山,应该是比较靠谱的了,而且还是免费的。详细情况请参阅:http://open.189.cn 使用方式:#定...

python的dict,set,list,tuple应用详解

本文深入剖析了python中dict,set,list,tuple应用及对应示例,有助于读者对其概念及原理的掌握。具体如下: 1.字典(dict) dict 用 {} 包围 dict....

Python Tensor FLow简单使用方法实例详解

Python Tensor FLow简单使用方法实例详解

本文实例讲述了Python Tensor FLow简单使用方法。分享给大家供大家参考,具体如下: 1、基础概念 Tensor表示张量,是一种多维数组的数据结构。Flow代表流,是指张量之...