使用python的递归画出一颗树

使用python的递归画出一颗树

python 中使用递归画出一棵树的源代码,大家可以参考画出一片森林,源代码如下:

# drawTree.py
import turtle as p


def maketree(x,y):
	# p = Turtle()
	p.color("green")
	p.pensize(5)
	#p.setundobuffer(None)
	p.hideturtle()
	#Make the turtle inbisible.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(10000)
	p.getscreen().tracer(30,0)
	#return the TurtleScreen object the turtle is drawing on.
	#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(x,y) #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], 110, 65, 0.6375)
	print(len(p.getscreen().turtles())) #用了多少个turtle绘制


def tree(plist, l, a, f):
	"""plist is list of pensize
	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():
	maketree(-200,-200)
	# maketree(0,0)
	# maketree( 200,-200)
	p.done()
	# print("请输入:")

main()