Advertisement
JAS_Software

Mini Game

May 22nd, 2021
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.58 KB | None | 0 0
  1. from random import randint
  2.  
  3. class Character():
  4.  
  5.     # Create a character
  6.     def __init__(self, char_name, char_description):
  7.         self.name = char_name
  8.         self.description = char_description
  9.         self.conversation = None
  10.         self.items = None
  11.  
  12.     # Describe this character
  13.     def describe(self):
  14.         print( self.name + " is here!" )
  15.         print( self.description )
  16.  
  17.     # Set what this character will say when talked to
  18.     def set_conversation(self, conversation):
  19.         self.conversation = conversation
  20.  
  21.     # Talk to this character
  22.     def talk(self):
  23.         if self.conversation is not None:
  24.             print("[" + self.name + " says]: " + self.conversation)
  25.         else:
  26.             print(self.name + " doesn't want to talk to you")
  27.  
  28.     # Fight with this character
  29.     def fight(self, combat_item):
  30.         print(self.name + " doesn't want to fight with you")
  31.         return True
  32.        
  33.     def characterType(self):
  34.         return self.__class__.__name__
  35.        
  36.     def get_items(self):
  37.         return self.items
  38.    
  39.     def set_items(self,items):
  40.         self.items = items
  41.  
  42.  
  43. class Enemy(Character):
  44.     responses = ['Grrr','Aargh','Go Away!','What do you want?','Get Lost!','Give me your brain!']
  45.     def __init__(self,name,description):
  46.         super().__init__(name,description)
  47.         self.weapon = None
  48.  
  49.     def get_weapon(self):
  50.         if self.weapon == None:
  51.             return 'Immune'
  52.         else:
  53.             return self.weapon
  54.  
  55.     def set_weapon(self,weapon):
  56.         self.weapon = weapon
  57.  
  58.     def fight(self,combatItem):
  59.         if combatItem == 'rock':
  60.             print('You defeat {} with {}'.format(self.name,combatItem))
  61.             return 'Win'
  62.         elif combatItem == self.weapon:
  63.             print('Nothing Happens')
  64.             return 'Draw'
  65.         else:
  66.             print('Pathetic Earthling. Your {} is no match for me.'.format(combatItem))
  67.             return 'Lose'
  68.  
  69.     def describe(self):
  70.         print('I am the mighty {}!'.format(self.name))
  71.         #print(self.description)
  72.  
  73.     def talk(self):
  74.         comment = self.responses[randint(0,len(self.responses)-1)]
  75.         print(comment)
  76.    
  77. class Friend(Character):
  78.     responses = ['Hello','How are you?','A rock may be useful against Zach','Do you want a brew?','Zach uses scissors as a weapon']
  79.     def __init__(self,name,description):
  80.         super().__init__(name,description)
  81.  
  82.     def fight(self):
  83.         print('I do not want to fight you. I am your friend')
  84.  
  85.     def describe(self):
  86.         print('I am your friend {}. Items: {}'.format(self.name,self.items))
  87.            
  88.     def talk(self):
  89.         comment = self.responses[randint(0,len(self.responses)-1)]
  90.         print(comment)
  91.         #self.giveGift()
  92.        
  93.     def giveGift(self):
  94.         if self.items is not None:
  95.             print('{} gives you {}'.format(self.name,self.items))
  96.             items = self.items
  97.             self.set_items('None')
  98.             return items
  99.  
  100.  
  101.  
  102.  
  103. class Room():
  104.     #constructor method
  105.     #init > means initialise
  106.     #self > means this object
  107.     #self > The self parameter automatically receives a reference to the object invoking the method
  108.     #self > By using self, a method can invoke the object and access the attributes and methods of that object
  109.     def __init__(self,roomName):
  110.         #object attributes
  111.         self.name = roomName
  112.         self.description = None
  113.         self.items = None
  114.         self.exits = None
  115.         self.linkedRooms = {}
  116.         self.character = None
  117.  
  118.     def set_description(self,description):
  119.         self.description = description
  120.  
  121.     def set_name(self,name):
  122.         self.name = name
  123.  
  124.     def get_name(self):
  125.         return str(self.name)
  126.  
  127.     def get_description(self):
  128.         return str(self.description)
  129.  
  130.     def get_character(self):
  131.         return self.character
  132.  
  133.     def set_character(self,character):
  134.         self.character = character
  135.  
  136.     def describe(self):
  137.         print(self.get_name().upper() + '\n' + '----------')
  138.         print(self.get_description())
  139.         if self.get_character() is not None:
  140.             character = self.get_character()
  141.             print('*** INHABITANT IN ROOM ***')
  142.             character.describe()
  143.         for direction in self.linkedRooms:
  144.             room = self.linkedRooms[direction]
  145.             print( "The " + room.get_name() + " is " + direction)
  146.         #for direction in self.linkedRooms:
  147.         #    room = self.linkedRooms[direction]
  148.         #    print(self.linkedRooms[direction] + ' > ' + direction)
  149.             #print(room.get_name())
  150.         #print('Directions: ' + repr(self.linkedRooms) + '\n')
  151.  
  152.     def set_linkedRooms(self,direction,room):
  153.         self.linkedRooms[direction] = room
  154.  
  155.     def get_linkedRooms(self,direction):
  156.         return self.linkedRooms[direction]
  157.  
  158.     def move(self,direction):
  159.         if direction in self.linkedRooms:
  160.             print('Moving {} '.format(direction))
  161.             return self.linkedRooms[direction]
  162.         else:
  163.             print('You cannot move {}'.format(direction))
  164.             return self
  165.  
  166.  
  167.  
  168.  
  169. from room import Room
  170. from character import Enemy
  171. from character import Friend
  172.  
  173. def createMap(kitchen,diningroom,ballroom):
  174.     kitchen.set_linkedRooms('south',diningroom)
  175.     diningroom.set_linkedRooms('north',kitchen)
  176.     diningroom.set_linkedRooms('west',ballroom)
  177.     ballroom.set_linkedRooms('east',diningroom)
  178.  
  179. def addDescriptions(kitched,diningroom,ballroom):
  180.     kitchen.set_description('A small room with a big table')
  181.     diningroom.set_description('A large room with ornate golden decorations on each wall')
  182.     ballroom.set_description('A vast room with a shiny wooden floor; huge candlesticks guard the entrance')
  183.  
  184. def fullMap(rooms):
  185.     for r in rooms:
  186.         r.describe()
  187.  
  188. def getAction():
  189.     response = input('Enter Action: ').strip().lower()
  190.     return response
  191.  
  192. def displayActions():
  193.     print('exit > Leave Game' + '\n' + 'north, south, east, west > Move in that direction' + '\n' + 'display > show room details')
  194.  
  195.  
  196. def prepareEnemy():
  197.     zach = Enemy('Zach','A Zombie and he wants to eat your brain')
  198.     zach.set_weapon('scissors')
  199.     zach.set_conversation('Grrr')
  200.     #zach.talk()
  201.     #zach.describe()
  202.     return zach
  203.  
  204. def prepareFriend():
  205.     fred = Friend('Fred','A friendly person who will make you a brew')
  206.     fred.set_items('rock')
  207.     fred.set_conversation('Hello')
  208.     #fred.talk()
  209.     #fred.describe()    
  210.     return fred
  211.    
  212. def checkForCharacter(currentRoom):
  213.     if currentRoom.get_character() == None:
  214.         return False
  215.     else:
  216.         return True
  217.  
  218. #Main Program
  219. kitchen = Room('Kitchen')
  220. ballroom = Room('Ballroom')
  221. diningroom = Room('Dining Room')
  222. rooms = [kitchen,ballroom, diningroom]
  223.  
  224. createMap(kitchen,diningroom,ballroom)
  225. addDescriptions(kitchen,diningroom,ballroom)
  226.  
  227. zach = prepareEnemy()
  228. fred = prepareFriend()
  229.  
  230. diningroom.set_character(fred)
  231. ballroom.set_character(zach)
  232.  
  233. currentRoom = kitchen
  234. currentRoom.describe()
  235.  
  236. #fullMap(rooms)
  237.  
  238. play = True
  239. while play:
  240.     action = getAction()
  241.     play = not (action == 'exit')
  242.     if action == 'exit':
  243.         play = False
  244.     elif action == 'display':
  245.         currentRoom.describe()
  246.     elif action == 'help':
  247.         displayActions()
  248.     elif action == 'fight':
  249.         if checkForCharacter(currentRoom):
  250.             character = currentRoom.get_character()
  251.             if character.characterType() == 'Enemy':
  252.                 weapon = input('Choose your weapon (rock/paper/scissors)').strip().lower()
  253.                 result = character.fight(weapon)
  254.                 if result == 'Win':
  255.                     currentRoom.set_character(None)
  256.                 elif result == 'Lose':
  257.                     play = False
  258.                 else:
  259.                     currentRoom.describe()
  260.             else:
  261.                 character.fight()
  262.         else:
  263.             print('Nobody here to fight')
  264.     elif action == 'talk':
  265.         if checkForCharacter(currentRoom):
  266.             character = currentRoom.get_character()
  267.             character.talk()
  268.             if character.characterType() == 'Friend':
  269.                 character.giveGift()
  270.         else:
  271.             print('There is nobody here to talk to')
  272.     elif action in ['north','south','east','west']:
  273.         print('Action {}'.format(action))
  274.         currentRoom = currentRoom.move(action)
  275.         currentRoom.describe()
  276.     else:
  277.         print('{} is an invalid action'.format(action))
  278.         print('Possible Actions - display, help, fight, talk, north, south, east, west, exit')
  279.  
  280. print('End of Game')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement