· 4 years ago · May 30, 2021, 12:18 AM
1from room import Room
2from character import Enemy
3from character import Friend
4from character import Character
5from rpginfo import RPGInfo
6from item import Item
7
8def createMap(kitchen,diningroom,ballroom):
9 kitchen.set_linkedRooms('south',diningroom)
10 kitchen.set_linkedRooms('north',storeroom)
11 diningroom.set_linkedRooms('north',kitchen)
12 diningroom.set_linkedRooms('west',ballroom)
13 ballroom.set_linkedRooms('east',diningroom)
14 ballroom.set_linkedRooms('south',garden)
15 ballroom.set_linkedRooms('west',courtyard)
16 courtyard.set_linkedRooms('east',ballroom)
17 garden.set_linkedRooms('north',ballroom)
18 garden.set_linkedRooms('south',forest)
19 forest.set_linkedRooms('north',garden)
20 forest.set_linkedRooms('south',deepforest)
21 deepforest.set_linkedRooms('north',forest)
22 storeroom.set_linkedRooms('south',kitchen)
23
24
25def addDescriptions(kitched,diningroom,ballroom):
26 kitchen.description='A small dirty room. It contains a large table covererd in dust'
27 diningroom.description='A large room with ornate golden decorations on each wall'
28 ballroom.description='A vast room with a shiny wooden floor; huge candlesticks guard the entrance'
29 garden.description='An overgrown garden covered with thorn bushes and weeds'
30 forest.description='A dark foreboding place. The leafless trees are crooked and close preventing easy passage'
31 deepforest.description='A very dark place, the trees grow so close together, all sunlight is blocked'
32 storeroom.description='A small room with shelves covered in rotting food and dirt'
33 courtyard.description='A large cobbled square, the way to the outside world'
34
35def fullMap(rooms):
36 for r in rooms:
37 r.describe()
38
39def getAction():
40 response = input('Enter Action: ').strip().lower()
41 return response
42
43def prepareZach(weapon):
44 zach = Enemy('Zach','A Zombie and he wants to eat your brain','Zombie')
45 zach.set_weapon(weapon)
46 zach.set_responses(['Grr','Eat Brain','Urrgh','Zombie Stomp'])
47 return zach
48
49def prepareSid(weapon):
50 sid = Enemy('Sid','A Skeleton with piercing red eyes','Skeleton')
51 sid.set_weapon(weapon)
52 sid.set_responses(['Rattle','Chatter','Attack','Bone Chilling'])
53 return sid
54
55def prepareRat(weapon):
56 roland = Enemy('Roland','A Giant annoying rat wearing a baseball cap and jacket','Rat')
57 roland.set_weapon(weapon)
58 roland.set_responses(['Gnaw','Chitter','Squeek','Scratch'])
59 return roland
60
61def prepareOgre(weapon):
62 ollie = Enemy('Ollie','A Huge ugly monster waving it fists in the air','Ogre')
63 ollie.set_weapon(weapon)
64 ollie.set_responses(['Roar','Smash','Attack','Aaarrgh'])
65 return ollie
66
67def prepareFriend():
68 fred = Friend('Fred','A friendly person who will give you information')
69 fred.set_responses(['Hello','Ollie has a rock','Paper works against a Rat','Skeleton has paper','A Zombie has scissors'])
70 return fred
71
72def checkForCharacter(currentRoom):
73 if currentRoom.character == None:
74 return False
75 else:
76 return True
77
78def playerName():
79 name = input('Warrior, what is your name? ').strip().title()
80 if name == '':
81 name = 'Anon'
82 return name
83
84#Main Program
85name = playerName()
86castle = RPGInfo('The Spooky Castle')
87RPGInfo.setName(name)
88RPGInfo.setAuthor('Ann Onn')
89RPGInfo.gameinfo()
90castle.welcome()
91RPGInfo.aim()
92RPGInfo.hint()
93RPGInfo.pause()
94
95kitchen = Room('Kitchen')
96ballroom = Room('Ballroom')
97diningroom = Room('Dining Room')
98garden = Room('Garden')
99forest = Room('Forest')
100storeroom = Room('Store Room')
101courtyard = Room('Courtyard')
102deepforest = Room('Deep Forest')
103rooms = [kitchen,ballroom, diningroom]
104
105createMap(kitchen,diningroom,ballroom) #,garden,forest)
106addDescriptions(kitchen,diningroom,ballroom) #,garden,forest)
107
108player = Character(name,'A Fearsome Warrior')
109rock = Item('Rock','Good against scissors','1')
110paper = Item('Paper','Good against rock','2')
111scissors = Item('Scissors','Good against paper','3')
112
113zach = prepareZach(scissors)
114sid = prepareSid(paper)
115fred = prepareFriend()
116roland = prepareRat(rock)
117ollie = prepareOgre(rock)
118
119
120diningroom.character = fred
121diningroom.item = paper
122ballroom.character= zach
123forest.character=sid
124forest.item = paper
125deepforest.character=roland
126deepforest.item = scissors
127courtyard.character=ollie
128kitchen.item = scissors
129storeroom.item = rock
130
131
132currentRoom = kitchen
133currentRoom.describe()
134#fullMap(rooms)
135
136
137victory = False
138play = True
139while play and not victory:
140 action = getAction()
141 play = not (action == 'exit')
142 if action == 'exit':
143 play = False
144 elif action == 'look':
145 currentRoom.describe()
146 elif action == 'help':
147 RPGInfo.instructions()
148 elif action == 'fight':
149 if checkForCharacter(currentRoom):
150 character = currentRoom.character
151 if character.characterType() == 'Enemy':
152 weapon = input('Choose your weapon {}: '.format(player.displayItems())).strip().title()
153 if weapon in player.getInventory():
154 result = character.fight(weapon)
155 if result == 'Win':
156 treasure = character.get_weapon()
157 print('{} drops to the floor'.format(treasure.get_name()))
158 currentRoom.item = treasure
159 currentRoom.character = None
160 player.remove_Item(weapon)
161 castle.updateDefeated()
162 victory = castle.checkForVictory()
163 elif result == 'Lose':
164 play = False
165 else:
166 currentRoom.describe()
167 else:
168 print('You do not have {}'.format(weapon))
169 else:
170 character.fight()
171 else:
172 print('Nobody here to fight')
173 elif action == 'talk':
174 if checkForCharacter(currentRoom):
175 character = currentRoom.character
176 character.talk()
177 else:
178 print('There is nobody here to talk to')
179 elif action in ['north','south','east','west']:
180 print('Action {}'.format(action))
181 currentRoom = currentRoom.move(action)
182 currentRoom.describe()
183 elif action == 'take':
184 if currentRoom._item is not None:
185 print('You take {}'.format(currentRoom.item.get_name()))
186 player.set_items(currentRoom.item)
187 currentRoom.item = None
188 else:
189 print('There is nothing here to take')
190 elif action == 'inventory':
191 print(player.displayItems())
192 elif action == 'score':
193 castle.showScore()
194 else:
195 print('{} is an invalid action'.format(action))
196 RPGInfo.hint()
197
198if victory:
199 print('Congratulations {}, you have won'.format(castle.getName()))
200else:
201 print('You lost')
202RPGInfo.credits()
203
204
205
206
207
208class Room():
209 #constructor method
210 #init > means initialise
211 #self > means this object
212 #self > The self parameter automatically receives a reference to the object invoking the method
213 #self > By using self, a method can invoke the object and access the attributes and methods of that object
214 numberOfRooms = 0
215
216 def __init__(self,roomName):
217 #object attributes
218 self._name = roomName
219 self._description = None
220 self._item = None
221 self.exits = None
222 self.linkedRooms = {}
223 self._character = None
224 Room.numberOfRooms += 1
225
226 def get_numberOfRooms():
227 return Room.numberOfRooms
228
229 @property
230 def name(self):
231 return self._name
232
233 @name.setter
234 def name(self,name):
235 self._name = name
236
237 @property
238 def description(self):
239 return str(self._description)
240
241 @description.setter
242 def description(self,description):
243 self._description = description
244
245 @property
246 def character(self):
247 return self._character
248
249 @character.setter
250 def character(self,character):
251 self._character = character
252
253 @property
254 def item(self):
255 return self._item
256
257 @item.setter
258 def item(self,item):
259 self._item = item
260
261 def describe(self):
262 print(self.name.upper() + '\n' + '----------')
263 print('ROOM > {} - {}'.format(self.name,self.description))
264 if self.character is not None:
265 character = self.character
266 print('CHARACTER > {} - {}'.format(character.name, character.get_description()))
267 if self.item is not None:
268 item = self.item
269 print('ITEM > {} - {}'.format(item.get_name(),item.get_description()))
270 for direction in self.linkedRooms:
271 room = self.linkedRooms[direction]
272 print( "The " + room.name + " is " + direction)
273
274 def set_linkedRooms(self,direction,room):
275 self.linkedRooms[direction] = room
276
277 def get_linkedRooms(self,direction):
278 return self.linkedRooms[direction]
279
280 def move(self,direction):
281 if direction in self.linkedRooms:
282 print('Moving {} '.format(direction))
283 return self.linkedRooms[direction]
284 else:
285 print('You cannot move {}'.format(direction))
286 return self
287
288
289
290
291
292from random import randint
293
294class Character():
295 characterCount = 0
296 classVariable = 0
297 responses = []
298
299 def instanceMethod(self):
300 print('Instance Method - can access and modify instance and class variables')
301 self.__class__.classVariable += 10
302 return self.__class__.classVariable
303
304 @classmethod
305 def classMethod(cls):
306 print('Class Method - Can access and modify class variables')
307 cls.classVariable += 1
308 return cls.classVariable
309
310 @staticmethod
311 def staticMethod():
312 print('Static Method - Cannot access or modify class or instance variables')
313 return 'Nothing'
314
315 # Create a character
316 def __init__(self, char_name, char_description):
317 self.name = char_name
318 self.description = char_description
319 self.conversation = None
320 self.items = []
321 Character.characterCount += 1
322
323 def displayItems(self):
324 if len(self.get_items()) > 0:
325 itemstring = ''
326 for item in self.get_items():
327 itemstring = itemstring + str(item.get_name()) + " "
328 return itemstring
329 else:
330 return "None"
331
332 def get_Name(self):
333 return self.name
334
335 def get_characterCount():
336 return Character.characterCount
337
338 # Describe this character
339 def describe(self):
340 print( self.name + " is here!" )
341 print( self.description )
342
343 # Set what this character will say when talked to
344 def set_conversation(self, conversation):
345 self.conversation = conversation
346
347 # Talk to this character
348 def talk(self):
349 if self.conversation is not None:
350 print(self.name + ' says ' + self.conversation)
351 else:
352 print(self.name + " doesn't want to talk to you")
353
354 # Fight with this character
355 def fight(self, combat_item):
356 print(self.name + " doesn't want to fight with you")
357 return True
358
359 def characterType(self):
360 return self.__class__.__name__
361
362 def get_description(self):
363 return self.description
364
365 def getInventory(self):
366 if len(self.get_items()) > 0:
367 itemstring = []
368 for item in self.get_items():
369 itemstring.append(item.get_name())
370 return itemstring
371 else:
372 return []
373
374 def get_items(self):
375 return self.items
376
377 def set_items(self,item):
378 self.items.append(item)
379
380 def remove_Item(self,itemused):
381 for item in self.get_items():
382 if item.get_name() == itemused:
383 self.get_items().remove(item)
384 break
385
386 def set_responses(self,responses):
387 self.responses = responses
388
389 def get_responses(self):
390 return self.responses
391
392class Enemy(Character):
393 enemyCount = 0
394 enemyType = ''
395
396 def __init__(self,name,description,enemyType):
397 super().__init__(name,description)
398 self.weapon = None
399 self.enemyType = enemyType
400 Enemy.enemyCount += 1
401
402 def get_Type(self):
403 return self.enemyType
404
405 def set_Type(enemyType):
406 self.enemyType = enemyType
407
408 def get_enemyCount(self):
409 return self.enemyCount
410
411 def get_weapon(self):
412 if self.weapon == None:
413 return None
414 else:
415 return self.weapon
416
417 def set_weapon(self,weapon):
418 self.weapon = weapon
419
420 def fight(self,combatItem):
421 weapon = self.weapon.get_name().lower()
422 if combatItem.lower() == weapon:
423 print('Both weapons, but the fight is a draw')
424 return 'Draw'
425 elif (combatItem.lower() == 'rock' and weapon == 'scissors') or (combatItem.lower() == 'paper' and weapon == 'rock') or (combatItem.lower() == 'scissors' and weapon == 'paper'):
426 print('You defeat {} with {}'.format(self.name,combatItem))
427 return 'Win'
428 else:
429 print('Pathetic Human. Your {} is no match for me.'.format(combatItem))
430 return 'Lose'
431
432 def describe(self):
433 print('{} - I am the mighty {}!'.format(self.get_Type(),self.name))
434 #print(self.description)
435
436 def talk(self):
437 comment = self.responses[randint(0,len(self.responses)-1)]
438 print(comment)
439
440class Friend(Character):
441 friendCount = 0
442
443 def __init__(self,name,description):
444 super().__init__(name,description)
445 Friend.friendCount += 1
446
447 def get_friendCount():
448 return Friend.friendCount
449
450 def fight(self):
451 print('I do not want to fight you. I am your friend')
452
453 def describe(self):
454 print('Hello, my name is {}. I am your friend. I have information'.format(self.name))
455
456 def talk(self):
457 comment = self.responses[randint(0,len(self.responses)-1)]
458 print(comment)
459
460 def giveGift(self):
461 if self.items is not None:
462 print('{} gives you {}'.format(self.name,self.items))
463 items = self.items
464 self.set_items('None')
465 return items
466
467
468
469
470
471
472import datetime
473
474class RPGInfo():
475 author = "Anon"
476 name = "Unknown"
477 enemiesDefeated = 0
478
479 def checkForVictory(self):
480 if self.enemiesDefeated == 4:
481 return True
482 else:
483 return False
484
485 def __init__(self,title):
486 self.title = title
487 self._cyear = format(datetime.datetime.now().year)
488
489 def get_defeated(self):
490 return self.enemiesDefeated
491
492 def set_defeated(self,value):
493 self.enemiesDefeated = se
494
495 def updateDefeated(self):
496 self.enemiesDefeated = self.enemiesDefeated + 1
497
498 def showScore(self):
499 print('Current Score = {}'.format(self.enemiesDefeated))
500
501 @property
502 def cyear(self):
503 return self._cyear
504
505 @cyear.setter
506 def cyear(self,cyear):
507 self._year = cyear
508
509 #instance method
510 def welcome(self):
511 print('{}, Welcome to {}\n----------'.format(self.name,self.title))
512
513 def getTitle(self):
514 return self.title
515
516 #static method
517 @staticmethod
518 def gameinfo():
519 print('Made using OOP RPG Creator (c) {} \n'.format(RPGInfo.author))
520
521 #class method
522 @classmethod
523 def setAuthor(cls,author):
524 cls.author = author
525
526 @classmethod
527 def setName(cls,name):
528 cls.name = name
529
530 def getName(cls):
531 return cls.name
532
533 @classmethod
534 def getAuthor(cls):
535 return cls.author
536
537 @classmethod
538 def credits(cls):
539 print('{}, thank you for playing.'.format(cls.name))
540 print('Created by {}.'.format(cls.author))
541
542 def hint():
543 print('Type help to display possible commands')
544
545 def instructions():
546 print('The following actions are possible')
547 print('north, south, east, west > Move in that direction')
548 print('fight > fight character if one exists')
549 print('inventory > list items held by hero')
550 print('look > show room details')
551 print('take > pick up item if one exists')
552 print('talk > talk to character if one exists')
553 print('score > show current score')
554 print('exit > Leave Game')
555
556 def aim():
557 print('Expore the rooms and collect the items')
558 print('There are four Creatures lurking around the map')
559 print('Defeat all four to win the game')
560 print('Talk to a friend to get help in defeating the Creatures')
561
562 def pause():
563 response = input('Press any key to start the game')
564
565
566
567
568
569
570class Item():
571 def __init__(self,name,description,value):
572 #constructor
573 #object attributes
574 self.name = name
575 self.description = description
576 self.value = value
577
578 def set_name(self,name):
579 self.name = name
580
581 def get_name(self):
582 return self.name
583
584 def set_description(self,description):
585 self.description = description
586
587 def get_description(self):
588 return self.description
589
590 def set_value(self,value):
591 self.value = value
592
593 def get_value(self):
594 return self.value
595
596 def getDetails(self):
597 print(self.get_name() + ' : ' + self.get_description())
598
599 def drop(self):
600 print('You have dropped {}'.format(self.get_name()))
601
602 def use(self):
603 print('You have used {}',(self.get_name()))
604
605
606
607
608
609