-
Notifications
You must be signed in to change notification settings - Fork 0
/
nnuklear.py
executable file
·192 lines (150 loc) · 4.79 KB
/
nnuklear.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# -*- coding: utf-8 -*-
import curses
from typing import Any
# game modules
import nuklear.engine as engine
import nuklear.lang as lang
from nuklear.parser import Level
t = lang.i18n.t
def winboard(parent: Any, pair: int, win_status_height: int = 3) -> Any:
y = int(((parent.getmaxyx()[0]) - engine.MAX_Y) / 2) - win_status_height
x = int((parent.getmaxyx()[1] - engine.MAX_X + 2) / 2)
if y < 0:
y = 0
win = curses.newwin(engine.MAX_Y + 2, engine.MAX_X + 2, y, x)
win.bkgd(' ', curses.color_pair(pair))
win.box()
return win
def winstatus(parent: Any, pair: int) -> Any:
maxy, maxx = parent.getmaxyx()
y = maxy - 3
x = 0
win = curses.newwin(3, maxx, y, x)
win.bkgd(' ', curses.color_pair(pair))
win.box()
return win
def question(parent, pair: int, string: str) -> bool:
maxy, maxx = parent.getmaxyx()
w = len(string) + 4
h = 3
y = int((maxy - h) / 2)
x = int((maxx - w) / 2)
win = parent.derwin(h, w, y, x)
win.bkgd(' ', curses.color_pair(pair))
win.box()
win.addstr(1, 2, string)
win.refresh()
answer = None
while True:
key = win.getkey()
if key in (t('nnuklear.yes_lower'), t('nnuklear.yes_upper')):
answer = True
elif key in (t('nnuklear.no_lower'), t('nnuklear.no_upper')):
answer = False
if answer is not None:
break
win.clear()
return answer
def winending(parent: Any, pair: int) -> None:
maxy, maxx = parent.getmaxyx()
msg = t('nnuklear.end_game')
info = t('nnuklear.exit_key')
w = max([len(msg), len(info)]) + 4 # frame + space on left and on right - 2+2=4 :)
h = 4
y = int((maxy - h) / 2)
x = int((maxx - w) / 2)
win = parent.subwin(h, w, y, x)
win.bkgd(' ', curses.color_pair(pair))
win.box()
msg_x = int((w - len(msg)) / 2)
info_x = int((w - len(info)) / 2)
win.addstr(1, msg_x, msg)
win.addstr(2, info_x, info)
win.refresh()
while True:
if win.getkey() == ' ':
break
def update_status(win: Any, lvl_number: int, moves: int, title: str = '', author: str = '') -> None:
_, maxx = win.getmaxyx()
y = 1
win.addstr(y, 2, '| ' + t('nnuklear.level') + f': {lvl_number:3d}')
win.addstr(y, 14, f'| {title:^24}')
x = maxx - 14
win.addstr(y, x, '| ' + t('nnuklear.moves') + f': {moves:3d}')
win.refresh()
def update_board(win: Any, lvl: Level) -> None:
for y in range(engine.MAX_Y):
for x in range(engine.MAX_X):
elem = lvl.board.get(x, y)
wx = x + 1
wy = y + 1
if elem == engine.Object.EMPTY:
win.addch(wy, wx, ' ')
elif elem == engine.Object.WALL:
win.addch(wy, wx, '#')
elif elem == engine.Object.CONTAINER:
win.addch(wy, wx, 'o')
elif elem == engine.Object.DESTROYER:
win.addch(wy, wx, '.')
elif elem == engine.Object.PLAYER:
win.addch(wy, wx, '&')
win.refresh()
def main(stdscr: Any) -> None:
curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_BLUE)
curses.init_pair(2, curses.COLOR_WHITE, curses.COLOR_BLACK)
wboard = wstaus = None
def drawwins():
stdscr.clear()
stdscr.refresh()
wboard = winboard(stdscr, 1)
wstatus = winstatus(stdscr, 1)
return wboard, wstatus
wboard, wstatus = drawwins()
wboard.refresh()
wstatus.refresh()
e = engine.Engine(pln_file='nuklear.pln')
e.parsefile()
e.start()
def update():
update_board(wboard, e.current_level)
update_status(wstatus, e.current_level.number, e.moves, e.current_level.title, e.current_level.author)
update()
while True:
if e.is_end():
if e.is_game_over():
winending(stdscr, 2)
break
if not e.next_level():
continue
e.start()
update()
key = stdscr.getkey()
if key == 'KEY_UP':
ret = e.player_move_up()
update()
elif key == 'KEY_DOWN':
ret = e.player_move_down()
update()
elif key == 'KEY_LEFT':
ret = e.player_move_left()
update()
elif key == 'KEY_RIGHT':
ret = e.player_move_right()
update()
elif key == 'KEY_RESIZE':
wboard, wstatus = drawwins()
update()
elif key == 'r':
e.start()
update()
elif key == 'q':
if question(stdscr, 2, t('nnuklear.exit_info')):
break
else:
wboard, wstatus = drawwins()
update()
if __name__ == '__main__':
try:
curses.wrapper(main)
except KeyboardInterrupt:
pass