答案 0 :(得分:0)
我不得不说,我喜欢您的程序当前生成的树,因为它们更像是真实的树,并且每棵都是不同的。如果我们从代码中删除随机元素,并添加一个中间分支,那么我们将获得所需的一棵统一树,而只有所需的一棵统一树:
from turtle import Screen, Turtle, Vec2D
THICKNESS = 30
def draw_line(position, angle, length, size, color):
turtle.penup()
turtle.goto(position)
turtle.setheading(angle)
turtle.color(color)
turtle.pensize(size)
turtle.pendown()
turtle.forward(length)
def draw_tree(position, angle, length, size, color, n):
if n == 0:
return
if n <= 3:
color = 'green'
draw_line(position, angle, length, size, color)
position = turtle.position()
length /= 1.4
size *= 0.7
draw_tree(position, angle - THICKNESS, length, size, color, n - 1)
draw_tree(position, angle, length, size, color, n - 1)
draw_tree(position, angle + THICKNESS, length, size, color, n - 1)
screen = Screen()
screen.tracer(False)
turtle = Turtle(visible=False)
draw_tree(Vec2D(0, -350), 90, 150, 10, 'brown', 9)
screen.update()
screen.tracer(True)
screen.exitonclick()