用python中的递归画一棵小树

时间:2022-11-12 10:40:42

先给出画一颗小树的代码:

#tree.py
from turtle import Turtle


def tree(plist, l, a, f):
# plist is list of pens
# l is the length of brantch
# is half of the angle between 2 brantches
# f is factor by which branch is shortened from level to level
if l>5:
lst=[]
for p in plist:
p.forward(l)   #向前走l的长度
q=p.clone()
p.left(a)      #逆时针转动箭头方向
q.right(a)

lst.append(p)
lst.append(q)
tree(lst, l*f, a, f)


def main():
p=Turtle()
p.color("green")
p.pensize(5)            # set width of line(pen)
p.speed(10)             # set speed of pen (from 1 to 10)

p.hideturtle()          # 隐藏箭头

#p.getscreen().tracer(30,0)  #Return the TurtleScreen object the turtle is drawing on

p.left(90)
p.penup()
p.goto(0,0)
p.pendown()

t=tree([p],110,65,0.6375)
main()


turtle的函数调用

用python中的递归画一棵小树 
用python中的递归画一棵小树 
用python中的递归画一棵小树