forked from jackzhenguo/python-small-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathturtlesnow.py
More file actions
48 lines (44 loc) · 1.36 KB
/
turtlesnow.py
File metadata and controls
48 lines (44 loc) · 1.36 KB
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
import turtle as p
import random
def snow(snow_count):
p.hideturtle()
p.speed(500)
p.pensize(2)
for i in range(snow_count):
r = random.random()
g = random.random()
b = random.random()
p.pencolor(r, g, b)
p.pu()
p.goto(random.randint(-350, 350), random.randint(1, 270))
p.pd()
dens = random.randint(8, 12)
snowsize = random.randint(10, 14)
for _ in range(dens):
p.forward(snowsize) # 向当前画笔方向移动snowsize像素长度
p.backward(snowsize) # 向当前画笔相反方向移动snowsize像素长度
p.right(360 / dens) # 顺时针移动360 / dens度
def ground(ground_line_count):
p.hideturtle()
p.speed(500)
for i in range(ground_line_count):
p.pensize(random.randint(5, 10))
x = random.randint(-400, 350)
y = random.randint(-280, -1)
r = -y / 280
g = -y / 280
b = -y / 280
p.pencolor(r, g, b)
p.penup() # 抬起画笔
p.goto(x, y) # 让画笔移动到此位置
p.pendown() # 放下画笔
p.forward(random.randint(40, 100)) # 眼当前画笔方向向前移动40~100距离
def main():
p.setup(800, 600, 0, 0)
# p.tracer(False)
p.bgcolor("black")
snow(30)
ground(30)
# p.tracer(True)
p.mainloop()
main()