-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsnowterm.py
106 lines (90 loc) · 2.86 KB
/
snowterm.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
import curses
import random
import sys
import time
snowflakes = {
'*': 1,
'+': 0.8,
'.': 0.4,
}
def max_dimensions(window):
height, width = window.getmaxyx()
return height - 2, width - 1
def snowflake_char(window):
width = max_dimensions(window)[1]
char = random.choice(list(snowflakes.keys()))
position = random.randrange(1, width)
return (0, position, char)
def update_snowflakes(prev, window):
new = {}
for (height, position), char in prev.items():
max_height = max_dimensions(window)[0]
new_height = height
if random.random() <= snowflakes[char]:
new_height += 1
if new_height > max_height or prev.get((new_height, position)):
new_height -= 1
new[(new_height, position)] = char
return new
def redisplay(snowflakes, window):
for (height, position), char in snowflakes.items():
max_height, max_width = max_dimensions(window)
if height > max_height or position >= max_width:
continue
window.addch(height, position, char)
def draw_moon(window):
moon = [
' ** ',
' *** ',
' ***',
' ***',
' *** ',
' ** ',
]
start_position = max_dimensions(window)[1] - 10
window.attrset(curses.color_pair(1))
for height, line in enumerate(moon, start=1):
for position, sym in enumerate(line, start=start_position):
window.addch(height, position, sym)
window.attrset(curses.color_pair(0))
def main(window, speed):
if curses.can_change_color():
curses.init_color(curses.COLOR_BLACK, 0,0,0)
curses.init_color(curses.COLOR_WHITE, 1000, 1000, 1000)
curses.init_color(curses.COLOR_YELLOW, 1000, 1000, 0)
curses.init_pair(1, curses.COLOR_YELLOW, 0)
try:
curses.curs_set(0)
except Exception:
pass # Can't hide cursor in 2019 huh?
snowflakes = {}
while True:
height, width = max_dimensions(window)
if len(snowflakes.keys()) >= 0.95 * (height * width):
snowflakes.clear()
snowflakes = update_snowflakes(snowflakes, window)
snowflake = snowflake_char(window)
snowflakes[(snowflake[0], snowflake[1])] = snowflake[2]
window.clear()
draw_moon(window)
redisplay(snowflakes, window)
window.refresh()
try:
time.sleep((0.2) / (speed / 100))
except ZeroDivisionError:
time.sleep(0.2)
if __name__ == '__main__':
speed = 100
if len(sys.argv) > 1:
try:
speed = int(sys.argv[1])
except ValueError:
print(
'Usage:\npython snowterm.py [SPEED]\n'
'SPEED is integer representing percents.',
)
sys.exit(1)
try:
curses.wrapper(main, speed)
except KeyboardInterrupt:
sys.exit(0)