-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathwindow.py
More file actions
284 lines (225 loc) · 9.48 KB
/
window.py
File metadata and controls
284 lines (225 loc) · 9.48 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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import urwid
class MainWindow:
##(name, foreground, background, mono, foreground_high, background_high)
palette = [
('body', '', '', '', '#fff', '#000'),
('footer', '', '', '', '#fff', '#000'),
('header', '', '', '', '#fff', '#000'),
('white', '', '', '', '#fff', '#000'),
('brown', '', '', '', '#d80', '#000'),
('yellow', '', '', '', '#ff0', '#000'),
('red', '', '', '', '#f00', '#000'),
('flash brown', '', '', 'bold', '#d80,standout', '#000'),
('flash yellow', '', '', 'bold', '#ff0,standout', '#000'),
('flash red', '', '', 'bold', '#f00,standout', '#000'),
('light white', '', '', 'bold', '#fff', '#000'),
('light blue', '', '', 'bold', '#0af', '#000'),
('white blue', '', '', '', '#adf', '#000'),
]
def __init__(self, game):
self.game = game
self.game.initialize_player()
self.header_text = urwid.Text(u"Top\nheader")
self.header = urwid.AttrWrap(self.header_text, 'header')
self.footer_text = urwid.Text(u"")
self.footer = urwid.AttrWrap(self.footer_text, 'footer')
# self.cell_txt = urwid.Text("")
# self.pathogen_txt = urwid.Text("")
# self.player_col = urwid.Filler(self.cell_txt, valign='top', height='pack')
# self.pathogen_col = urwid.Filler(self.pathogen_txt, valign='top', height='pack')
# self.body = urwid.Columns([urwid.LineBox(self.player_col), urwid.LineBox(self.pathogen_col)])
self.area = GameArea()
self.area.mainloop = self
body = self.area.board_overview()
self.view = MainFrame(header=self.header, body=body, footer=self.footer, focus_part='body')
self.view.mainloop = self
self.last_alarm = None
def main(self):
#self.loop = urwid.MainLoop(self.view, self.palette, input_filter=self.input_filter) #, pop_ups=True)
self.loop = urwid.MainLoop(self.view, self.palette, pop_ups=True)
self.loop.main_window = self
#self.loop.mwin = self
self.loop.screen.set_terminal_properties(colors=256)
self.loop.screen.register_palette(self.palette)
### TODO: check that constantly adding alerts doesn't leak mem
# 1-second game tick
self.last_alarm = self.loop.set_alarm_in(0, callback=self.game_tick)
# 10-second game tick
#self.loop.set_alarm_in(0, callback=self.protein_gain)
self.loop.run()
def write_header(self):
header_txt = "Ticks: %s\nProteins: %s\nLevel: %s\n" % (self.game.ticks, self.game.proteins, self.game.level)
self.header_text.set_text(header_txt)
def write_footer(self):
#footer_txt = self.game.display_log()
self.footer_text.set_text("")
def draw_cells(self):
cell_str = []
for i in self.game.player_cells:
cell_str.append((i.palette, i.symbol))
if cell_str:
self.area.cells.set_text(cell_str)
def draw_pathogens(self):
pathogen_str = []
for i in self.game.pathogens:
pathogen_str.append((i.palette, i.symbol))
if pathogen_str:
self.area.pathogens.set_text(pathogen_str)
def draw_cells_desc(self):
content = []
for i in self.game.player_cells:
content.append((i.palette, i.symbol))
content.append(('body', ": %s (age: %s)" % (i.name, i.age)))
content.append(('body', "[ATK/DEF: %s/%s]" % (i.base_attack, i.defense)))
content.append(('body', '\n'))
if content:
self.area.cells_txt.set_text(content)
def draw_pathogens_desc(self):
content = []
for i in self.game.pathogens:
content.append((i.palette, i.symbol))
content.append(('body', ": %s (age: %s)" % (i.name, i.age)))
content.append(('body', "[ATK/DEF: %s/%s]" % (i.base_attack, i.defense)))
content.append(('body', '\n'))
if content:
self.area.pathogens_txt.set_text(content)
def game_tick(self, loop, user_data):
# So we have a single alarm object at all times?
self.loop.remove_alarm(self.last_alarm)
# If the game is paused, skip these events
if not self.game.paused:
# Player attacks
#self.game.player_attack(self)
# Cell actions
for i in self.game.player_cells:
i.heartbeat(self)
# Pathogens actions
for i in self.game.pathogens:
i.heartbeat(self)
# Gain a protein
self.protein_gain(1)
self.game.check_level_state()
self.game.age_all_cells()
self.game.ticks += 1
self.write_header()
self.draw_cells()
self.draw_cells_desc()
self.draw_pathogens()
self.draw_pathogens_desc()
self.write_footer()
self.last_alarm = self.loop.set_alarm_in(sec=1, callback=self.game_tick)
def protein_gain(self, amount=0):
self.game.proteins += amount
class MainFrame(urwid.Frame):
active_window = None
def keypress(self, size, key):
#self.mainloop.game.log("key pressed: %s" % key)
if key == 'q':
raise urwid.ExitMainLoop()
elif key == 'esc':
raise urwid.ExitMainLoop()
elif key == 'n':
self.mainloop.game.spawn_cell()
self.mainloop.write_header()
elif key == 'd':
self.purchase_pop_up()
elif key == ' ':
if self.mainloop.game.paused:
self.mainloop.game.log("Game unpaused.")
self.mainloop.game.paused = False
else:
self.mainloop.game.log("Game paused.")
self.mainloop.game.paused = True
elif key == 'up':
#self.mainloop.game.log("key pressed: %s" % key)
body = self.mainloop.area.lymphatic_view()
self.mainloop.view.contents['body'] = (body, None)
self.mainloop.loop.draw_screen()
elif key == 'down':
#self.mainloop.game.log("key pressed: %s" % key)
body = self.mainloop.area.board_overview()
self.mainloop.view.contents['body'] = (body, None)
self.mainloop.loop.draw_screen()
elif key == 'm':
# Pause the game while in log view
self.mainloop.game.paused = True
self.mainloop.game.log("Current focus: %s" % self.get_focus_widgets())
body = self.mainloop.area.log_view()
self.mainloop.view.contents['body'] = (body, None)
self.mainloop.loop.draw_screen()
#self.lymph_window()
#def lymph_window(self):
# lymph_txt = urwid.Text("Here would be details about the lymphatic system, the rate of WBC generation and the like.")
# box = urwid.LineBox(urwid.Filler(lymph_txt))
# overlay = urwid.Overlay(
# top_w = box,
# bottom_w = self,
# align = 'left',
# width = 40,
# valign = 'middle',
# height = 40,
# #left = 5,
# )
# self.mainloop.loop.widget = overlay
def purchase_pop_up(self):
question = urwid.Text(("bold", "A pop-up!"), "center")
prompt = urwid.LineBox(urwid.Filler(question))
overlay = OptionOverlay(
top_w = prompt,
bottom_w = self,
align = 'left',
width = 10,
valign = 'middle',
height = 6,
left = 5,
)
self.mainloop.loop.widget = overlay
class OptionOverlay(urwid.Overlay):
def selectable(self):
return True
def keypress(self, size, key):
if key == 'q':
raise urwid.ExitMainLoop()
elif key == 'enter':
self.bottom_w.mainloop.loop.widget = self.bottom_w
class GameArea:
def __init__(self):
self.board_overview()
def selectable(self):
return True
def keypress(self, size, key):
pass
#self.mainloop.game.log("key pressed: %s" % key)
def board_overview(self):
self.cells = urwid.Text("")
self.pathogens = urwid.Text("")
self.cells_txt = urwid.Text("")
self.pathogens_txt = urwid.Text("")
self.cells_col = urwid.Pile([
(10, urwid.LineBox(urwid.Filler(self.cells), title='White Blood Cells')),
urwid.LineBox(urwid.Filler(self.cells_txt, valign='top'), title='Cell Details'),
])
self.pathogens_col = urwid.Pile([
(10, urwid.LineBox(urwid.Filler(self.pathogens), title='Pathogens')),
urwid.LineBox(urwid.Filler(self.pathogens_txt, valign='top'), title='Pathogen Details'),
])
body = urwid.AttrWrap(urwid.Columns([self.cells_col, self.pathogens_col]), 'body')
return body
def lymphatic_view(self):
lymph_txt = urwid.Text("Here would be details about the lymphatic system, the rate of WBC generation and the like.")
box = urwid.LineBox(urwid.Filler(lymph_txt))
body = urwid.AttrWrap(box, 'body')
return body
def log_view(self):
footer_txt = self.mainloop.game.display_log()
footer = urwid.Text(footer_txt)
box = LogBody(urwid.Filler(footer))
body = urwid.AttrWrap(box, 'body')
return body
# test class to override keypresses in the body of MainFrame for specific windows
class LogBody(urwid.LineBox):
def selectable(self):
return True
def keypress(self, size, key):
if key == 'esc':
raise urwid.ExitMainLoop()