Repository navigation
Expand file tree
/
Copy pathadv.py
More file actions
181 lines (142 loc) · 5.35 KB
/
Copy pathadv.py
File metadata and controls
181 lines (142 loc) · 5.35 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import textwrap
from room import Room
from player import Player
from item import Treasure, LightSource
# Declare all the rooms
room = {
'outside': Room("Outside Cave Entrance",
"North of you, the cave mount beckons"),
'foyer': Room("Foyer", """Dim light filters in from the south. Dusty
passages run north and east."""),
'overlook': Room("Grand Overlook", """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."""),
'narrow': Room("Narrow Passage", """The narrow passage bends here from west
to north. The smell of gold permeates the air."""),
'treasure': Room("Treasure Chamber", """You've found the long-lost treasure
chamber! The only exit is to the south."""),
}
# Link rooms together
room['outside'].n_to = room['foyer']
room['foyer'].s_to = room['outside']
room['foyer'].n_to = room['overlook']
room['foyer'].e_to = room['narrow']
room['overlook'].s_to = room['foyer']
room['narrow'].w_to = room['foyer']
room['narrow'].n_to = room['treasure']
room['treasure'].s_to = room['narrow']
room['outside'].is_light = True
room['foyer'].is_light = True
# Add some items
t = Treasure("coins", "Shiny coins", 100)
room['overlook'].contents.append(t)
t = Treasure("silver", "Tarnished silver", 200)
room['treasure'].contents.append(t)
l = LightSource("lamp", "Brass lamp")
room['foyer'].contents.append(l)
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).
"""
attrib = d + '_to'
# See if the room has the destination attribute
if hasattr(curRoom, attrib):
# If so, return its value (the next room)
return getattr(curRoom, attrib)
# Otherwise print an error and stay in the same room
print("You can't go that way")
return curRoom
def find_item(name, curRoom):
"""
Search the current room to see if we can locate the treasure in question.
"""
for i in curRoom.contents:
if i.name == name:
return i
return None
#
# Main
#
# Make a new player object that is currently in the 'outside' room.
player = Player(room['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:
# Make a list of all the items the player has (or are in the room) that are
# light sources:
light_sources = [item for item in player.contents + player.curRoom.contents
if isinstance(item, LightSource) and item.lightsource]
is_light = player.curRoom.is_light or len(light_sources) > 0
if is_light:
# Print the room name
print("\n{}\n".format(player.curRoom.name))
# Print the room description
for line in textwrap.wrap(player.curRoom.description):
print(line)
# Print any items found in the room
if len(player.curRoom.contents) > 0:
print("\nYou also see:\n")
for i in player.curRoom.contents:
print(" " + str(i))
else:
print("\nIt's pitch dark!\n")
# User prompt
s = input("\nCommand> ").strip().lower().split()
if len(s) > 2 or len(s) < 1:
print("I don't understand that.")
continue
# Intransitive verbs
if len(s) == 1:
if s[0] == "quit" or s[0] == "q":
done = True
elif s[0] == "inventory" or s[0] == "i":
if len(player.contents) == 0:
print("You're not carrying anything.")
else:
print("You are carrying:\n")
for i in player.contents:
print(f" {i}")
elif s[0] == "score":
print(f"Your score is currently {player.score}.")
elif s[0] in ["n", "s", "w", "e"]:
player.curRoom = tryDirection(s[0], player.curRoom)
else:
print("Unknown command {}".format(' '.join(s)))
# Transitive verbs
elif len(s) == 2:
if s[0] == 'get' or s[0] == 'take':
if is_light:
item = find_item(s[1], player.curRoom)
if item == None:
print("I don't see that here.")
else:
# Notify the item that it's about to be taken
item.on_take(player)
# Move from room to player
player.curRoom.contents.remove(item)
player.contents.append(item)
print(f"{item}: taken.")
else:
print("Good luck finding that in the dark.")
elif s[0] == 'drop':
item = find_item(s[1], player)
if item == None:
print("You're not carrying that.")
else:
# Move from player to room
player.contents.remove(item)
player.curRoom.contents.append(item)
print(f"{item}: dropped.")
else:
print("Unknown command {}".format(' '.join(s)))