1
0
python-roguelike/main.py

71 lines
2.1 KiB
Python
Raw Normal View History

2022-01-06 11:09:43 -05:00
#!/usr/bin/env python3
import traceback
2022-01-07 15:52:53 -05:00
2022-01-06 11:09:43 -05:00
import tcod
2022-01-11 16:08:57 -05:00
import color
2022-01-18 14:04:05 -05:00
import exceptions
import input_handlers
2022-01-18 14:37:48 -05:00
import 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.")
2022-01-06 11:09:43 -05:00
def main() -> None:
screen_width = 80
screen_height = 50
tileset = tcod.tileset.load_tilesheet(
"dejavu10x10_gs_tc.png",
32,
8,
tcod.tileset.CHARMAP_TCOD
)
2022-01-18 14:37:48 -05:00
handler: input_handlers.BaseEventHandler = setup_game.MainMenu()
2022-01-18 14:04:05 -05:00
2022-01-06 11:09:43 -05:00
with tcod.context.new_terminal(
2022-01-06 13:27:15 -05:00
screen_width,
screen_height,
tileset=tileset,
title="Yet Another Roguelike Tutorial",
vsync=True,
2022-01-06 11:09:43 -05:00
) as context:
root_console = tcod.Console(screen_width, screen_height, order="F")
2022-01-18 14:04:05 -05:00
try:
while True:
root_console.clear()
2022-01-18 14:37:48 -05:00
handler.on_render(console=root_console)
2022-01-18 14:04:05 -05:00
context.present(root_console)
try:
for event in tcod.event.wait():
context.convert_event(event)
2022-01-18 14:37:48 -05:00
handler = handler.handle_events(event)
2022-01-18 14:04:05 -05:00
except Exception: # Handle exceptions in game.
traceback.print_exc() # Print error to stderr.
# Then print the error to the message log.
2022-01-18 14:37:48 -05:00
if isinstance(handler, input_handlers.EventHandler):
handler.engine.message_log.add_message(
traceback.format_exc(),
color.error,
)
2022-01-18 14:04:05 -05:00
except exceptions.QuitWithoutSaving:
raise
except SystemExit: # Save and quit.
2022-01-18 14:37:48 -05:00
save_game(handler, "savegame.sav")
2022-01-18 14:04:05 -05:00
raise
except BaseException: # Save on any other unexpected exception.
2022-01-18 14:37:48 -05:00
save_game(handler, "savegame.sav")
2022-01-18 14:04:05 -05:00
raise
2022-01-06 11:09:43 -05:00
if __name__ == "__main__":
main()