bensimmo

OOP 3.7 main.py

Jan 12th, 2021
407
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #main.py
  2. #-------
  3. from room import Room
  4. from item import Item
  5. from character import Character, Enemy, Animal
  6.  
  7. # setup rooms
  8. kitchen = Room("kitchen")
  9. kitchen.set_description("a dank and dirty room buzzing with flies")
  10.  
  11. ballroom = Room("ballroom")
  12. ballroom.set_description("a large clean room with a sparkling floor")
  13.  
  14. dining_hall = Room("dining hall")
  15. dining_hall.set_description("a dimly lit room with scraps of food surrounding a table")
  16.  
  17. office = Room("office")
  18. office.set_description("a small room with bright LED lighting, looks a nice place to work")
  19.  
  20. # setup items
  21. table = Item("table")
  22. table.set_description("round and has places for four people")
  23. table.set_mass("heavy")
  24. table.set_material("metal")
  25.  
  26. sword = Item("sword", "light", "wood")
  27. sword.set_description("long and pointy with a small handle, it has lots of small holes in it")
  28.  
  29. cheese = Item("cheese", "light", "cheese")
  30. cheese.set_description("Smelly, a little bit gooey")
  31.  
  32. #setup room maze
  33. kitchen.link_room(dining_hall, "south")
  34. ballroom.link_room(dining_hall, "east")
  35. dining_hall.link_room(kitchen, "north")
  36. dining_hall.link_room(ballroom, "west")
  37. dining_hall.link_room(office, "south")
  38. office.link_room(dining_hall, "north")
  39.  
  40.  
  41. #setup characters
  42. dave = Enemy("Dave", "A smelly zombie")
  43. dave.set_conversation("Brrlgrh... rgrhl... brains...")
  44. dave.set_weakness("sword")
  45.  
  46. fred = Character("Fred", "Busy making computer games")
  47. fred.set_conversation("Sorry, to busy, send me an email")
  48.  
  49. cat = Animal("Cat", "Laid down, half sleeping. Acts like it owns the place")
  50. cat.set_huggable(True)
  51. cat.set_food("cheese")
  52. cat.set_hug_sound("purrrrr")
  53.  
  54.  
  55. #setup character locations
  56. dining_hall.set_character(dave)
  57. office.set_character(fred)
  58. kitchen.set_character(cat)
  59.  
  60.  
  61.  
  62. #set starting room
  63. current_room = kitchen
  64.  
  65. # test items for now
  66. table.get_details()
  67. sword.get_details()
  68.  
  69. #start game
  70. alive = True
  71.  
  72. while alive:
  73.     current_room.get_details()
  74.     inhabitant = current_room.get_character()
  75.     if inhabitant is not None:
  76.         inhabitant.describe()
  77.    
  78.     # loop so we don't display room details after every action.
  79.     while True:
  80.         command = input(" What would would you like to do? >")
  81.        
  82.         # Check whether a direction was typed
  83.         if command in ["north", "south", "east", "west"]:
  84.             in_room = current_room
  85.             current_room = current_room.move(command)
  86.             if current_room == in_room:
  87.                 pass
  88.             else:
  89.                 break # Moved so we need to start the main loop again
  90.        
  91.         elif command == "talk":
  92.             inhabitant.talk()
  93.        
  94.         elif command == "fight":
  95.             if isinstance(inhabitant, Enemy):
  96.                 fight_item = input(" What item do you want to use >?")
  97.                 if inhabitant.fight(fight_item):
  98.                     current_room.character = None
  99.                 else:
  100.                     print("You died")
  101.                     alive = False
  102.                     break # You're dead, so no more action, break this while and the the main while using above
  103.             else:
  104.                 print("You cannot fight anyone")
  105.  
  106.         elif command == "info":
  107.             break
  108.        
  109.         elif command == "feed":
  110.             if isinstance(inhabitant, Animal):
  111.                 feed_item = input(" What item do you want to use >?")
  112.                 if inhabitant.feed(feed_item):
  113.                     current_room.character = None
  114.                     print(f"{inhabitant.name} has left the room")
  115.             else:
  116.                 print("you cannot feed anyone")
  117.  
  118.         elif command == "hug":
  119.             if isinstance(inhabitant, Animal):
  120.                 inhabitant.hug()
  121.             elif isinstance(inhabitant, Enemy):
  122.                 print(f"{inhabitant.name} is an Enemy")
  123.             else:
  124.                 print("nothing to hug")
  125.            
  126.  
  127. #################################
  128. #character.py
  129. #-----------
  130. class Character():
  131.  
  132.     # Create a character
  133.     def __init__(self, char_name, char_description):
  134.         self.name = char_name
  135.         self.description = char_description
  136.         self.conversation = None
  137.  
  138.     # Describe this character
  139.     def describe(self):
  140.         print( f"{self.name} is here!" )
  141.         print( self.description )
  142.  
  143.     # Set what this character will say when talked to
  144.     def set_conversation(self, conversation):
  145.         self.conversation = conversation
  146.  
  147.     # Talk to this character
  148.     def talk(self):
  149.         if self.conversation is not None:
  150.             print(f"[{self.name} says]: {self.conversation}")
  151.         else:
  152.             print(f"{self.name} doesn't want to talk to you")
  153.  
  154.     # Fight with this character
  155.     def fight(self, combat_item):
  156.         print(f"{self.name} doesn't want to fight with you")
  157.         return True
  158.  
  159.  
  160.  
  161. '''
  162. The name of this new class is Enemy.
  163. Putting Character inside the brackets tells Python that the Enemy class will inherit all of the methods from Character.
  164. Character is called the superclass of Enemy, and Enemy is a subclass of Character.
  165. '''
  166. class Enemy(Character):
  167.     #making the __init__ here stops it using the one from Character class
  168.     def __init__(self, char_name, char_description):
  169.         #so we need to tell it we still want to with super().
  170.         super().__init__(char_name, char_description)
  171.         self.weakness = None
  172.  
  173.     def set_weakness(self, item_name):
  174.         self.weakness = item_name
  175.        
  176.     def get_weakness(self):
  177.         # use print for testing
  178.         # print(f"{self.name} has a weakness for {self.weakness}")
  179.         return self.weakness
  180.    
  181.     def fight(self, combat_item):
  182.         if combat_item == self.weakness:
  183.             print(f"You fend {self.name} off with the {combat_item}")
  184.             return True
  185.         else:
  186.             print(f"{self.name} crushes you, puny adventurer")
  187.             return False
  188.    
  189.  
  190. class Animal(Character):
  191.     #making the __init__ here stops it using the one from Character class
  192.     def __init__(self, char_name, char_description):
  193.         #so we need to tell it we still want to with super().
  194.         super().__init__(char_name, char_description)
  195.         self.huggable = False
  196.         self.hug_sound = None
  197.         self.food = None
  198.    
  199.     def set_huggable(self, is_huggable):
  200.         self.huggable = is_huggable
  201.        
  202.     def get_huggable(self):
  203.         return self.huggable
  204.    
  205.     def set_hug_sound(self, sound):
  206.         self.hug_sound = sound
  207.        
  208.     def get_hug_sound(self):
  209.         return self.hug_sound
  210.    
  211.     def set_food(self, food):
  212.         self.food = food
  213.        
  214.     def get_food(self):
  215.         return self.food
  216.    
  217.     def talk(self):
  218.         if self.conversation is not None:
  219.             print(f"[{self.name} says]: {self.conversation}")
  220.         else:
  221.             print(f"{self.name} cannot talk")
  222.  
  223.     def hug(self):
  224.         if self.huggable is not None:
  225.             print(f"[{self.name}]: {self.hug_sound}")
  226.             return True
  227.         else:
  228.             print(f"{self.name} does not like hugs")
  229.             return False
  230.  
  231.     def feed(self, food_item):
  232.         print(self.food, food_item)
  233.         if self.food is not None:
  234.             if food_item == self.food:
  235.                 print(f"You fed {self.name} with {food_item}, {self.name} like it a lot")
  236.                 return True
  237.             else:
  238.                 print(f"{self.name} looks at you in disgust.")
  239.                 return False
  240.         else:
  241.             print(f"{self.name} is not hungry.")
  242.             return False
  243.        
  244.  
  245.  
  246. #############
  247. #item.py
  248. class Item():
  249.     def __init__(self, item_name, item_mass = None, item_material = None ):
  250.         self.name = item_name
  251.         self.description = None
  252.         self.mass = item_mass
  253.         self.material = item_material
  254.  
  255.     def get_name(self):
  256.         return self.name
  257.  
  258.     def set_name(self, item_name):
  259.         self.name = item_name
  260.  
  261.     def get_description(self):
  262.         return self.description
  263.  
  264.     def set_description(self, item_description):
  265.         self.description = item_description
  266.  
  267.     def get_mass(self):
  268.         return self.mass
  269.  
  270.     def set_mass(self, item_mass):
  271.         self.mass = item_mass
  272.  
  273.     def get_material(self):
  274.         return self.material
  275.  
  276.     def set_material(self, item_material):
  277.         self.material = item_material
  278.  
  279.     def get_details(self):
  280.         print(f"The {self.name} is {self.mass}, it is made from {self.material}.")
  281.         print(f"It is {self.description}.")
  282.  
  283.  
  284. ####################
  285. #room.py
  286. class Room():
  287.     def __init__(self, room_name):
  288.         self.name = room_name
  289.         self.description = None
  290.         self.linked_rooms = {}
  291.         self.character = None
  292.  
  293.     def set_description(self, room_description):
  294.         self.description = room_description
  295.  
  296.     def get_description(self):
  297.         return self.description
  298.  
  299.     def set_name(self, room_name):
  300.         self.name = room_name
  301.  
  302.     def get_name(self):
  303.         return self.name
  304.  
  305.     def set_character(self, character_name):
  306.         self.character = character_name
  307.  
  308.     def get_character(self):
  309.         return self.character
  310.    
  311.     def describe(self):
  312.         print(self.description)
  313.  
  314.     def link_room(self, room_to_link, direction):
  315.         self.linked_rooms[direction] = room_to_link
  316.         # print( f"{self.name} linked rooms : {repr(self.linked_rooms)}" )
  317.  
  318.     def get_details(self):
  319.         print("\n==")
  320.         print(f"You are in the {self.name}, it is {self.description}.")
  321.         print("~~")
  322.         for direction in self.linked_rooms:
  323.             room = self.linked_rooms[direction]
  324.             print( f"The {room.get_name()} is {direction}")
  325.  
  326.     def move(self, direction):
  327.         if direction in self.linked_rooms:
  328.             return self.linked_rooms[direction]
  329.         else:
  330.             print("You can't go that way")
  331.             return self
  332.  
  333.  
RAW Paste Data