一、二叉树
1.代码
import turtle
turtle.setup(700,700)
turtle.penup()
turtle.goto(0,-250)
turtle.pendown()
turtle.left(90)
turtle.pensize(2)
turtle.fd(200)
turtle.delay(1)
def DrawTree(length,angle):
if length > 15:
turtle.left(angle)
turtle.fd(length)
DrawTree(length-15,angle)
turtle.backward(length)
turtle.rt(2*angle)
turtle.fd(length)
DrawTree(length-15,angle)
turtle.backward(length)
turtle.left(angle)
DrawTree(100,30)
turtle.mainloop()
2.运行结果
一、改进的二叉树
1.代码
from turtle import *
from random import *
screensize(500,500)
pensize(5)
pencolor("brown")
penup()
goto(0,-250)
seth(90)
pendown()
fd(250)
delay(1)
def tree(l):
if l>0:
left(30)
fd(l)
tree(l-20)
backward(l)
rt(60)
fd(l)
tree(l-20)
backward(l)
left(30)
def leaves():
pencolor("green")
for i in range(50):
x=uniform(-250,250)
y=uniform(100,280)
penup()
goto(x,y)
pendown()
dot(10)
pencolor("yellow")
for i in range(50):
x=uniform(-250,250)
y=uniform(-280,-200)
penup()
goto(x,y)
pendown()
dot(10)
tree(100)
leaves()
mainloop()
2.运行结果