Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #####ALL THE CODE FOR MY ADVENTURE GAME######
- ###Main PROGRAM - PLEAE NOTE YOU WILL NEED THE CLASSES FOR THIS TO WORKS (SEE BELOW)##############################################
- ######SVAE AS main.py ############################################################################################################
- """import classes"""
- from room import Room
- from item import Item
- from character import Character
- from character import Enemy
- from character import Friend
- from player import Player
- ###set up player
- """set up instance from Player class"""
- protag = Player("Doug the Dauntless")
- protag.inventory = [["health potion",":a potion to restore health"],
- ["health potion",":a potion to restore health"],
- ["dagger", ":suitable only as a letter opener"],
- ["note",":the note is from your mom excusing you from PE"]]
- protag.health = 100
- ###set up a bunch of rooms using Room class
- """Use Room class to creat instances of rooms and the lonks between them"""
- kitchen = Room("Kitchen")
- kitchen.set_description("The kitchen is gleaming and immaculate apart from the dead body on the floor")
- pantry = Room("Pantry")
- pantry.set_description("The pantry is mostly empty, apart from a dead rat and something on a shelf.")
- dining_room = Room("Dining room")
- dining_room.set_description("The table is set but thick with dust.")
- corridor = Room ("East Corridor")
- corridor.set_description("The East Corridor connects the Dining Room to the Entrance Hall.\n\
- Broken glass litters the floor. The Entrance Hall door is locked")
- entrance_hall = Room ("Entrance Hall")
- entrance_hall = Room ("The Entrance Hall is the winning destination for now!")
- ##set up links between rooms using Room class
- """links between rooms"""
- kitchen.link_room(pantry,"North")
- pantry.link_room(kitchen,"South")
- kitchen.link_room(dining_room,"West")
- dining_room.link_room(kitchen,"East")
- dining_room.link_room(corridor,"West")
- corridor.link_room(dining_room,"East")
- entrance_hall.link_room(corridor,"East")
- ##set enemy character
- """use the sub-class of Character to define enemies"""
- dave = Enemy("Dave", "smelly zombie")
- dave.set_conversation("Uurrrrgghhhhh! Can I talk to you about PPI? <HE LOOKS LIKE HE MIGHT BITE YOU>")
- dave.set_weakness("bacon")
- dining_room.set_character(dave)
- bob = Enemy("Bob","rusty knight")
- bob.set_conversation("You shall not pass!")
- bob.set_bribe_weakness("metal polish")
- bob.set_reward("Bob is pleased and lets you pass")
- corridor.set_character(bob)
- ##set friendly character
- """use the subclass Friend of Character to set up a friendly character"""
- susan = Friend("Susan","golden unicorn, about 12 inches tall")
- susan.set_strengths("icecream")
- susan.set_reward(["metal polish",":great for polishing metal!"])
- susan.set_conversation("TRALA - LA - LA -LA - LAAAA ... <Susan begins singing in a high-pitched voice. She seems unaware of you.>")
- kitchen.set_character(susan)
- ###create some items and put them in a room
- """Use the Item class to create instances of items and place them in Rooms"""
- sword = Item ("sword")
- sword.set_item_description("The sword is 4 ft long with a basket hilt. It is gleaming and sharp.")
- pantry.set_item(sword)
- bacon = Item ("piece of bacon")
- bacon.set_item_description("The bacon is old and mouldy.")
- kitchen.set_item(bacon)
- icecream = Item("carton of icecream")
- icecream.set_item_description("It's neoplitan flavour!")
- pantry.set_item(icecream)
- ##set starting room
- """set the room you start in"""
- starting_room = kitchen
- ###Main Loop###
- """Set the main game loop which also sets the commands that players can perform"""
- while True:
- print("\n")
- starting_room.get_details()
- inhabited = starting_room.get_character()
- """gets basic room info & checks to see if a room has a character in it"""
- if isinstance(inhabited,Enemy):
- print ("BEWARE! ENEMIES ARE NEAR!")
- elif isinstance(inhabited,Friend):
- print ("THERE IS SOMEONE NEAR WHO MAY HELP YOU!")
- room_item = starting_room.get_item()
- """Player can give commands - like a direction to move or to talk"""
- command = input("> ")
- command = command.title()
- if command in ["North","South","East","West"]:
- starting_room = starting_room.move(command)
- elif command =="Talk":
- if inhabited is not None:
- print ("You try to talk to",inhabited.name)
- inhabited.talk()
- else:
- print ("Are you crazy - there is no-one to talk to!")
- ###FIGHT
- """the fight action"""
- elif command =="Fight":
- if inhabited is not None:
- fight_with = input("What will you fight with? >>> ")
- protag.set_inv_item = fight_with
- in_invent = protag.check_inventory(fight_with)
- if in_invent == False:
- print ("You do not have",fight_with,"!")
- else:
- if inhabited.fight(fight_with) == False:
- break
- else:
- print ("As there is no enemy to fight, you settle for punching yourself in the face a few times.")
- print ("You quickly tire of this pointless activity.")
- """the bribe option"""
- ###BRIBE
- elif command =="Bribe":
- if inhabited is not None:
- bribe_with= input("What will you bribe with? >>> ")
- protag.set_inv_item = bribe_with
- in_invent = protag.check_inventory(bribe_with)
- if in_invent == False:
- print ("You do not have",bribe_with,"!")
- else:
- if inhabited.bribe(bribe_with) == False:
- break
- elif inhabited.name == "Bob":
- starting_room = entrance_hall
- break
- else:
- print ("As there is no enemy to bribe, you wave the",bribe_with,"in the air")
- print ("It resolves nothing. You quickly tire of this pointless activity.")
- """The look option"""
- ###LOOK
- elif command =="Look":
- if inhabited is not None:
- inhabited.describe()
- if room_item is not None:
- room_item.get_item_details()
- """The Give option"""
- ###Give
- elif command =="Give":
- inventory = protag.inventory
- if inhabited is not None:
- give_with= input("What will you give? >>> ")
- protag.set_inv_item = give_with
- in_invent = protag.check_inventory(give_with)
- if in_invent == False:
- print ("You do not have",give_with,"!")
- else:
- if inhabited.hug(give_with):
- print (inhabited.name, "rewards you with",inhabited.reward)
- inventory.append(inhabited.reward)
- else:
- print ("As there is no-one to give to, you wave the",give_with,"in the air")
- print ("It resolves nothing. You quickly tire of this pointless activity.")
- ###STATUS
- """Status shows your character info including inventory"""
- elif command =="Status":
- protag.describe()
- ###GET
- """allows you to get items and add them to your inventory"""
- elif command =="Get":
- if room_item is not None:
- get_what = input("What will you get? >>> ")
- if get_what in room_item.item:
- inventory = protag.inventory
- got = [room_item.item,room_item.item_description]
- inventory.append(got)
- print ("You got the ", room_item.item)
- room_item.item = None
- room_item.item_description = None
- else:
- print ("You can't get that!")
- else:
- print ("There is nothing for you to get here.")
- print ("Game Over")
- ######ROM CLASS - SAVE AS A SEPERATE FILE room.py ######################################################
- ###Room Class
- class Room():
- def __init__(self,room_name):
- self.name = room_name
- self.description = None
- self.linked_rooms = {}
- self.character = None
- self.item = None
- def set_character(self, new_character):
- self.character = new_character
- def get_character(self):
- return self.character
- def set_item(self, new_item):
- self.item = new_item
- def get_item(self):
- return self.item
- def set_description(self,room_description):
- self.description = room_description
- def get_description(self):
- return self.description
- def get_name(self):
- return self.name
- def set_name(self,room_name):
- self.name = room_name
- def describe(self):
- print (self.description)
- def link_room(self, room_to_link, direction):
- self.linked_rooms[direction] = room_to_link
- return (self.name + " linked rooms: "+ repr(self.linked_rooms))
- def get_details(self):
- print (self.name)
- self.describe()
- for direction in self.linked_rooms:
- room = self.linked_rooms[direction]
- print ("The " + room.get_name() + " is " + direction)
- def move(self, direction):
- if direction in self.linked_rooms:
- return self.linked_rooms[direction]
- else:
- print("You can't go that way")
- return self
- #######ITEM CLASS SAVE AS item.py ###########################################################################################
- ###Room Item
- class Item():
- ##init class
- def __init__(self,item_name):
- self.item = item_name
- self.item_description = None
- #self.item_room = {}
- ##define item description as a thing
- def set_item_description(self,item_description):
- self.item_description = item_description
- ##get item desrciption
- def get_item_description(self):
- return self.item_description
- ##set item name
- def set_item(self,item):
- self.item = item
- ##get item name
- def get_item_name(self):
- return self.item
- def get_item_details(self):
- print ("A", self.item, "is here!")
- print (self.item_description)
- #####CHARACTER CLASS - save as character.py ############################################################################
- class Character():
- # Create a character
- def __init__(self, char_name, char_description):
- self.name = char_name
- self.description = char_description
- self.conversation = None
- # Describe this character
- def describe(self):
- print( self.name + " is here!" )
- print( self.name,"is a", self.description )
- # Set what this character will say when talked to
- def set_conversation(self, conversation):
- self.conversation = conversation
- # Talk to this character
- def talk(self):
- if self.conversation is not None:
- print("[" + self.name + " says]: " + self.conversation)
- else:
- print(self.name + " doesn't want to talk to you")
- # Fight with this character
- def fight(self, combat_item):
- print(self.name + " doesn't want to fight with you")
- return True
- def bribe(self,bribe_item):
- print(self.name + " is incorrutable and may not be bribed.")
- return True
- class Enemy(Character):
- def __init__(self, char_name, char_description):
- super().__init__(char_name, char_description)
- self.weakness = None
- self.bribe_weakness = None
- self.reward = None
- ##fighting
- def fight(self, combat_item):
- if combat_item == self.weakness:
- print("You fend " + self.name + " off with the " + combat_item )
- return True
- else:
- print(self.name + " crushes you, puny adventurer")
- return False
- ##bribing
- def bribe(self, bribe_item):
- if bribe_item == self.bribe_weakness:
- print("You present " + self.name + " with the " + bribe_item,".They appear satisfied.")
- print (self.reward)
- return True
- else:
- print(self.name + " takes offence at your offer and crushes you, puny adventurer")
- return False
- ##set get weakness
- def set_weakness(self,item_weakness):
- self.weakness = item_weakness
- def get_weakness(self):
- return self.weakness
- ##set get weakness
- def set_bribe_weakness(self,bribe_item_weakness):
- self.bribe_weakness = bribe_item_weakness
- def get_bribe_weakness(self):
- return self.bribe_weakness
- ##set get reward
- def set_reward(self,item_reward):
- self.reward = item_reward
- def get_reward(self):
- return self.reward
- class Friend(Character):
- def __init__(self, char_name, char_description):
- super().__init__(char_name, char_description)
- self.strengths = None
- self.reward = None
- ##fighting
- def hug(self, gift_item):
- if gift_item == self.strengths:
- print("You reward " + self.name + " with the " + gift_item )
- return True
- elif gift_item == "Hug":
- print("You reward " + self.name + " with the " + gift_item )
- print ("You both get a warm feeling, but that's all.")
- else:
- print(self.name + " ignores you, totally.")
- ##set get strengths
- def set_strengths(self,item_strengths):
- self.strengths = item_strengths
- def get_strengths(self):
- return self.strengths
- ##set get reward
- def set_reward(self,item_reward):
- self.reward = item_reward
- def get_reward(self):
- return self.reward
- ######PLAYER CLASS - save as player.py #################################################################################
- ###Player Class
- class Player():
- def __init__(self,player_name):
- self.name = player_name
- self.health = None
- self.inventory = []
- def set_inventory(self,inventory_list):
- self.inventory = inventory_list
- def get_inventory(self):
- return self.inventory
- def set_health(self,health_points):
- self.health = health_points
- def get_health(self):
- return self.health
- def describe(self):
- print ("You name is, ", self.name)
- print ("You are carrying:")
- for item in self.inventory:
- print (item[0], item[1])
- print ()
- print ("Your health is :", self.health)
- def set_inv_item(self,inv_item):
- self.item = inv_item
- def get_inv_item(self):
- return self.item
- def check_inventory(self,item):
- in_inv = False
- for thing in self.inventory:
- if item in thing[0]:
- in_inv = True
- break
- return in_inv
- #def inventory_append(self):
- ###########TAHTS IT! #################################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement