-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
74 lines (63 loc) · 2.35 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
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python3
'''
This is the main test code for the maturam game
'''
import traceback
import tcod
import colours
import exceptions
import setup_game
import input_handlers
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() -> None: # pylint: disable=R0914
'''
This is the main function
'''
screen_width = 80
screen_height = 50
tileset = tcod.tileset.load_tilesheet(
'images/dejavu10x10_gs_tc.png',
32,
8,
tcod.tileset.CHARMAP_TCOD
)
handler: input_handlers.BaseEventHandler = setup_game.MainMenu()
with tcod.context.new(
columns=screen_width,
rows=screen_height,
tileset=tileset,
title="Yet Another Roguelike Tutorial",
vsync=True,
) as context:
root_console = tcod.Console(screen_width, screen_height, order="F")
try:
while True:
root_console.clear()
handler.on_render(console=root_console)
context.present(root_console)
try:
for event in tcod.event.wait():
context.convert_event(event)
handler = handler.handle_events(event)
except Exception: # pylint: disable=W0703
# 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(), colours.ERROR
)
except exceptions.QuitWithoutSaving:
raise
except SystemExit: # Save and quit.
save_game(handler, "saves/savegame.sav")
raise
except BaseException: # Save on any other unexpected exception.
save_game(handler, "saves/savegame.sav")
raise
if __name__ == "__main__":
main()