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()

源代码出处: http://www.icourse163.org/learn/BIT-268001?tid=1002001005#/learn/content?type=detail&id=1002613031&cid=1002856091

你可能感兴趣的:(python编程:利用函数递归调用和turtle绘制树-3)