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

 更新时间:2018年01月16日 14:28:58   作者:wangsiting123  
这篇文章主要介绍了Python+Turtle动态绘制一棵树实例分享,具有一定借鉴价值,需要的朋友可以参考下

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

相关文章

  • Python2.X/Python3.X中urllib库区别讲解

    Python2.X/Python3.X中urllib库区别讲解

    本篇文章通过对比给大家详细讲解了在Python2和Python3中urllib库区别以及用法讲解,有需要的朋友跟着学习下吧。
    2017-12-12
  • Python帮你识破双11的套路

    Python帮你识破双11的套路

    一年一度的“双十一”又要来了,很多人已经开始摩拳擦掌,毕竟几天之后手还在不在就不好说了。看看Python帮你识破双11的套路,需要的朋友可以参考下
    2019-11-11
  • Python使用pycharm导入pymysql教程

    Python使用pycharm导入pymysql教程

    这篇文章主要介绍了Python使用pycharm导入pymysql教程,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-09-09
  • python中如何实现径向基核函数

    python中如何实现径向基核函数

    这篇文章主要介绍了python中如何实现径向基核函数问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-02-02
  • python实现查找所有程序的安装信息

    python实现查找所有程序的安装信息

    本文给大家分享的是使用python通过注册表信息实现快速查找windows应用程序的安装信息的方法和代码示例,有需要的小伙伴可以参考下
    2020-02-02
  • 巧妙使用python opencv库玩转视频帧率

    巧妙使用python opencv库玩转视频帧率

    这篇文章主要介绍了巧妙使用python opencv库玩转视频帧率的教程示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-04-04
  • django 消息框架 message使用详解

    django 消息框架 message使用详解

    这篇文章主要介绍了django 消息框架 message使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-07-07
  • 使用python turtle画高达

    使用python turtle画高达

    今天小编就为大家分享一篇使用python turtle画高达,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-01-01
  • 关于Python3的import问题(pycharm可以运行命令行import错误)

    关于Python3的import问题(pycharm可以运行命令行import错误)

    这篇文章主要介绍了关于Python3的import问题(pycharm可以运行命令行import错误),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-11-11
  • python列表去重的二种方法

    python列表去重的二种方法

    这篇文章主要介绍了python列表去重的二种方法,第二种方法无法保持原有顺序,需要的朋友可以参考下
    2014-02-02

最新评论