-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
127 lines (100 loc) · 3.58 KB
/
main.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
119
120
121
122
123
124
125
126
127
import time
import random
import asyncio
import os
import statistics
import curses
from itertools import cycle
import fire_animation
from curses_tools import draw_frame, \
read_controls, get_frame_size
TIC_TIMEOUT = 0.1
def get_frames(folder="spaceship"):
frames = []
for filename in os.listdir(folder):
filepath = os.path.join(folder, filename)
with open(filepath, 'r') as file:
frames.append(file.read())
return frames
def get_playground_limits(canvas, with_border=True):
max_row, max_col = canvas.getmaxyx()
canvas_padding = 2
if with_border:
min_row, min_col = (1, 1)
max_row -= canvas_padding
max_col -= canvas_padding
else:
min_row, min_col = (0, 0)
return min_row, min_col, max_row, max_col
def get_random_coords(min_row, max_row, min_col, max_col):
return (
random.randint(min_row, max_row),
random.randint(min_col, max_col),
)
def draw(canvas):
canvas.nodelay(True)
symbols = "*.○+●°•☆:☼★٭✽❇❈❉❊❋⁂"
curses.curs_set(False)
canvas.border()
window_y, window_x = canvas.getmaxyx()
coroutines = []
frames = get_frames()
coroutines.append(animate_spaceship(canvas, window_y//2, window_x//2, frames))
stars_count = 100
min_row, min_col, max_row, max_col = get_playground_limits(canvas)
for i in range(stars_count):
coroutines.append(blink(
canvas,
*get_random_coords(min_row, max_row, min_col, max_col),
random.randint(0, 3),
random.choice(symbols),
))
fire_coroutine = fire_animation.fire(
canvas,
window_y//2,
window_x//2,
)
coroutines.append(fire_coroutine)
while True:
for coroutine in coroutines.copy():
try:
coroutine.send(None)
except StopIteration:
coroutines.remove(coroutine)
canvas.refresh()
time.sleep(TIC_TIMEOUT)
async def animate_spaceship(canvas, row, column, frames):
canvas.nodelay(True)
screen_min_y, screen_min_x, screen_max_y, screen_max_x = get_playground_limits(canvas)
for frame in cycle(frames):
for i in range(2):
height, width = get_frame_size(frame)
frame_max_row, frame_max_column = screen_max_y - height, screen_max_x - width
x_direction, y_direction, space_pressed = read_controls(canvas)
row += x_direction
column += y_direction
row = statistics.median([screen_min_y, row, frame_max_row])
column = statistics.median([screen_min_x, column, frame_max_column])
draw_frame(canvas, row, column, frame)
await asyncio.sleep(0)
# стираем предыдущий кадр, прежде чем рисовать новый
draw_frame(canvas, row, column, frame, negative=True)
async def blink(canvas, row, column, seconds, symbol='*'):
while True:
for _ in range(seconds):
await asyncio.sleep(0)
canvas.addstr(row, column, symbol, curses.A_DIM)
for _ in range(20):
await asyncio.sleep(0)
canvas.addstr(row, column, symbol)
for _ in range(3):
await asyncio.sleep(0)
canvas.addstr(row, column, symbol, curses.A_BOLD)
for _ in range(5):
await asyncio.sleep(0)
canvas.addstr(row, column, symbol)
for _ in range(3):
await asyncio.sleep(0)
if __name__ == '__main__':
curses.update_lines_cols()
curses.wrapper(draw)