my_tree.py
845 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import turtle
import random
pen = turtle.Turtle()
pen.color('sienna')
pen.speed(0)
# 画布大小
w = turtle.Screen()
w.bgcolor('wheat') # wheat小麦
# 移动到起点
pen.left(90)
pen.up()
pen.backward(200) # 后退
pen.down()
def tree(n):
if n >= 0:
if n <= 12:
color_list = ['snow','lightcoral']
color = random.choice(color_list)
pen.pencolor(color)
pen.pensize(n/3)
else:
pen.color("sienna")
pen.pensize(n/10)
pen.forward(n)
angle = random.random()
pen.right(30*angle)
length = 1.5 * random.random()
tree(n-10*length)
pen.left(60*angle)
tree(n-10*length)
pen.right(30*angle)
pen.up()
pen.backward(n)
pen.down()
tree(75)
turtle.done()