-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscreen.py
430 lines (348 loc) · 14.6 KB
/
screen.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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
from shutil import get_terminal_size
from time import sleep
import logging
import os
from typing import Tuple
from rich.panel import Panel
from rich.align import Align
from rich.table import Table
from rich.console import Console
from rich.layout import Layout
from art import text2art
from settings import Settings
import keyboard
from utils import Utils, Const
import game_objects
# Set up the logger
logger = logging.getLogger(__name__)
test_art = (r'''
#####################|
####################/
###################| ___________
##################/_______/```````````\______________
#############___/`````````````````````````````"""""""|
############/ ``````````````````````""""""""""""""" |
############|```````````````""""""""""""""""""" __/
###########_/\_____```````"""""""________________/
##########/ \____________/
#######/
##/
~
~ ~ ~ ~ ~
~ ~ ~
__
``\__ ~ ~ ##
`````| ~ #####
"___/ ~ #### # #
#####
# ###
## #
## #
#
#
_||
, ._/ /---+-\_
#=\ # /#_,/~~~ |\.
,___ \/ /# / /_ __ __ /#'|
# \__#/ /___# / | __ __\
/ \\/# \*` / [][ ] | / | | ##
` /==|\ | | T__ | \._/|
# /|*\ =L _ _ _|__ \_ |
|*| ` /_______/| `,__./==\|
|#| ||=======||\___ __._ _|
________ _____/##|________||=======|| \ ` _` \
|/|=|=|=* #=|/|/###\|*|=|==|||=======|| | | | |
. `
" ` , " ` " '
' ' ''')
class Screen:
def __init__(self):
self.con = Console(style="yellow on black")
self.layout = Layout()
self.con.height = Settings.console_height
self.twidth = get_terminal_size()[0]
self.theight = get_terminal_size()[1]
logger.info(f"{self.__class__.__name__} initialized")
def show(self):
Utils.transition()
# Set to current height in settings
self.con.height = Settings.console_height
logger.info(f'Drawing {self.__class__.__name__}')
self.con.print(self.layout)
def resize_if_needed(self):
current_x, current_y = get_terminal_size()
if (current_x, current_y) != (self.twidth, self.theight):
os.system(Settings.clear_cmd)
self.con.print(self.layout)
class MenuWindow(Screen):
def __init__(self, opt1: str, opt2: str, opt3: str, opt4: str):
super(MenuWindow, self).__init__()
self.opt1 = Panel(
Align(text2art(f"1. {opt1}", font=Settings.font), align="center", vertical="middle"))
self.opt2 = Panel(
Align(text2art(f"2. {opt2}", font=Settings.font), align="center", vertical="middle"))
self.opt3 = Panel(
Align(text2art(f"3. {opt3}", font=Settings.font), align="center", vertical="middle"))
self.opt4 = Panel(
Align(text2art(f"4. {opt4}", font=Settings.font), align="center", vertical="middle"))
self.layout.split_row(
Layout(Const.sidebar, ratio=1),
Layout(name="Center", ratio=2),
Layout(Const.sidebar, ratio=1)
)
self.layout["Center"].split_column(
Layout(self.opt1),
Layout(self.opt2),
Layout(self.opt3),
Layout(self.opt4)
)
def listen(self) -> str:
logger.info(f"{self.__class__.__name__} Listening for user input")
# Listen for user input and if it matches a key, return it
while True:
# Resize window
self.resize_if_needed()
# Wait for keyboard input
key = keyboard.read_key(suppress=True)
usable_keys = ["esc", "1", "2", "3", "4"]
if key in usable_keys:
logger.debug(f"User pressed {key}")
return key
class PauseDisplay(MenuWindow):
def __init__(self):
super(PauseDisplay, self).__init__("Resume", "Options", "Stats", "Quit Game")
class OptionsDisplay(MenuWindow):
def __init__(self):
super(OptionsDisplay, self).__init__("Height", "Font", "Color",
"Back") # Pass in menu options any MenuWindow subclass
class HeightOptions(MenuWindow):
def __init__(self):
super(HeightOptions, self).__init__("50", "55", "60",
"65") # Pass in menu options any MenuWindow subclass
class ItemOptions(MenuWindow):
def __init__(self):
super(ItemOptions, self).__init__("Use Item", "Drop Item", "Item Info", "Back")
class ActiveGameDisplay(Screen):
hud = f"""
Cover: 60 Actions: O O O
Health: 100 Movement: 2
Ammo: 6/28 Enemies Visible: 2
Armor: 23 Concealed: Y
Inventory: 'I' | Character: 'I' | Help: 'H' | Line of Sight: 'S'
"""
game_map = ""
def __init__(self):
super(ActiveGameDisplay, self).__init__()
# Split to three rows
self.layout.split_row(
Layout(Const.sidebar, ratio=1),
Layout(name="Center", ratio=2),
Layout(Const.sidebar, ratio=1)
)
# Split the center into 2. GameMap on top and HUD on the bottom
self.layout["Center"].split_column(
Layout(Panel(Align(
ActiveGameDisplay.game_map, align="center", vertical="top"),
title_align="center", title=f"4th St. Library", padding=2), name="MAP", ratio=2),
Layout(Panel(ActiveGameDisplay.hud, title=f"Buster Scruggs: Level 12",
title_align="center"), name="HUD",
size=8)
)
def listen(self) -> str:
logger.info(f"{self.__class__.__name__}lassname Listening for user input")
# Listen for user input and if it matches a key, return it
while True:
# Resize window
self.resize_if_needed()
# Wait for keyboard input
key = keyboard.read_key(suppress=True)
usable_keys = ["esc", "1", "2", "3", "4", "i", "m", "c"]
if key in usable_keys:
logger.debug(f"User pressed {key}")
return key
def load_map(self, level):
"""Loads map intro file, sleeps, then loads the actual map file"""
self.layout["MAP"].update(
Panel(Align(level.intro_view, align="center", vertical="middle"), title_align="center",
title=f"{level.name}",
padding=2))
self.show()
sleep(5)
self.layout["MAP"].update(
Panel(Align(level.level_view, align="center", vertical="middle"), title_align="center",
title=f"{level.name}", padding=2))
self.show()
class InventoryDisplay(Screen):
def __init__(self, table_data: Tuple[Table, list]): # TODO Type hint tuple
super(InventoryDisplay, self).__init__()
for item_row in table_data[1]:
if "-->" in item_row:
item_panel = Panel(item_row[-1], padding=1)
self.layout.split_row(
Layout(item_panel, ratio=1),
Layout(
Panel(table_data[0], title_align="center", title=f"Inventory", expand=True, padding=1),
name="INV", ratio=2),
Layout(Const.sidebar, ratio=1)
)
def redraw_inv(self, table_data: Tuple[Table, list]):
for item_row in table_data[1]:
if "-->" in item_row:
item_panel = Panel(item_row[-1], padding=1)
logger.info("Redrawing inventory table")
self.layout.split_row(
Layout(item_panel, ratio=1),
Layout(Panel(table_data[0], title_align="center", title=f"Inventory", expand=True, padding=1),
name="INV", ratio=2),
Layout(Const.sidebar, ratio=1)
)
self.show()
def listen(self) -> str:
logger.info(f"{self.__class__.__name__} Listening for user input")
# Listen for user input and if it matches a key, return it
while True:
# Resize window
self.resize_if_needed()
# Wait for keyboard input
key = keyboard.read_key(suppress=True)
usable_keys = ["esc", "1", "2", "3", "4", "i", "up", "down", "enter"]
if key in usable_keys:
logger.debug(f"User pressed {key}")
return key
class WorldMapDisplay(Screen):
"""Add map: GameMap obj to init"""
def __init__(self):
super(WorldMapDisplay, self).__init__()
self.layout.split_row(
Layout(
Panel(
Align(f"[white]{text2art('World GameMap', font='big')}[/white]", align="center",
vertical="middle")),
ratio=4),
Layout(Panel(Align(f"[white]{text2art('LOC', font='big')}[/white]", align="center")),
ratio=1)
)
def listen(self) -> str:
logger.info(f"{self.__class__.__name__} Listening for user input")
# Listen for user input and if it matches a key, return it
while True:
# Resize window
self.resize_if_needed()
# Wait for keyboard input
key = keyboard.read_key(suppress=True)
usable_keys = ["esc", "m", "l"]
if key in usable_keys:
logger.debug(f"User pressed {key}")
return key
class LocalMapDisplay(Screen):
def __init__(self):
super(LocalMapDisplay, self).__init__()
self.layout.split_row(
Layout(Panel(
Align(f"[white]{text2art('Local Area GameMap', font='big')}[/white]",
align="center",
vertical="middle")),
ratio=4),
Layout(Panel(Align(f"[white]{text2art('LOC', font='big')}[/white]", align="center")),
ratio=1)
)
def listen(self) -> str:
logger.info(f"{self.__class__.__name__} Listening for user input")
# Listen for user input and if it matches a key, return it
while True:
# Resize window
self.resize_if_needed()
# Wait for keyboard input
key = keyboard.read_key(suppress=True)
usable_keys = ["esc", "m", "l"]
if key in usable_keys:
logger.debug(f"User pressed {key}")
return key
class CharacterStatus(Screen):
def __init__(self):
super(CharacterStatus, self).__init__()
self.layout.split_column(
Layout(Panel("Top"), ratio=1, name="Top"),
Layout(Panel("Character Status/Level info"), ratio=4, name="Bottom")
)
self.layout["Top"].split_row(
Layout(
Panel(Align(f"[white]{text2art('Loadout', font='big')}[/white]", align="center")),
ratio=1,
name="TopLeft"),
Layout(Panel(Align(f"[white]{text2art('Status', font='big')}[/white]", align="center")),
ratio=1,
name="TopRight"),
)
self.layout["Bottom"].split_row(
Layout(Panel("Left"), ratio=1, name="Left"),
Layout(Panel("Right"), ratio=1, name="Right")
)
self.layout["Left"].split_row(
Layout(Panel("Player Picture Here"), ratio=1),
Layout(Panel("Equipment"), ratio=1)
)
self.layout["Right"].split_row(
Layout(Panel("Health\nStam\nStr\nDex"), ratio=1),
Layout(Panel("Budds/Debuffs"), ratio=1)
)
def listen(self) -> str:
logger.info(f"{self.__class__.__name__} Listening for user input")
# Listen for user input and if it matches a key, return it
while True:
# Resize window
self.resize_if_needed()
# Wait for keyboard input
key = keyboard.read_key(suppress=True)
usable_keys = ["esc", "c"]
if key in usable_keys:
logger.debug(f"User pressed {key}")
return key
class SplashScreen(Screen):
def __init__(self):
super(SplashScreen, self).__init__()
self.layout.split_column(
Layout(Panel(
Align(text2art("*=* Wasted Lands *=*", font="big"), align="center")),
ratio=1, name="Top"),
Layout(Panel(Align(text2art(
f"Produced by Aldo\nCreated by Odin\nPublished by GWA",
font='big'), align="center")), ratio=4, name="Bottom")
)
def show(self):
Utils.transition()
logger.info(f"Drawing {self.__class__.__name__}")
self.con.print(self.layout)
sleep(3)
return
class MainMenu(Screen):
def __init__(self):
super(MainMenu, self).__init__()
self.layout.split_row(
Layout(Panel(test_art), ratio=1),
Layout(Panel(""), name="Middle", ratio=2),
Layout(Panel(test_art), ratio=1)
)
self.layout["Middle"].split_column(
Layout(Panel(Align(text2art("Wasted Lands", font="big"), align="center")), ratio=1,
name="Top"),
Layout(Panel(""), ratio=4, name="Bottom")
)
self.layout["Bottom"].split_column(
Layout(Panel(Align(text2art("New Game", font="big"), align="center"))),
Layout(Panel(Align(text2art("Load Game", font="big"), align="center"))),
Layout(Panel(Align(text2art("Options", font="big"), align="center"))),
Layout(Panel(Align(text2art("Quit Game", font="big"), align="center")))
)
def listen(self) -> str:
logger.info(f"{self.__class__.__name__} Listening for user input")
# Listen for user input and if it matches a key, return it
while True:
# Resize window
self.resize_if_needed()
# Wait for keyboard input
key = keyboard.read_key(suppress=True)
usable_keys = ["esc", "1", "2", "3", "4"]
if key in usable_keys:
logger.debug(f"User pressed {key}")
return key