|
| 1 | +"""(0, 0) - the square where the game starts and ends.""" |
| 2 | + |
| 3 | +from agps.utils import action_prompt, move, take_words |
| 4 | + |
| 5 | +class GameWon(Exception): |
| 6 | + """Abuse exception handling to escape the game loop when we finish.""" |
| 7 | + pass |
| 8 | + |
| 9 | +scene_contents = dict(keys_used = 0, |
| 10 | + rope = 1, |
| 11 | + ) |
| 12 | + |
| 13 | +def use_key(inventory): |
| 14 | + if inventory['golden key'] > 0: |
| 15 | + inventory['golden key'] -= 1 |
| 16 | + scene_contents['keys_used'] += 1 |
| 17 | + if scene_contents['keys_used'] == 5: |
| 18 | + raise GameWon |
| 19 | + print("You put the key in and turn it carefully. " |
| 20 | + "%s more to go." % 5 - scene_contents['keys_used']) |
| 21 | + else: |
| 22 | + print("You don't have a golden key to use.") |
| 23 | +use = {'use', 'insert'} |
| 24 | + |
| 25 | +def pick_up_rope(inventory): |
| 26 | + if scene_contents['rope'] > 0: |
| 27 | + inventory['rope'] += 1 |
| 28 | + scene_contents['rope'] -= 1 |
| 29 | + print("You put the rope in your bag. That could come in useful.") |
| 30 | + else: |
| 31 | + print("You already collected the rope.") |
| 32 | + |
| 33 | +def enter(name, inventory): |
| 34 | + print("You are in a sizeable clearing in the forest. At your feet, you see " |
| 35 | + "a mysterious trap door, with five golden keyholes. Paths lead off " |
| 36 | + "in four directions, which your unerring sense of direction tells you " |
| 37 | + "correspond to the four compass points.") |
| 38 | + if scene_contents.get('rope'): |
| 39 | + print("A short length of rope is lying on the floor.") |
| 40 | + print() |
| 41 | + while True: |
| 42 | + action = action_prompt() |
| 43 | + if action[0] is move: |
| 44 | + return action[1] |
| 45 | + if (action[0] in use) and ('key' in action): |
| 46 | + use_key(inventory) |
| 47 | + elif (action[0] in take_words) and ('rope' in action): |
| 48 | + pick_up_rope(inventory) |
0 commit comments