python编程:利用函数递归调用和turtle绘制树-3

python编程:利用函数递归调用和turtle绘制树-3



源代码(还不是太了解~~~囧)
  1. # drawtree.py
  2.  
  3. from turtle import Turtle, mainloop
  4.  
  5. def tree(plist, l, a, f):
  6.     """ plist is list of pens
  7.     l is length of branch
  8.     a is half of the angle between 2 branches
  9.     f is factor by which branch is shortened
  10.     from level to level."""
  11.     if l > 5#
  12.         lst = []
  13.         for in plist:
  14.             p.forward(l)#沿着当前的方向画画Move the turtle forward by the specified distance, in the direction the turtle is headed.
  15.             = p.clone()#Create and return a clone of the turtle with same position, heading and turtle properties.
  16.             p.left(a) #Turn turtle left by angle units
  17.             q.right(a)# turn turtle right by angle units, nits are by default degrees, but can be set via the degrees() and radians() functions.
  18.             lst.append(p)#将元素增加到列表的最后
  19.             lst.append(q)
  20.         tree(lst, l*f, a, f)
  21.    
  22.             
  23.  
  24. def main():
  25.     = Turtle()
  26.     p.color("green")
  27.     p.pensize(5)
  28.     #p.setundobuffer(None)
  29.     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,
  30.     #because hiding the turtle speeds up the drawing observably.
  31.     #p.speed(10)
  32.    # p.getscreen().tracer(1,0)#Return the TurtleScreen object the turtle is drawing on.
  33.     p.speed(10)
  34.     #TurtleScreen methods can then be called for that object.
  35.     p.left(90)# Turn turtle left by angle units. direction 调整画笔
  36.  
  37.     p.penup() #Pull the pen up – no drawing when moving.
  38.     p.goto(0,-200)#Move turtle to an absolute position. If the pen is down, draw line. Do not change the turtle’s orientation.
  39.     p.pendown()# Pull the pen down – drawing when moving. 这三条语句是一个组合相当于先把笔收起来再移动到指定位置,再把笔放下开始画
  40.     #否则turtle一移动就会自动的把线画出来
  41.  
  42.     #t = tree([p], 200, 65, 0.6375)
  43.     = tree([p], 200650.6375)
  44.      
  45. main()


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值