__main__.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. """Offer Adventure at a custom command prompt.
  2. Copyright 2010-2015 Brandon Rhodes. Licensed as free software under the
  3. Apache License, Version 2.0 as detailed in the accompanying README.txt.
  4. """
  5. import argparse
  6. import os
  7. import re
  8. import readline
  9. from sys import executable, stdout
  10. from time import sleep
  11. from . import load_advent_dat
  12. from .game import Game
  13. BAUD = 1200
  14. def baudout(s):
  15. for c in s:
  16. sleep(9. / BAUD) # 8 bits + 1 stop bit @ the given baud rate
  17. stdout.write(c)
  18. stdout.flush()
  19. def loop():
  20. parser = argparse.ArgumentParser(
  21. description='Adventure into the Colossal Caves.',
  22. prog='{} -m adventure'.format(os.path.basename(executable)))
  23. parser.add_argument(
  24. 'savefile', nargs='?', help='The filename of game you have saved.')
  25. args = parser.parse_args()
  26. if args.savefile is None:
  27. game = Game()
  28. load_advent_dat(game)
  29. game.start()
  30. baudout(game.output)
  31. else:
  32. game = Game.resume(args.savefile)
  33. baudout('GAME RESTORED\n')
  34. while not game.is_finished:
  35. line = input('> ')
  36. words = re.findall(r'\w+', line)
  37. if words:
  38. baudout(game.do_command(words))
  39. try:
  40. loop()
  41. except EOFError:
  42. pass