import turtle

from random import *

from math import *

def tree(n, l):

turtle.pendown()

turtle.pencolor(t, t, t)

turtle.pensize(n / 3)

turtle.forward(l)

if n > 0:

b = random() * 15 + 10

c = random() * 15 + 10

d= l * (random() * 0.25 + 0.7)

turtle.right(b)

tree(n - 1, d)

turtle.left(b + c)

tree(n - 1, d)

turtle.right(c)

else:

turtle.right(90)

turtle.pencolor(n, n*0.8, n*0.8)

turtle.circle(3)

turtle.left(90)

z=random()

if z>0.7:

turtle.up()

an = -40 + random()*40

dis = int(800*random()*0.5 + 400*random()*0.3 + 200*random()*0.2)

turtle.forward(dis)

turtle.down()

turtle.right(90)

turtle.pencolor(n*0.5+0.5, 0.4+n*0.4, 0.4+n*0.4)

turtle.circle(2)

turtle.left(90)

turtle.up()

turtle.backward(dis)

turtle.up()

turtle.backward(l)

turtle.bgcolor(0.5, 0.5, 0.5) # 背景色

turtle.hideturtle() # 隐藏turtle

turtle.speed(0) # 速度，1-10渐进，0最快

turtle.tracer(0, 0)

turtle.up() # 抬笔

turtle.backward(100)

turtle.left(90) # 左转90度

turtle.up() # 抬笔

turtle.backward(300) # 后退300

tree(12, 100) # 递归7层

turtle.done()