Skip to content

Commit e21874e

Browse files
committed
141
1 parent 970e5d8 commit e21874e

File tree

1 file changed

+71
-1
lines changed

1 file changed

+71
-1
lines changed

md/141.md

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,74 @@
66
@version
77
@date 2020/02/08
88
```
9-
9+
10+
#### 141 turtle绘制漫天雪花
11+
12+
13+
导入模块
14+
15+
导入 `turtle`库和 python的 `random`
16+
17+
```python
18+
import turtle as p
19+
import random
20+
```
21+
22+
绘制雪花
23+
24+
```python
25+
def snow(snow_count):
26+
p.hideturtle()
27+
p.speed(500)
28+
p.pensize(2)
29+
for i in range(snow_count):
30+
r = random.random()
31+
g = random.random()
32+
b = random.random()
33+
p.pencolor(r, g, b)
34+
p.pu()
35+
p.goto(random.randint(-350, 350), random.randint(1, 270))
36+
p.pd()
37+
dens = random.randint(8, 12)
38+
snowsize = random.randint(10, 14)
39+
for _ in range(dens):
40+
p.forward(snowsize) # 向当前画笔方向移动snowsize像素长度
41+
p.backward(snowsize) # 向当前画笔相反方向移动snowsize像素长度
42+
p.right(360 / dens) # 顺时针移动360 / dens度
43+
44+
```
45+
46+
绘制地面
47+
48+
```python
49+
def ground(ground_line_count):
50+
p.hideturtle()
51+
p.speed(500)
52+
for i in range(ground_line_count):
53+
p.pensize(random.randint(5, 10))
54+
x = random.randint(-400, 350)
55+
y = random.randint(-280, -1)
56+
r = -y / 280
57+
g = -y / 280
58+
b = -y / 280
59+
p.pencolor(r, g, b)
60+
p.penup() # 抬起画笔
61+
p.goto(x, y) # 让画笔移动到此位置
62+
p.pendown() # 放下画笔
63+
p.forward(random.randint(40, 100)) # 眼当前画笔方向向前移动40~100距离
64+
```
65+
66+
主函数
67+
68+
```python
69+
def main():
70+
p.setup(800, 600, 0, 0)
71+
# p.tracer(False)
72+
p.bgcolor("black")
73+
snow(30)
74+
ground(30)
75+
# p.tracer(True)
76+
p.mainloop()
77+
78+
main()
79+
```

0 commit comments

Comments
 (0)