-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
53 lines (48 loc) · 1.64 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
# Python packages
import os, pickle
# Our packages
import tests
from game.constants import SAVE_LOCATION
from game.gamestate import GameState
from game.utils import capture_input
def play(args):
"""
Setup and starts the game
"""
if args.load:
try:
with open(SAVE_LOCATION, 'r') as f:
game = pickle.load(f)
except IOError:
print 'No saved games found.'
return
if game.finished:
user_input = capture_input(
"You completed the game! Would you like to start a new game? (y/n) "
)
if user_input[0] == 'y':
game = GameState()
else:
print 'Playing on saved file...'
else:
game = GameState()
game.run()
def test(args):
tests.run()
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(description="""
Welcome to your text-based adventure game! This file will allow you to play the
game you have developed in the project. Available commands are provided below.
Call `python main.py {command} -h` to view the help screen for the given command.
""")
commands = parser.add_subparsers(title='Available commands')
play_parser = commands.add_parser('play', description='Start the game')
play_parser.add_argument(
'-l', '--load', help='Load a saved game', action='store_true'
)
play_parser.set_defaults(func=play)
test_parser = commands.add_parser('test', description='Test code against unit tests')
test_parser.set_defaults(func=test)
args = parser.parse_args()
args.func(args)