Advertisement
richwhilecooper

Complete prototype adventure game main.py , player.py, class

Nov 30th, 2018
543
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 15.05 KB | None | 0 0
  1. #####ALL THE CODE FOR MY ADVENTURE GAME######
  2. ###Main PROGRAM - PLEAE NOTE YOU WILL NEED THE CLASSES FOR THIS TO WORKS (SEE BELOW)##############################################
  3. ######SVAE AS main.py ############################################################################################################
  4. """import classes"""
  5. from room import Room
  6. from item import Item
  7. from character import Character
  8. from character import Enemy
  9. from character import Friend
  10. from player import Player
  11. ###set up player
  12. """set up instance from Player class"""
  13. protag = Player("Doug the Dauntless")
  14. protag.inventory = [["health potion",":a potion to restore health"],
  15.                     ["health potion",":a potion to restore health"],
  16.                     ["dagger", ":suitable only as a letter opener"],
  17.                     ["note",":the note is from your mom excusing you from PE"]]
  18. protag.health = 100
  19.  
  20.  
  21. ###set up a bunch of rooms using Room class
  22. """Use Room class to creat instances of rooms and the lonks between them"""
  23. kitchen = Room("Kitchen")
  24. kitchen.set_description("The kitchen is gleaming and immaculate apart from the dead body on the floor")
  25.  
  26. pantry = Room("Pantry")
  27. pantry.set_description("The pantry is mostly empty, apart from a dead rat and something on a shelf.")
  28.  
  29. dining_room = Room("Dining room")
  30. dining_room.set_description("The table is set but thick with dust.")
  31.  
  32. corridor = Room ("East Corridor")
  33. corridor.set_description("The East Corridor connects the Dining Room to the Entrance Hall.\n\
  34. Broken glass litters the floor. The Entrance Hall door is locked")
  35.  
  36. entrance_hall = Room ("Entrance Hall")
  37. entrance_hall = Room ("The Entrance Hall is the winning destination for now!")
  38.  
  39. ##set up links between rooms using Room class
  40. """links between rooms"""
  41. kitchen.link_room(pantry,"North")
  42. pantry.link_room(kitchen,"South")
  43. kitchen.link_room(dining_room,"West")
  44. dining_room.link_room(kitchen,"East")
  45. dining_room.link_room(corridor,"West")
  46. corridor.link_room(dining_room,"East")
  47. entrance_hall.link_room(corridor,"East")
  48.  
  49. ##set enemy character
  50. """use the sub-class of Character to define enemies"""
  51. dave = Enemy("Dave", "smelly zombie")
  52. dave.set_conversation("Uurrrrgghhhhh! Can I talk to you about PPI? <HE LOOKS LIKE HE MIGHT BITE YOU>")
  53. dave.set_weakness("bacon")
  54. dining_room.set_character(dave)
  55.  
  56. bob = Enemy("Bob","rusty knight")
  57. bob.set_conversation("You shall not pass!")
  58. bob.set_bribe_weakness("metal polish")
  59. bob.set_reward("Bob is pleased and lets you pass")
  60. corridor.set_character(bob)
  61.  
  62. ##set friendly character
  63. """use the subclass Friend of Character to set up a friendly character"""
  64. susan = Friend("Susan","golden unicorn, about 12 inches tall")
  65. susan.set_strengths("icecream")
  66. susan.set_reward(["metal polish",":great for polishing metal!"])
  67. susan.set_conversation("TRALA - LA - LA -LA - LAAAA ... <Susan begins singing in a high-pitched voice.  She seems unaware of you.>")
  68. kitchen.set_character(susan)
  69.  
  70. ###create some items and put them in a room
  71. """Use the Item class to create instances of items and place them in Rooms"""
  72. sword = Item ("sword")
  73. sword.set_item_description("The sword is 4 ft long with a basket hilt.  It is gleaming and sharp.")
  74. pantry.set_item(sword)
  75.  
  76. bacon = Item ("piece of bacon")
  77. bacon.set_item_description("The bacon is old and mouldy.")
  78. kitchen.set_item(bacon)
  79.  
  80. icecream = Item("carton of icecream")
  81. icecream.set_item_description("It's neoplitan flavour!")
  82. pantry.set_item(icecream)
  83.  
  84. ##set starting room
  85. """set the room you start in"""
  86. starting_room = kitchen
  87.  
  88. ###Main Loop###
  89. """Set the main game loop which also sets the commands that players can perform"""
  90. while True:
  91.     print("\n")        
  92.     starting_room.get_details()
  93.     inhabited = starting_room.get_character()
  94.     """gets basic room info & checks to see if a room has a character in it"""
  95.     if  isinstance(inhabited,Enemy):
  96.         print ("BEWARE! ENEMIES ARE NEAR!")
  97.     elif isinstance(inhabited,Friend):
  98.         print ("THERE IS SOMEONE NEAR WHO MAY HELP YOU!")
  99.     room_item = starting_room.get_item()
  100.  
  101.     """Player can give commands - like a direction to move or to talk"""
  102.     command = input("> ")
  103.     command = command.title()
  104.     if command in ["North","South","East","West"]:
  105.         starting_room = starting_room.move(command)
  106.     elif command =="Talk":
  107.         if inhabited is not None:
  108.             print ("You try to talk to",inhabited.name)
  109.             inhabited.talk()
  110.         else:
  111.             print ("Are you crazy - there is no-one to talk to!")
  112.    
  113. ###FIGHT
  114.             """the fight action"""
  115.     elif command =="Fight":
  116.         if inhabited is not None:
  117.             fight_with = input("What will you fight with? >>> ")
  118.             protag.set_inv_item = fight_with
  119.             in_invent = protag.check_inventory(fight_with)
  120.            
  121.             if in_invent == False:
  122.                 print ("You do not have",fight_with,"!")
  123.             else:
  124.                 if inhabited.fight(fight_with) == False:
  125.                     break
  126.         else:
  127.             print ("As there is no enemy to fight, you settle for punching yourself in the face a few times.")
  128.             print ("You quickly tire of this pointless activity.")
  129.             """the bribe option"""
  130.     ###BRIBE
  131.     elif command =="Bribe":
  132.         if inhabited is not None:
  133.             bribe_with= input("What will you bribe with? >>> ")
  134.             protag.set_inv_item = bribe_with
  135.             in_invent = protag.check_inventory(bribe_with)
  136.             if in_invent == False:
  137.                 print ("You do not have",bribe_with,"!")
  138.             else:
  139.                 if inhabited.bribe(bribe_with) == False:
  140.                     break
  141.                 elif inhabited.name == "Bob":
  142.                     starting_room = entrance_hall
  143.                     break
  144.         else:
  145.             print ("As there is no enemy to bribe, you wave the",bribe_with,"in the air")
  146.             print ("It resolves nothing. You quickly tire of this pointless activity.")
  147.         """The look option"""
  148.    ###LOOK
  149.     elif command =="Look":
  150.         if inhabited is not None:
  151.             inhabited.describe()
  152.         if room_item is not None:
  153.             room_item.get_item_details()
  154.         """The Give option"""
  155.     ###Give
  156.     elif command =="Give":
  157.         inventory = protag.inventory
  158.         if inhabited is not None:
  159.             give_with= input("What will you give? >>> ")
  160.             protag.set_inv_item = give_with
  161.             in_invent = protag.check_inventory(give_with)
  162.             if in_invent == False:
  163.                 print ("You do not have",give_with,"!")
  164.             else:
  165.                 if inhabited.hug(give_with):
  166.                     print (inhabited.name, "rewards you with",inhabited.reward)
  167.                     inventory.append(inhabited.reward)                
  168.         else:
  169.             print ("As there is no-one to give to, you wave the",give_with,"in the air")
  170.             print ("It resolves nothing. You quickly tire of this pointless activity.")
  171.  
  172.     ###STATUS
  173.             """Status shows your character info including inventory"""
  174.     elif command =="Status":
  175.         protag.describe()
  176.  
  177.     ###GET
  178.         """allows you to get items and add them to your inventory"""
  179.     elif command =="Get":
  180.         if room_item is not None:
  181.             get_what = input("What will you get? >>> ")
  182.             if get_what in room_item.item:
  183.                 inventory = protag.inventory
  184.                 got = [room_item.item,room_item.item_description]
  185.                 inventory.append(got)
  186.                 print ("You got the ", room_item.item)
  187.                 room_item.item = None
  188.                 room_item.item_description = None
  189.                
  190.             else:
  191.                 print ("You can't get that!")
  192.         else:
  193.             print ("There is nothing for you to get here.")
  194. print ("Game Over")
  195.  
  196. ######ROM CLASS - SAVE AS A SEPERATE FILE room.py ######################################################
  197. ###Room Class
  198.  
  199. class Room():
  200.     def __init__(self,room_name):
  201.         self.name = room_name
  202.         self.description = None
  203.         self.linked_rooms = {}
  204.         self.character = None
  205.         self.item = None
  206.  
  207.     def set_character(self, new_character):
  208.         self.character = new_character
  209.  
  210.     def get_character(self):
  211.         return self.character
  212.  
  213.     def set_item(self, new_item):
  214.         self.item = new_item
  215.  
  216.     def get_item(self):
  217.         return self.item    
  218.        
  219.     def set_description(self,room_description):
  220.         self.description = room_description
  221.        
  222.     def get_description(self):
  223.         return self.description
  224.  
  225.     def get_name(self):
  226.         return self.name
  227.    
  228.     def set_name(self,room_name):
  229.         self.name = room_name
  230.  
  231.     def describe(self):
  232.         print (self.description)
  233.  
  234.     def link_room(self, room_to_link, direction):
  235.         self.linked_rooms[direction] = room_to_link
  236.         return (self.name + " linked rooms: "+ repr(self.linked_rooms))
  237.  
  238.     def get_details(self):
  239.         print (self.name)
  240.         self.describe()
  241.         for direction in self.linked_rooms:
  242.             room = self.linked_rooms[direction]
  243.             print ("The " + room.get_name() + " is " + direction)
  244.            
  245.  
  246.     def move(self, direction):
  247.         if direction in self.linked_rooms:
  248.             return self.linked_rooms[direction]
  249.         else:
  250.             print("You can't go that way")
  251.             return self
  252.  
  253.  
  254. #######ITEM CLASS SAVE AS item.py ###########################################################################################
  255. ###Room Item
  256.  
  257. class Item():
  258.     ##init class
  259.     def __init__(self,item_name):
  260.         self.item = item_name
  261.         self.item_description = None
  262.         #self.item_room = {}
  263.  
  264.     ##define item description as a thing    
  265.     def set_item_description(self,item_description):
  266.         self.item_description = item_description
  267.        
  268.     ##get item desrciption
  269.     def get_item_description(self):
  270.         return self.item_description
  271.  
  272.     ##set item name
  273.     def set_item(self,item):
  274.         self.item = item
  275.  
  276.     ##get item name
  277.     def get_item_name(self):
  278.         return self.item
  279.  
  280.  
  281.     def get_item_details(self):
  282.         print ("A", self.item, "is here!")
  283.         print (self.item_description)
  284.  
  285.            
  286. #####CHARACTER CLASS - save as character.py ############################################################################            
  287. class Character():
  288.  
  289.     # Create a character
  290.     def __init__(self, char_name, char_description):
  291.         self.name = char_name
  292.         self.description = char_description
  293.         self.conversation = None
  294.  
  295.     # Describe this character
  296.     def describe(self):
  297.         print( self.name + " is here!" )
  298.         print( self.name,"is a", self.description )
  299.  
  300.     # Set what this character will say when talked to
  301.     def set_conversation(self, conversation):
  302.         self.conversation = conversation
  303.  
  304.     # Talk to this character
  305.     def talk(self):
  306.         if self.conversation is not None:
  307.             print("[" + self.name + " says]: " + self.conversation)
  308.         else:
  309.             print(self.name + " doesn't want to talk to you")
  310.  
  311.     # Fight with this character
  312.     def fight(self, combat_item):
  313.         print(self.name + " doesn't want to fight with you")
  314.         return True
  315.  
  316.     def bribe(self,bribe_item):
  317.         print(self.name + " is incorrutable and may not be bribed.")
  318.         return True
  319.  
  320. class Enemy(Character):
  321.     def __init__(self, char_name, char_description):
  322.         super().__init__(char_name, char_description)
  323.         self.weakness = None
  324.         self.bribe_weakness = None
  325.         self.reward = None
  326.  
  327.     ##fighting
  328.     def fight(self, combat_item):
  329.         if combat_item == self.weakness:
  330.             print("You fend " + self.name + " off with the " + combat_item )
  331.             return True
  332.         else:
  333.             print(self.name + " crushes you, puny adventurer")
  334.             return False
  335.  
  336.     ##bribing
  337.     def bribe(self, bribe_item):
  338.         if bribe_item == self.bribe_weakness:
  339.             print("You present " + self.name + " with the " + bribe_item,".They appear satisfied.")
  340.             print (self.reward)    
  341.             return True
  342.         else:
  343.             print(self.name + " takes offence at your offer and crushes you, puny adventurer")
  344.             return False
  345.        
  346.     ##set get weakness  
  347.     def set_weakness(self,item_weakness):
  348.         self.weakness = item_weakness
  349.  
  350.     def get_weakness(self):
  351.         return self.weakness
  352.  
  353.     ##set get weakness  
  354.     def set_bribe_weakness(self,bribe_item_weakness):
  355.         self.bribe_weakness = bribe_item_weakness
  356.  
  357.     def get_bribe_weakness(self):
  358.         return self.bribe_weakness
  359.  
  360.     ##set get reward
  361.     def set_reward(self,item_reward):
  362.         self.reward = item_reward
  363.  
  364.     def get_reward(self):
  365.         return self.reward
  366.        
  367.  
  368. class Friend(Character):
  369.     def __init__(self, char_name, char_description):
  370.         super().__init__(char_name, char_description)
  371.         self.strengths = None
  372.         self.reward = None
  373.     ##fighting
  374.     def hug(self, gift_item):
  375.         if gift_item == self.strengths:
  376.             print("You reward " + self.name + " with the " + gift_item )
  377.             return True
  378.         elif gift_item == "Hug":
  379.             print("You reward " + self.name + " with the " + gift_item )
  380.             print ("You both get a warm feeling, but that's all.")
  381.         else:
  382.             print(self.name + " ignores you, totally.")
  383.  
  384.     ##set get strengths  
  385.     def set_strengths(self,item_strengths):
  386.         self.strengths = item_strengths
  387.  
  388.     def get_strengths(self):
  389.         return self.strengths
  390.    
  391.     ##set get reward
  392.     def set_reward(self,item_reward):
  393.         self.reward = item_reward
  394.  
  395.     def get_reward(self):
  396.         return self.reward
  397.  
  398.  
  399. ######PLAYER CLASS - save as player.py #################################################################################
  400. ###Player Class
  401.  
  402. class Player():
  403.     def __init__(self,player_name):
  404.         self.name = player_name
  405.         self.health = None
  406.         self.inventory = []
  407.  
  408.     def set_inventory(self,inventory_list):
  409.         self.inventory = inventory_list
  410.  
  411.     def get_inventory(self):
  412.         return self.inventory
  413.  
  414.     def set_health(self,health_points):
  415.         self.health = health_points
  416.  
  417.     def get_health(self):
  418.         return self.health
  419.  
  420.     def describe(self):
  421.         print ("You name is, ", self.name)
  422.         print ("You are carrying:")
  423.         for item in self.inventory:
  424.             print (item[0], item[1])
  425.         print ()
  426.         print ("Your health is :", self.health)
  427.  
  428.     def set_inv_item(self,inv_item):
  429.         self.item = inv_item
  430.  
  431.     def get_inv_item(self):
  432.         return self.item
  433.  
  434.     def check_inventory(self,item):
  435.         in_inv = False
  436.         for thing in self.inventory:
  437.             if item in thing[0]:
  438.                 in_inv = True
  439.                 break
  440.         return in_inv
  441.        
  442.  
  443.     #def inventory_append(self):
  444.        
  445. ###########TAHTS IT!  #################################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement