-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgameEngine2.py
386 lines (347 loc) · 14.6 KB
/
gameEngine2.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
class Item:
# initializes the item. name = the name of the item
# desc = the description of the item provided when the player examines it
# key_val = the value the item has as a key, passed to an object if this item is used on it
# lock_val = the value compared with a given key value to see if this item reacts with an item being used on it
# trans_id = an item that this item becomes after the correct key has been applied to it
def __init__(self, name, desc, key_val, lock_val=None, trans_id=None, destination=None, trap_desc=None):
self.Name = name
self.Desc = desc
self.KeyVal = key_val
self._LockVal = lock_val
self._TransID = trans_id
self.Destination = destination
self.Trap_Desc = trap_desc
def get_lock_val(self):
return self._LockVal
def get_trans_id(self):
return self._TransID
def examine(self):
print(self.Desc)
# use some object on this item to see if it does anything. If it does, turn into the relevant item
# and return 1, if the items do not interact return 0
def use(self, input):
key = input.KeyVal
if self._LockVal is not None and key == self._LockVal:
output = "It works! The " + input.get_name() + " and the " + self.Name + " become a " \
+ self._TransID.getName()
print(output)
self.Name = self._TransID.getName()
self.Desc = self._TransID.getDesc()
self.KeyVal = self._TransID.getKeyVal()
self._LockVal = self._TransID.getTransKey()
self._TransID = self._TransID.getTransID()
return 1
else:
return 0
# represents the player character and the actions they take
class Player:
# Initializes with a Room variable indicating the Room the player starts the game in, an integer indicating how
# many actions the player has before time runs out, and a Bag list indicating the items what
# the player has in their inventory
def __init__(self, location, time):
self.Bag = []
self._Location = location
self.Turns_Remaining = time
self.Last_Loc = None
def add_item(self, item):
self.Bag.append(item)
# takes the name of an item the player wishes to pick up, and searches everywhere the player has access to for an
# Item with a matching name. If found, adds the Item to the player's Bag construct
def take(self, item):
for thing in self._Location.Floor:
if thing.Name == item:
self.Bag.append(thing)
self._Location.Floor.remove(thing)
print("You pick up the " + item + "and put it in your bag")
return 1
for shelf in self._Location.Shelves:
if not shelf.Locked:
for thing in shelf.Contents:
if thing.Name == item:
self.Bag.append(thing)
self._Location.Floor.remove(thing)
print("You pick up the " + item + "and put it in your bag")
if thing.destination is not None:
print(thing.Trap_Desc)
self._Location = thing.Destination
return 1
print("There is no " + item + " here.")
return 0
# represents the player using an item from their inventory on another object (combining it with another item in
# their inventory, using it to unlock a door, or shelf, or disarm a trap). Takes as argument the Name of the Item
# the player wishes to use as item and the Name of the object they wish to use it on as target
def use(self, item, target):
lock = None
key = None
for thing in self.Bag:
if thing.Name == item:
key = thing
elif thing.Name == target:
lock = thing
for thing in self._Location.Doors:
if thing.Name == target:
lock = thing
for thing in self._Location.Traps:
if thing.Name == target:
lock = thing
for thing in self._Location.Shelves:
if thing.Name == target:
lock = thing
if lock and key:
if lock.use(key) == 1:
self.Bag.remove(key)
if not key:
print("You do not have a " + item)
elif not lock:
print("There is no " + target + " to use your " + item + " on.")
# takes as argument the name of an object the player wishes to examine more closely. If the object is found, prints
# the description of that object
def look(self, target):
if self._Location.Name == target:
self._Location.examine()
return 1
for thing in self.Bag:
if thing.Name == target:
thing.examine()
return 1
for thing in self._Location.Floor:
if thing.Name == target:
thing.examine()
return 1
for thing in self._Location.Shelves:
if thing.Name == target:
thing.examine()
return 1
for thing in self._Location.Traps:
if thing.Name == target:
thing.examine()
return 1
for thing in self._Location.Doors:
if thing.Name == target:
thing.examine()
return 1
print("There is no " + target + " here.")
return 0
# gives the player the more detailed text they receive upon first entering a room
def look_around(self):
self._Location.look_around()
# takes as argument the name of an Item the player wishes to remove from their bag
def drop(self, item):
for thing in self.Bag:
if thing.Name == item:
print("You drop your " + item + " on the floor")
self._Location.add_item(thing)
self.Bag.remove(thing)
return 1
print("You do not have a " + item)
return 0
# takes as argument the name of a room the player wishes to move to, and attempts to move there (if it can be found
# and there are no traps preventing it)
def move(self, room):
<<<<<<< HEAD
for thing in self._Location.Neighbors:
if thing.Name == room:
for trap in self._Location.Traps:
if trap.Locked is True:
trap.spring()
if trap.Destination:
self._Location = trap.Destination
else:
self.Turns_Left = 0
return 2
thing.enter()
self._Location = thing
return 1
print("You do not see a " + room + " to go to.")
=======
for door in self._Location.Doors:
if not door.Locked:
if door.Destination.Name == room or door.Direction == room or door.Name == room:
if door.Destination == self.Last_Loc:
door.Destination.enter()
self.Last_Loc = self._Location
self._Location = door.Destination
return 1
for trap in self._Location.Traps:
if trap.Locked is True:
trap.spring()
if trap.Destination:
self._Location = trap.Destination
else:
self.Turns_Remaining = 0
return 2
door.Destination.enter()
self.Last_Loc = self._Location
self._Location = door.Destination
return 1
print("You cannot get to " + room + " from here.")
>>>>>>> origin/GameEngine
return 0
class Room:
# Represents the discrete spaces found within the dungeon. Initializes with a name and a description, as well as
# empty lists to contain the Items, Doors, Shelves, Traps, and other Rooms that can be accessed from this room
<<<<<<< HEAD
def __init__(self, name, long_desc, short_desc):
self.Name = name
self.LongDesc = long_desc
=======
def __init__(self, name, desc, short_desc):
self.Name = name
self.Desc = desc
>>>>>>> origin/GameEngine
self.ShortDesc = short_desc
self.Floor = []
self.Doors = []
self.Shelves = []
self.Traps = []
self.Visited = False
def add_item(self, item):
self.Floor.append(item)
def add_door(self, door):
self.Doors.append(door)
def add_shelf(self, shelf):
self.Shelves.append(shelf)
def add_trap(self, trap):
self.Traps.append(trap)
def examine(self):
if self.Visited:
<<<<<<< HEAD
print(self.ShortDesc)
else:
print(self.LongDesc)
print("Through nearby doorways, you can see: ")
for room in self.Neighbors:
print(room.Name)
if len(self.Doors) > 0:
for door in self.Doors:
print("There is a " + door.Name + " here.")
=======
print(self.Desc)
else:
print(self.ShortDesc)
for door in self.Doors:
door.examine()
>>>>>>> origin/GameEngine
if len(self.Shelves) > 0:
for shelf in self.Shelves:
print("There is a " + shelf.Name + " here.")
if len(self.Traps) > 0:
for trap in self.Traps:
print("There is a " + trap.Name + " here.")
if len(self.Floor) > 0:
print("There are a few items scattered about: ")
for item in self.Floor:
print(item.Name)
def enter(self):
<<<<<<< HEAD
print("You enter the " + self.Name)
self.examine()
=======
print("You enter ")
>>>>>>> origin/GameEngine
if not self.Visited:
self.Visited = True
else:
self.examine()
class Door:
# init door with name, description, key value that will unlock it
def __init__(self, name, desc, direction, destination, locked=False, lock_val=None, unlock_desc=None):
self.Name = name
self.Desc = desc
self.UnlockDesc = unlock_desc
self.Direction = direction
self._LockVal = lock_val
self.neighbor = destination
self.Locked = locked
def get_lock_val(self):
return self._LockVal
def examine(self):
print(self.Desc + " lies to the " + self.Direction)
# takes an Item as input, if the door is locked it compares the key value of the item passed against the key it
# expects. If they match, it adds the room it stores (leads to) to the list of Rooms accessible from the room set as
# the Door's location. (notably, location need not be the room where the player finds a door, and a door could be
# implemented that doesn't change the room the player is in, but they must search to see what they have unlocked.)
def use(self, input):
key = input.KeyVal
if self.Locked:
if key == self._LockVal:
self.Locked = False
self.Desc = self.UnlockDesc
print("Success! The passage opens to reveal: ")
self.examine()
return 1
else:
return 0
else:
print("That's already open.")
return 0
class Shelf:
# represents objects found in rooms that contain Items. Initializes with the name of the Shelf, a description,
# weather or not the Shelf needs to be unlocked before the items in it can be accessed, and, if it does need to be
# unlocked, what key value it is expecting to unlock it
def __init__(self, name, desc, locked, lock_val=None):
if locked and not lock_val:
print("ERROR: locked status=True and lock_val=None, a locked door must have a key")
self.Name = name
self.Contents = []
self.Desc = desc
self._LockVal = lock_val
self.Locked = locked
def get_lock_val(self):
return self._LockVal
def use(self, input):
key = input.KeyVal
if self.Locked and key == self._LockVal:
print("Success! The " + key.Name + " opens the " + self.Name)
self.Locked = False
self.examine()
return 1
else:
print("That doesn't seem to do anything.")
return 0
def examine(self):
if self.Locked:
print(self.Desc)
print("It seems like there may something in this, but you can't seem to get inside it.")
return 0
else:
print("The " + self.Name + " contains:")
for item in self.Contents:
print(item.get_name())
return 1
class Trap:
# a trap that can spring when the player moves through the room it's in, killing them or sending them to a different
# room. Initializes with a name, description, description of what happens to the player when the trap is sprung,
# key value expected to disarm the trap, and the room the player is sent to when the trap is sprung.
# if no destination is given, the trap simply kills the player.
def __init__(self, name, desc, s_desc, lock_val, destination=None):
self.Name = name
self.Desc = desc
self.Spring_desc = s_desc
self._LockVal = lock_val
self.Destination = destination
self.Locked = True
def get_lock_val(self):
return self._LockVal
def examine(self):
print(self.Desc)
if not self.Locked:
print("It appears to be disarmed and safe to pass")
def spring(self):
print(self.Spring_desc)
def use(self, input):
key = input.getKeyVal()
if self.Locked:
if key == self._LockVal:
output = "It works! The " + input.get_name() + " disarms the " + self.Name + \
", it should be safe to pass now"
print(output)
self.Locked = False
return 1
else:
print("That doesn't seem to do anything")
return 0
else:
print("This trap is already disarmed.")
return 0