-
Notifications
You must be signed in to change notification settings - Fork 0
/
树3.py
118 lines (64 loc) · 1.69 KB
/
树3.py
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# Project Leader:Python代码大全
# Project:表白树源代码
import turtle
import random
def love(x,y):#在(x,y)处画爱心lalala
lv=turtle.Turtle()
lv.hideturtle()
lv.up()
lv.goto(x,y)#定位到(x,y)
def curvemove():#画圆弧
for i in range(20):
lv.right(10)
lv.forward(2)
lv.color('green')
lv.speed(10000000)
lv.pensize(1)
#开始画爱心lalala
lv.down()
lv.begin_fill()
lv.left(140)
lv.forward(22)
curvemove()
lv.left(120)
curvemove()
lv.forward(22)
lv.left(140)#画完复位
lv.end_fill()
def tree(branchLen,t):
if branchLen > 5:#剩余树枝太少要结束递归
if branchLen<20:
t.color("green")
t.pensize(random.uniform((branchLen + 5) / 4 - 2, (branchLen + 6) / 4 + 5))
t.down()
t.forward(branchLen)
love(t.xcor(),t.ycor())#传输现在turtle的坐标
t.up()
t.backward(branchLen)
t.color("brown")
return
t.pensize(random.uniform((branchLen+5)/4-2,(branchLen+6)/4+5))
t.down()
t.forward(branchLen)
# 以下递归
ang=random.uniform(15,45)
t.right(ang)
tree(branchLen-random.uniform(12,16),t)#随机决定减小长度
t.left(2*ang)
tree(branchLen-random.uniform(12,16),t)#随机决定减小长度
t.right(ang)
t.up()
t.backward(branchLen)
myWin = turtle.Screen()
t = turtle.Turtle()
t.hideturtle()
t.speed(1000)
t.left(90)
t.up()
t.backward(200)
t.down()
t.color("brown")
t.pensize(32)
t.forward(60)
tree(100,t)
myWin.exitonclick()