|
| 1 | +# Write a text adventure that allows the player to move from room to room by |
| 2 | +# typing "n", "w", "s", or "e" for north, west, south, and east. |
| 3 | + |
| 4 | +import textwrap |
| 5 | + |
| 6 | +# These are the existing rooms. Add more as you see fit. |
| 7 | + |
| 8 | +rooms = { |
| 9 | + "outside": { |
| 10 | + "name": "Outside Cave Entrance", |
| 11 | + "description": "North of you, the cave mouth beckons.", |
| 12 | + "n_to": "foyer", |
| 13 | + }, |
| 14 | + |
| 15 | + "foyer": { |
| 16 | + "name": "Foyer", |
| 17 | + "description": "Dim light filters in from the south. Dusty passages run north and east.", |
| 18 | + "n_to": "overlook", |
| 19 | + "s_to": "outside", |
| 20 | + "e_to": "narrow", |
| 21 | + }, |
| 22 | + |
| 23 | + "overlook": { |
| 24 | + "name": "Grand Overlook", |
| 25 | + "description": """A steep cliff appears before you, falling |
| 26 | +into the darkness. Ahead to the north, a light flickers in |
| 27 | +the distance, but there is no way across the chasm.""", |
| 28 | + "s_to": "foyer", |
| 29 | + }, |
| 30 | + |
| 31 | + "narrow": { |
| 32 | + "name": "Narrow Passage", |
| 33 | + "description": "The narrow passage bends here from west to north. The smell of gold permeates the air.", |
| 34 | + "w_to": "foyer", |
| 35 | + "n_to": "treasure", |
| 36 | + }, |
| 37 | + |
| 38 | + "treasure": { |
| 39 | + "name": "Treasure Chamber", |
| 40 | + "description": """You've found the long-lost treasure |
| 41 | +chamber. Sadly, it has already been completely emptied by |
| 42 | +earlier adventurers. The only exit is to the south.""", |
| 43 | + "s_to": "narrow", |
| 44 | + }, |
| 45 | + |
| 46 | +} |
| 47 | + |
| 48 | +""" template room to copy into code |
| 49 | + "room": { |
| 50 | + "name": "", |
| 51 | + "description": "", |
| 52 | + "n_to": "", |
| 53 | + "s_to": "", |
| 54 | + "e_to": "", |
| 55 | + "w_to": "", |
| 56 | + }, |
| 57 | +""" |
| 58 | + |
| 59 | +# Write a class to hold player information, e.g. what room they are in currently |
| 60 | +class Player: |
| 61 | + """Holds information about a player""" |
| 62 | + def __init__(self, startRoom): |
| 63 | + self.curRoom = startRoom |
| 64 | + |
| 65 | +def tryDirection(d, curRoom): |
| 66 | + """ |
| 67 | + Try to move a direction, or print an error if the player can't go that way. |
| 68 | +
|
| 69 | + Returns the room the player has moved to (or the same room if the player didn't move). |
| 70 | + """ |
| 71 | + key = d + "_to" |
| 72 | + |
| 73 | + if key not in rooms[curRoom]: |
| 74 | + print("You can't go that way") |
| 75 | + return curRoom |
| 76 | + |
| 77 | + dest = rooms[curRoom][key] |
| 78 | + |
| 79 | + return dest |
| 80 | + |
| 81 | + |
| 82 | +# |
| 83 | +# Main |
| 84 | +# |
| 85 | + |
| 86 | +# Make a new player object that is currently in the 'outside' room. |
| 87 | + |
| 88 | +p = Player('outside') |
| 89 | + |
| 90 | +# Write a loop that: |
| 91 | +# |
| 92 | +# * Prints the current room name |
| 93 | +# * Prints the current description (the textwrap module might be useful here). |
| 94 | +# * Waits for user input and decides what to do. |
| 95 | +# |
| 96 | +# If the user enters a cardinal direction, attempt to move to the room there. |
| 97 | +# Print an error message if the movement isn't allowed. |
| 98 | +# |
| 99 | +# If the user enters "q", quit the game. |
| 100 | + |
| 101 | +done = False |
| 102 | + |
| 103 | +while not done: |
| 104 | + # Print the room name |
| 105 | + print("\n{}\n".format(rooms[p.curRoom]['name'])) |
| 106 | + |
| 107 | + # Print the room description |
| 108 | + for line in textwrap.wrap(rooms[p.curRoom]['description']): |
| 109 | + print(line) |
| 110 | + |
| 111 | + # User prompt |
| 112 | + s = input("\nCommand> ").strip().lower() |
| 113 | + |
| 114 | + # Handle input |
| 115 | + if s == "q": |
| 116 | + done = True |
| 117 | + elif s in ["n", "s", "w", "e"]: |
| 118 | + p.curRoom = tryDirection(s, p.curRoom) |
| 119 | + else: |
| 120 | + print("Unknown command {}".format(s)) |
| 121 | + |
0 commit comments