-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextBasedAdventureTest.py
242 lines (222 loc) · 11.1 KB
/
TextBasedAdventureTest.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#Text based adventure game framework?
#I love text based adventure games, despite never living in a time before modern game engines.
#I have a passion to learn and love making things so please enjoy!
#Made by Killer Kat on github, January 2023
import random #lul so random XD
class Room:
def __init__(self, name, desc, roomID, northRoom=None, eastRoom=None, southRoom=None, westRoom=None):
self.name = name
self.desc = desc
self.roomID = roomID
self.contents = []
#These are the rooms that are connected to this room, IE southRoom is the room to the south of this one
self.northRoom = northRoom
self.eastRoom = eastRoom
self.southRoom = southRoom
self.westRoom = westRoom
def __str__(self):
return f"{self.name}: {self.desc}"
class Item:
def __init__(self, name, desc, canTake, useAction="Default"):
self.name = name
self.desc = desc
self.canTake = canTake
self.useAction = useAction
def __str__(self):
return f"{self.name}: {self.desc}"
class Container(Item):
def __init__(self, name, desc, canTake):
super().__init__(name,desc, canTake)
self.contents = []
#Global vars
score = 0
CurrentRoomID = 0
Inventory = []
isBombSet = True
#Items
defaultItem = Item("Default Item", "An incredibly default item, you bask in the glow of its defaultness!", True)
seriousItem = Item("SeriousItem", "An incredibly serious item, you feel the aura of its seriousness!", True)
logicBomb = Item("Logic Bomb","A worrying logical bomb, if you dont disarm it you might be destroyed with facts and logic.", False, "bomb")
seriousTable = Container("Serious Table", "The most serious table you have ever seen!", False)
seriousTable.contents.append(seriousItem)
#Rooms
defaultRoom = Room("Default Room", "A strikingly default room with a real sense of defaultness about it",0,)
defaultRoom.contents.append(defaultItem)
defaultRoom.contents.append(logicBomb)
seriousRoom = Room("Serious Room", "An incredibly serious room, the most serious room you have ever seen",1)
seriousRoom.contents.append(seriousTable)
#Room connections
defaultRoom.northRoom = seriousRoom
seriousRoom.southRoom = defaultRoom
#Need to define this after rooms or it doesnt work (wait or does it?)
currentRoom = defaultRoom
def TextParser(text, room):
global currentRoom
try:
split1 = text.split(":")
verb = split1[0].strip().lower()
noun = split1[1].strip().lower()
#print(verb, noun) #for debug, del this line later
match verb:
case "look":
if noun.lower() == "around": ### what? it shoud load the room by default and also if you specify it.
print("You see " + room.desc + " and : ")
if len(room.contents) == 0:
print("Nothing")
else:
for i in room.contents:
print(i.name)
if isinstance(i, Container):
print("It contains:")
if len(i.contents) == 0:
print("Nothing")
else:
for c in i.contents:
print(" " + c.name)
if room.northRoom is not None:
print("To the north there is: " + room.northRoom.name)
if room.eastRoom is not None:
print("To the east there is: " + room.eastRoom.name)
if room.southRoom is not None:
print("To the south there is: " + room.southRoom.name)
if room.westRoom is not None:
print("To the west there is: " + room.westRoom.name)
elif noun.lower() == "inventory":
print("You have:")
for i in Inventory:
print(i.name)
else:
parserLookHint = 0
for i in room.contents:
if noun == i.name.lower() :
print(i.desc)
parserLookHint = 1
if isinstance(i, Container):
print("It contains:")
if len(i.contents) == 0:
print("Nothing")
else:
for c in i.contents:
print(" " + c.name)
break
for i in Inventory:
if noun == i.name.lower():
print(i.desc)
parserLookHint = 1
break
if parserLookHint == 0:
print("Look at what? To look at your surroundings use Look : Around")
print("to check your inventory use Look : Inventory")
case "take":
for i in room.contents:
if noun == i.name.lower():
if i.canTake == True :
room.contents.remove(i)
Inventory.append(i)
print("You take the " + i.name)
else: print ("You cant take the " + i.name)
case "drop":
for i in Inventory:
if noun == i.name.lower():
Inventory.remove(i)
room.contents.append(i)
print("You drop the " + i.name)
case "loot":
for i in room.contents:
if noun == i.name.lower():
x = input("Loot what from " + noun + "? ")
for y in i.contents:
if y.name.lower() == x.lower() :
i.contents.remove(y)
Inventory.append(y)
print("You take the " + x + " from the " + noun)
else: print(x + " not found in this container.")
break
break
else: print("Container not found, try Loot : Container Name")
case "fill":
for i in room.contents:
if noun == i.name.lower():
x = input("Fill " + noun + " with what? ")
for y in Inventory:
if y.name.lower() == x.lower() :
i.contents.append(y)
Inventory.remove(y)
print("You put the " + x + " in the " + noun)
else: print("You dont have a " + x)
break
else: print("Container not found, try Fill : Container Name")
case "go":
if noun == "north" or noun == "n":
if room.northRoom is not None:
currentRoom = room.northRoom
else: print("You cannot go north here.")
elif noun == "east" or noun == "e":
if room.eastRoom is not None:
currentRoom = room.eastRoom
else: print("You cannot go east here.")
elif noun == "south" or noun == "s":
if room.southRoom is not None:
currentRoom = room.southRoom
else: print("You cannot go south here.")
elif noun == "west" or noun == "w":
if room.westRoom is not None:
currentRoom = room.westRoom
else: print("You cannot go west here.")
case "use":
usehint = True
for i in room.contents:
if i.name.lower() == noun:
Use(i.useAction)
usehint = False
break
for i in Inventory:
if i.name.lower() == noun:
Use(i.useAction)
usehint = False
break
if usehint == True:
print("Could not find " + noun + " try Use : Item.")
case "hint":
Hint()
case "help":
if noun == "please":
print("Thank you for being polite.")
Help()
else:
print("You should be more polite!")
case "xyzzy":
print("A hollow voice says, Nerd!")
case _:
print("The parser didnt recognize your verb, please try again!")
except IndexError:
print("Oops! The Parser didnt understand Try again...")
def Main(promt):
print("Score: " + str(score) + " " + promt)
TextParser(input(">"), currentRoom)
Main(currentRoom.name)
def ScoreHandler(x):
global score
score = score + x
def Use(x):
global isBombSet
match x:
case "bomb":
isBombSet = not isBombSet
if isBombSet == False:
logicBomb.desc = "A worrying logical bomb, its been disarmed."
else: logicBomb.desc = "A worrying logical bomb, if you dont disarm it you might be destroyed with facts and logic."
print("Toggled Bomb")
case _:
print("You cant use this.")
def Hint():
hintsList = ["Try going weast.", "XYZZY", "You cant get ye flask!", "You can get a hint by using the Hint verb!", "It's an open source game, just look at the code!", "Try calling our support hotline at 1-800-555-KILLERKAT", "Control alt delete", "Ask again later", "Have you listened to my podcast The CyberKat Cafe? Check out our website cyberkatcafe.com", "That's not a bug, it's a feature!"]
print(random.choice(hintsList))
def Help():
print("Look, use Look : Around to examine your surroundings or Look : Something to look at something in more detail, to see your inventory use Look : Inventory")
print("Go, use Go and then one of the 4 cardinal directions to move in that direction. Provided there is something in that direction to move towards.")
print("Take, use Take and then the name of an item to pick up that item, for items in containers you need to use Loot : Container Name")
print("Drop, what do you think it does? use Drop : Item Name to drop an item in the current room.")
print("Fill, used to put items inside of containers. use Fill : Container Name")
print("There might be some other verbs, but I'll give you a Hint and say they might not be as useful as you would hope.")
Main("Wellcome! To give commands use the format VERB: NOUN, the : is required. Try Help : Please for a list of commands")