-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
62 lines (48 loc) · 1.55 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
import sys
import time
from collections import defaultdict
from gameplay import GamePlay
from gameplay_exceptions import GameOverError, GameWinError
from gameplay_utils import MotionDirection
from terminal import Terminal
ESCAPE_KEY = 27
UP_KEY = 259
DOWN_KEY = 258
LEFT_KEY = 260
RIGHT_KEY = 261
pressed_key_to_motion_direction = defaultdict(
lambda: MotionDirection.DO_NOTHING,
{
UP_KEY: MotionDirection.UP,
DOWN_KEY: MotionDirection.DOWN,
LEFT_KEY: MotionDirection.LEFT,
RIGHT_KEY: MotionDirection.RIGHT,
},
)
def main():
terminal = Terminal()
max_y, max_x = terminal.get_max_y_and_x()
sys.setrecursionlimit(max_y * max_x)
game_play = GamePlay(max_y, max_x)
changes = game_play.init_borders_on_game_field()
terminal.print_changes(changes)
changes = game_play.init_enemy_and_hero_on_game_field()
terminal.print_changes(changes)
try:
pressed_key = terminal.get_pressed_key()
while pressed_key != ESCAPE_KEY:
motion_direction = pressed_key_to_motion_direction[pressed_key]
changes = game_play.make_progress(motion_direction)
terminal.print_changes(changes)
time.sleep(1 / 20)
pressed_key = terminal.get_pressed_key()
except KeyboardInterrupt:
terminal.destroy('Exit from game')
except GameOverError:
terminal.destroy('You lose, game over')
except GameWinError:
terminal.destroy('You win! Congratulations!')
finally:
terminal.destroy()
if __name__ == '__main__':
main()