forked from bloominstituteoftechnology/Intro-Python-I
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadv.py
More file actions
121 lines (95 loc) · 3.04 KB
/
adv.py
File metadata and controls
121 lines (95 loc) · 3.04 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# Write a text adventure that allows the player to move from room to room by
# typing "n", "w", "s", or "e" for north, west, south, and east.
import textwrap
# These are the existing rooms. Add more as you see fit.
rooms = {
"outside": {
"name": "Outside Cave Entrance",
"description": "North of you, the cave mouth beckons.",
"n_to": "foyer",
},
"foyer": {
"name": "Foyer",
"description": "Dim light filters in from the south. Dusty passages run north and east.",
"n_to": "overlook",
"s_to": "outside",
"e_to": "narrow",
},
"overlook": {
"name": "Grand Overlook",
"description": """A steep cliff appears before you, falling
into the darkness. Ahead to the north, a light flickers in
the distance, but there is no way across the chasm.""",
"s_to": "foyer",
},
"narrow": {
"name": "Narrow Passage",
"description": "The narrow passage bends here from west to north. The smell of gold permeates the air.",
"w_to": "foyer",
"n_to": "treasure",
},
"treasure": {
"name": "Treasure Chamber",
"description": """You've found the long-lost treasure
chamber. Sadly, it has already been completely emptied by
earlier adventurers. The only exit is to the south.""",
"s_to": "narrow",
},
}
""" template room to copy into code
"room": {
"name": "",
"description": "",
"n_to": "",
"s_to": "",
"e_to": "",
"w_to": "",
},
"""
# Write a class to hold player information, e.g. what room they are in currently
class Player:
"""Holds information about a player"""
def __init__(self, startRoom):
self.curRoom = startRoom
def tryDirection(d, curRoom):
"""
Try to move a direction, or print an error if the player can't go that way.
Returns the room the player has moved to (or the same room if the player didn't move).
"""
key = d + "_to"
if key not in rooms[curRoom]:
print("You can't go that way")
return curRoom
dest = rooms[curRoom][key]
return dest
#
# Main
#
# Make a new player object that is currently in the 'outside' room.
p = Player('outside')
# Write a loop that:
#
# * Prints the current room name
# * Prints the current description (the textwrap module might be useful here).
# * Waits for user input and decides what to do.
#
# If the user enters a cardinal direction, attempt to move to the room there.
# Print an error message if the movement isn't allowed.
#
# If the user enters "q", quit the game.
done = False
while not done:
# Print the room name
print("\n{}\n".format(rooms[p.curRoom]['name']))
# Print the room description
for line in textwrap.wrap(rooms[p.curRoom]['description']):
print(line)
# User prompt
s = input("\nCommand> ").strip().lower()
# Handle input
if s == "q":
done = True
elif s in ["n", "s", "w", "e"]:
p.curRoom = tryDirection(s, p.curRoom)
else:
print("Unknown command {}".format(s))