Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
84 lines (64 loc) · 2.66 KB
/
Copy pathmain.py
File metadata and controls
84 lines (64 loc) · 2.66 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
#!E:\Alejandro\Python\venv\roguelike_tutorial\Scripts\python
import traceback
import tcod
import os
import tcod.libtcodpy
import scripts.game_data
import scripts.color as color
import scripts.exceptions as exceptions
import scripts.input_handlers as input_handlers
import scripts.setup_game as setup_game
def save_game(handler: input_handlers.BaseEventHandler, filename: str) -> None:
"""If the current event handler has an active Engine then save it."""
if isinstance(handler, input_handlers.EventHandler):
handler.engine.save_as(filename)
print("Game saved.")
def main():
screen_width = scripts.game_data.screen_width
screen_height = scripts.game_data.screen_height
os.environ["SDL_RENDER_SCALE_QUALITY"] = "nearest"
tileset = tcod.tileset.load_tilesheet(
"resources/terminal16x16_gs_ro.png", 16, 16, tcod.tileset.CHARMAP_CP437
)
#tileset = tcod.tileset.load_truetype_font(
# "resources/PxPlus_HP_100LX_16x12.ttf", 16, 16
#)
handler: input_handlers.BaseEventHandler = setup_game.MainMenu()
with tcod.context.new(
columns=screen_width,
rows=screen_height,
tileset=tileset,
title="Crypts of the Embered",
vsync=True,
) as context:
console_width, console_height = context.recommended_console_size()
root_console = tcod.console.Console(
console_width, console_height, order="F"
)
context.present(root_console, keep_aspect=True, integer_scaling=True)
try:
while True:
root_console.clear(bg=color.console_bg)
handler.on_render(console=root_console)
context.present(root_console, keep_aspect=True, integer_scaling=True)
try:
for event in tcod.event.wait():
context.convert_event(event)
handler = handler.handle_events(event)
except Exception: # Handle exceptions in game.
traceback.print_exc() # Print error to stderr.
# Then print the error to the message log.
if isinstance(handler, input_handlers.EventHandler):
handler.engine.message_log.add_message(
traceback.format_exc(), color.error
)
except exceptions.QuitWithoutSaving:
raise
except SystemExit: # Save and quit.
save_game(handler, "savegame.sav")
raise
except BaseException: # Save on any other unexpected exception.
save_game(handler, "savegame.sav")
raise
if __name__ == "__main__":
main()