Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from room import Room
- from character import Enemy
- from character import Friend
- from character import Character
- from rpginfo import RPGInfo
- from item import Item
- def createMap(kitchen,diningroom,ballroom):
- kitchen.set_linkedRooms('south',diningroom)
- kitchen.set_linkedRooms('north',storeroom)
- diningroom.set_linkedRooms('north',kitchen)
- diningroom.set_linkedRooms('west',ballroom)
- ballroom.set_linkedRooms('east',diningroom)
- ballroom.set_linkedRooms('south',garden)
- ballroom.set_linkedRooms('west',courtyard)
- courtyard.set_linkedRooms('east',ballroom)
- garden.set_linkedRooms('north',ballroom)
- garden.set_linkedRooms('south',forest)
- forest.set_linkedRooms('north',garden)
- forest.set_linkedRooms('south',deepforest)
- deepforest.set_linkedRooms('north',forest)
- storeroom.set_linkedRooms('south',kitchen)
- def addDescriptions(kitched,diningroom,ballroom):
- kitchen.description='A small dirty room. It contains a large table covererd in dust'
- diningroom.description='A large room with ornate golden decorations on each wall'
- ballroom.description='A vast room with a shiny wooden floor; huge candlesticks guard the entrance'
- garden.description='An overgrown garden covered with thorn bushes and weeds'
- forest.description='A dark foreboding place. The leafless trees are crooked and close preventing easy passage'
- deepforest.description='A very dark place, the trees grow so close together, all sunlight is blocked'
- storeroom.description='A small room with shelves covered in rotting food and dirt'
- courtyard.description='A large cobbled square, the way to the outside world'
- def fullMap(rooms):
- for r in rooms:
- r.describe()
- def getAction():
- response = input('Enter Action: ').strip().lower()
- return response
- def prepareZach(weapon):
- zach = Enemy('Zach','A Zombie and he wants to eat your brain','Zombie')
- zach.set_weapon(weapon)
- zach.set_responses(['Grr','Eat Brain','Urrgh','Zombie Stomp'])
- return zach
- def prepareSid(weapon):
- sid = Enemy('Sid','A Skeleton with piercing red eyes','Skeleton')
- sid.set_weapon(weapon)
- sid.set_responses(['Rattle','Chatter','Attack','Bone Chilling'])
- return sid
- def prepareRat(weapon):
- roland = Enemy('Roland','A Giant annoying rat wearing a baseball cap and jacket','Rat')
- roland.set_weapon(weapon)
- roland.set_responses(['Gnaw','Chitter','Squeek','Scratch'])
- return roland
- def prepareOgre(weapon):
- ollie = Enemy('Ollie','A Huge ugly monster waving it fists in the air','Ogre')
- ollie.set_weapon(weapon)
- ollie.set_responses(['Roar','Smash','Attack','Aaarrgh'])
- return ollie
- def prepareFriend():
- fred = Friend('Fred','A friendly person who will give you information')
- fred.set_responses(['Hello','Ollie has a rock','Paper works against a Rat','Skeleton has paper','A Zombie has scissors'])
- return fred
- def checkForCharacter(currentRoom):
- if currentRoom.character == None:
- return False
- else:
- return True
- def playerName():
- name = input('Warrior, what is your name? ').strip().title()
- if name == '':
- name = 'Anon'
- return name
- #Main Program
- name = playerName()
- castle = RPGInfo('The Spooky Castle')
- RPGInfo.setName(name)
- RPGInfo.setAuthor('Ann Onn')
- RPGInfo.gameinfo()
- castle.welcome()
- RPGInfo.aim()
- RPGInfo.hint()
- RPGInfo.pause()
- kitchen = Room('Kitchen')
- ballroom = Room('Ballroom')
- diningroom = Room('Dining Room')
- garden = Room('Garden')
- forest = Room('Forest')
- storeroom = Room('Store Room')
- courtyard = Room('Courtyard')
- deepforest = Room('Deep Forest')
- rooms = [kitchen,ballroom, diningroom]
- createMap(kitchen,diningroom,ballroom) #,garden,forest)
- addDescriptions(kitchen,diningroom,ballroom) #,garden,forest)
- player = Character(name,'A Fearsome Warrior')
- rock = Item('Rock','Good against scissors','1')
- paper = Item('Paper','Good against rock','2')
- scissors = Item('Scissors','Good against paper','3')
- zach = prepareZach(scissors)
- sid = prepareSid(paper)
- fred = prepareFriend()
- roland = prepareRat(rock)
- ollie = prepareOgre(rock)
- diningroom.character = fred
- diningroom.item = paper
- ballroom.character= zach
- forest.character=sid
- forest.item = paper
- deepforest.character=roland
- deepforest.item = scissors
- courtyard.character=ollie
- kitchen.item = scissors
- storeroom.item = rock
- currentRoom = kitchen
- currentRoom.describe()
- #fullMap(rooms)
- victory = False
- play = True
- while play and not victory:
- action = getAction()
- play = not (action == 'exit')
- if action == 'exit':
- play = False
- elif action == 'look':
- currentRoom.describe()
- elif action == 'help':
- RPGInfo.instructions()
- elif action == 'fight':
- if checkForCharacter(currentRoom):
- character = currentRoom.character
- if character.characterType() == 'Enemy':
- weapon = input('Choose your weapon {}: '.format(player.displayItems())).strip().title()
- if weapon in player.getInventory():
- result = character.fight(weapon)
- if result == 'Win':
- treasure = character.get_weapon()
- print('{} drops to the floor'.format(treasure.get_name()))
- currentRoom.item = treasure
- currentRoom.character = None
- player.remove_Item(weapon)
- castle.updateDefeated()
- victory = castle.checkForVictory()
- elif result == 'Lose':
- play = False
- else:
- currentRoom.describe()
- else:
- print('You do not have {}'.format(weapon))
- else:
- character.fight()
- else:
- print('Nobody here to fight')
- elif action == 'talk':
- if checkForCharacter(currentRoom):
- character = currentRoom.character
- character.talk()
- else:
- print('There is nobody here to talk to')
- elif action in ['north','south','east','west']:
- print('Action {}'.format(action))
- currentRoom = currentRoom.move(action)
- currentRoom.describe()
- elif action == 'take':
- if currentRoom._item is not None:
- print('You take {}'.format(currentRoom.item.get_name()))
- player.set_items(currentRoom.item)
- currentRoom.item = None
- else:
- print('There is nothing here to take')
- elif action == 'inventory':
- print(player.displayItems())
- elif action == 'score':
- castle.showScore()
- else:
- print('{} is an invalid action'.format(action))
- RPGInfo.hint()
- if victory:
- print('Congratulations {}, you have won'.format(castle.getName()))
- else:
- print('You lost')
- RPGInfo.credits()
- class Room():
- #constructor method
- #init > means initialise
- #self > means this object
- #self > The self parameter automatically receives a reference to the object invoking the method
- #self > By using self, a method can invoke the object and access the attributes and methods of that object
- numberOfRooms = 0
- def __init__(self,roomName):
- #object attributes
- self._name = roomName
- self._description = None
- self._item = None
- self.exits = None
- self.linkedRooms = {}
- self._character = None
- Room.numberOfRooms += 1
- def get_numberOfRooms():
- return Room.numberOfRooms
- @property
- def name(self):
- return self._name
- @name.setter
- def name(self,name):
- self._name = name
- @property
- def description(self):
- return str(self._description)
- @description.setter
- def description(self,description):
- self._description = description
- @property
- def character(self):
- return self._character
- @character.setter
- def character(self,character):
- self._character = character
- @property
- def item(self):
- return self._item
- @item.setter
- def item(self,item):
- self._item = item
- def describe(self):
- print(self.name.upper() + '\n' + '----------')
- print('ROOM > {} - {}'.format(self.name,self.description))
- if self.character is not None:
- character = self.character
- print('CHARACTER > {} - {}'.format(character.name, character.get_description()))
- if self.item is not None:
- item = self.item
- print('ITEM > {} - {}'.format(item.get_name(),item.get_description()))
- for direction in self.linkedRooms:
- room = self.linkedRooms[direction]
- print( "The " + room.name + " is " + direction)
- def set_linkedRooms(self,direction,room):
- self.linkedRooms[direction] = room
- def get_linkedRooms(self,direction):
- return self.linkedRooms[direction]
- def move(self,direction):
- if direction in self.linkedRooms:
- print('Moving {} '.format(direction))
- return self.linkedRooms[direction]
- else:
- print('You cannot move {}'.format(direction))
- return self
- from random import randint
- class Character():
- characterCount = 0
- classVariable = 0
- responses = []
- def instanceMethod(self):
- print('Instance Method - can access and modify instance and class variables')
- self.__class__.classVariable += 10
- return self.__class__.classVariable
- @classmethod
- def classMethod(cls):
- print('Class Method - Can access and modify class variables')
- cls.classVariable += 1
- return cls.classVariable
- @staticmethod
- def staticMethod():
- print('Static Method - Cannot access or modify class or instance variables')
- return 'Nothing'
- # Create a character
- def __init__(self, char_name, char_description):
- self.name = char_name
- self.description = char_description
- self.conversation = None
- self.items = []
- Character.characterCount += 1
- def displayItems(self):
- if len(self.get_items()) > 0:
- itemstring = ''
- for item in self.get_items():
- itemstring = itemstring + str(item.get_name()) + " "
- return itemstring
- else:
- return "None"
- def get_Name(self):
- return self.name
- def get_characterCount():
- return Character.characterCount
- # Describe this character
- def describe(self):
- print( self.name + " is here!" )
- print( 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 characterType(self):
- return self.__class__.__name__
- def get_description(self):
- return self.description
- def getInventory(self):
- if len(self.get_items()) > 0:
- itemstring = []
- for item in self.get_items():
- itemstring.append(item.get_name())
- return itemstring
- else:
- return []
- def get_items(self):
- return self.items
- def set_items(self,item):
- self.items.append(item)
- def remove_Item(self,itemused):
- for item in self.get_items():
- if item.get_name() == itemused:
- self.get_items().remove(item)
- break
- def set_responses(self,responses):
- self.responses = responses
- def get_responses(self):
- return self.responses
- class Enemy(Character):
- enemyCount = 0
- enemyType = ''
- def __init__(self,name,description,enemyType):
- super().__init__(name,description)
- self.weapon = None
- self.enemyType = enemyType
- Enemy.enemyCount += 1
- def get_Type(self):
- return self.enemyType
- def set_Type(enemyType):
- self.enemyType = enemyType
- def get_enemyCount(self):
- return self.enemyCount
- def get_weapon(self):
- if self.weapon == None:
- return None
- else:
- return self.weapon
- def set_weapon(self,weapon):
- self.weapon = weapon
- def fight(self,combatItem):
- weapon = self.weapon.get_name().lower()
- if combatItem.lower() == weapon:
- print('Both weapons, but the fight is a draw')
- return 'Draw'
- elif (combatItem.lower() == 'rock' and weapon == 'scissors') or (combatItem.lower() == 'paper' and weapon == 'rock') or (combatItem.lower() == 'scissors' and weapon == 'paper'):
- print('You defeat {} with {}'.format(self.name,combatItem))
- return 'Win'
- else:
- print('Pathetic Human. Your {} is no match for me.'.format(combatItem))
- return 'Lose'
- def describe(self):
- print('{} - I am the mighty {}!'.format(self.get_Type(),self.name))
- #print(self.description)
- def talk(self):
- comment = self.responses[randint(0,len(self.responses)-1)]
- print(comment)
- class Friend(Character):
- friendCount = 0
- def __init__(self,name,description):
- super().__init__(name,description)
- Friend.friendCount += 1
- def get_friendCount():
- return Friend.friendCount
- def fight(self):
- print('I do not want to fight you. I am your friend')
- def describe(self):
- print('Hello, my name is {}. I am your friend. I have information'.format(self.name))
- def talk(self):
- comment = self.responses[randint(0,len(self.responses)-1)]
- print(comment)
- def giveGift(self):
- if self.items is not None:
- print('{} gives you {}'.format(self.name,self.items))
- items = self.items
- self.set_items('None')
- return items
- import datetime
- class RPGInfo():
- author = "Anon"
- name = "Unknown"
- enemiesDefeated = 0
- def checkForVictory(self):
- if self.enemiesDefeated == 4:
- return True
- else:
- return False
- def __init__(self,title):
- self.title = title
- self._cyear = format(datetime.datetime.now().year)
- def get_defeated(self):
- return self.enemiesDefeated
- def set_defeated(self,value):
- self.enemiesDefeated = se
- def updateDefeated(self):
- self.enemiesDefeated = self.enemiesDefeated + 1
- def showScore(self):
- print('Current Score = {}'.format(self.enemiesDefeated))
- @property
- def cyear(self):
- return self._cyear
- @cyear.setter
- def cyear(self,cyear):
- self._year = cyear
- #instance method
- def welcome(self):
- print('{}, Welcome to {}\n----------'.format(self.name,self.title))
- def getTitle(self):
- return self.title
- #static method
- @staticmethod
- def gameinfo():
- print('Made using OOP RPG Creator (c) {} \n'.format(RPGInfo.author))
- #class method
- @classmethod
- def setAuthor(cls,author):
- cls.author = author
- @classmethod
- def setName(cls,name):
- cls.name = name
- def getName(cls):
- return cls.name
- @classmethod
- def getAuthor(cls):
- return cls.author
- @classmethod
- def credits(cls):
- print('{}, thank you for playing.'.format(cls.name))
- print('Created by {}.'.format(cls.author))
- def hint():
- print('Type help to display possible commands')
- def instructions():
- print('The following actions are possible')
- print('north, south, east, west > Move in that direction')
- print('fight > fight character if one exists')
- print('inventory > list items held by hero')
- print('look > show room details')
- print('take > pick up item if one exists')
- print('talk > talk to character if one exists')
- print('score > show current score')
- print('exit > Leave Game')
- def aim():
- print('Expore the rooms and collect the items')
- print('There are four Creatures lurking around the map')
- print('Defeat all four to win the game')
- print('Talk to a friend to get help in defeating the Creatures')
- def pause():
- response = input('Press any key to start the game')
- class Item():
- def __init__(self,name,description,value):
- #constructor
- #object attributes
- self.name = name
- self.description = description
- self.value = value
- def set_name(self,name):
- self.name = name
- def get_name(self):
- return self.name
- def set_description(self,description):
- self.description = description
- def get_description(self):
- return self.description
- def set_value(self,value):
- self.value = value
- def get_value(self):
- return self.value
- def getDetails(self):
- print(self.get_name() + ' : ' + self.get_description())
- def drop(self):
- print('You have dropped {}'.format(self.get_name()))
- def use(self):
- print('You have used {}',(self.get_name()))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement