-
Notifications
You must be signed in to change notification settings - Fork 0
/
Commands.py
221 lines (180 loc) · 5.91 KB
/
Commands.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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#CMD Definining file
import os
os.system('python MapCreator.py')
import random
inventoryItems = {}
mapItems = {}
currentRow = 1
currentCol = 3
# Loads all the items in the game into memory
itemList = {}
infile = open('Items.txt', 'r')
for line in infile:
strLine = str(line)
space = strLine.find(" ")
word = strLine[:space]
define = strLine[space:]
itemList.update({word: define})
# Prints all the items in the players inventory
def inventory():
if inventoryItems:
for key,value in inventoryItems.items():
item = str(key) + ": " + str(value)
print(item)
else:
print("Your Inventory Is Empty")
def loadMap(room):
mapItems.clear()
with open(room,"r") as theRoom:
for line in theRoom:
strLine = str(line)
space = strLine.find(" ")
word = strLine[:space]
define = strLine[space:]
mapItems.update({word: define})
# Allows player to pick up and add an item to the player's inventory
def pickUp(item):
item = item.upper()
flag = False
if len(mapItems) > 0:
for key,value in inventoryItems.items():
if key == item:
flag = True
break
if flag == True:
print("You already have this item in your inventory")
else:
for key,value in mapItems.items():
if key == item:
inventoryItems.update({key:value})
print("This item is now in your inventory")
del mapItems[item]
break
else:
print("The item ","'", item,"'", " is not in this room")
# Displays a map if player has a map in their inventory
def displayMap():
for key,value in inventoryItems.items():
if key == "MAP":
with open("map.txt","r") as mapFile:
for line in mapFile:
print(line, end="")
return
print("You do not have a map in your inventory")
def destroyItem(item):
item = item.upper()
flag = False
for key,value in inventoryItems.items():
if key == item:
flag = True
break
if flag == False:
print("You do not have this item in your inventory")
else:
for key,value in itemList.items():
if key == item:
del inventoryItems[item]
def use(item):
item = item.upper()
for key in inventoryItems:
if key == item:
if item == "CELLPHONE":
rand = random.randrange(100)
if rand <= 20:
print("Your call went through and you have been rescued")
return
else:
print("Your phone caught fire and was destroyed.")
del inventoryItems[item]
return
print("You do not have an item that you can interact with")
def search():
allItems = ""
for key in mapItems:
allItems = allItems + key + ", "
if len(allItems)<2:
print("There is nothing here")
else:
print("You have found: ",allItems)
def moveRoom(mapIndexDict, direction):
global currentRow
global currentCol
direction = direction.upper()
direction = direction[:1]
availableDirections = []
directs = mapIndexDict["EXITS"]
for x in range(len(mapIndexDict["EXITS"])):
availableDirections.extend(directs[x])
for i in range(len(availableDirections)):
if direction == availableDirections[i]:
if direction == "N":
currentRow += 1
if direction == "S":
currentRow -= 1
if direction == "E":
currentCol += 1
if direction == "W":
currentCol -= 1
return False
print("You cannot travel that way")
def help():
with open("CMD.txt", "r") as helpFile:
for line in helpFile:
print(line)
def main():
mapIndexDict = {}
x = 1
finalRoom = False
print("Type 'Help' for help")
while not finalRoom:
#get room data
mapIndex = "M" + str(currentCol) + str(currentRow) + ".txt"
with open("Maps/MapIndex/"+mapIndex,"r") as currentRoom:
for line in currentRoom:
strLine = str(line)
eol = strLine.find("\n")
space = strLine.find(" ")
key = strLine[:space]
value = strLine[space:eol].strip()
mapIndexDict.update({key: value})
#get room content
print("You are currently in room: ", mapIndexDict["ROOMNAME"])
if mapIndexDict["ROOMNAME"] == "Crypt":
print("Congrats, you have escaped the wilderness!")
finalRoom = True
break
loadMap("Maps/"+mapIndexDict["MAPNAME"])
roomFlag = True
#while in room
while roomFlag == True:
#user action
cmd = str(input("Awaiting Command...:"))
cmd = cmd.upper()
cmd = cmd + " "
#if move: leave room
space = cmd.find(" ")
cmd1 = cmd[:space]
cmd2 = cmd[space:].strip()
if cmd1 == "PICKUP":
pickUp(cmd2)
elif cmd1 == "GO":
roomFlag = moveRoom(mapIndexDict, cmd2)
elif cmd1 == "DESTROY":
destroyItem(cmd2)
elif cmd1 == "SHOW":
if cmd2 == "MAP":
displayMap()
elif cmd2 == "INVENTORY":
inventory()
else:
print("You cannot look at this item")
elif cmd1 == "USE":
if cmd2 == "CELLPHONE":
use(cmd2)
else:
print("This is not an item you can interact with")
elif cmd1 == "SEARCH":
search()
elif cmd1 == "HELP":
help()
main()