Files
ages/agame/__main__.py
Alek Ratzloff e868d0e14f Add display abstraction
In case we want to run this on something that isn't an ANSI terminal, we
have the option to implement it however we want.

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2021-11-20 19:38:06 -08:00

57 lines
1.2 KiB
Python

import argparse
import importlib.util
from pathlib import Path
import sys
from agame.color import colorize
from agame.display import ANSIDisplay
from agame.game import Game
# Parse args
parser = argparse.ArgumentParser(
description="Run a game",
)
parser.add_argument(
"PACKAGE", type=str, help="The Python package that contains the game to run."
)
args = parser.parse_args()
# Get package to run
package = args.PACKAGE
# Load game module
game_module = importlib.import_module(package)
database = game_module.database # type: ignore
start_room = game_module.start_room # type: ignore
# Build the game state
game = Game(
display=ANSIDisplay(),
database=database,
room=database.rooms["prelude"],
)
try:
# Run
game.say("=" * 70)
game.say("Type ((help)) for help, or ((quit)) to exit.")
game.say("=" * 70)
game.say()
game.do_teleport_actions()
while True:
game.say()
line = game.display.input()
if line.lower() == "quit":
break
game.say()
game.run_command(line)
except (KeyboardInterrupt, EOFError):
# no-op, these are expected events
pass
finally:
game.display.finish()
game.say()
game.say("Bye.")