Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import randint
- class Character():
- # Create a character
- def __init__(self, char_name, char_description):
- self.name = char_name
- self.description = char_description
- self.conversation = None
- self.items = None
- # 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_items(self):
- return self.items
- def set_items(self,items):
- self.items = items
- class Enemy(Character):
- responses = ['Grrr','Aargh','Go Away!','What do you want?','Get Lost!','Give me your brain!']
- def __init__(self,name,description):
- super().__init__(name,description)
- self.weapon = None
- def get_weapon(self):
- if self.weapon == None:
- return 'Immune'
- else:
- return self.weapon
- def set_weapon(self,weapon):
- self.weapon = weapon
- def fight(self,combatItem):
- if combatItem == 'rock':
- print('You defeat {} with {}'.format(self.name,combatItem))
- return 'Win'
- elif combatItem == self.weapon:
- print('Nothing Happens')
- return 'Draw'
- else:
- print('Pathetic Earthling. Your {} is no match for me.'.format(combatItem))
- return 'Lose'
- def describe(self):
- print('I am the mighty {}!'.format(self.name))
- #print(self.description)
- def talk(self):
- comment = self.responses[randint(0,len(self.responses)-1)]
- print(comment)
- class Friend(Character):
- responses = ['Hello','How are you?','A rock may be useful against Zach','Do you want a brew?','Zach uses scissors as a weapon']
- def __init__(self,name,description):
- super().__init__(name,description)
- def fight(self):
- print('I do not want to fight you. I am your friend')
- def describe(self):
- print('I am your friend {}. Items: {}'.format(self.name,self.items))
- def talk(self):
- comment = self.responses[randint(0,len(self.responses)-1)]
- print(comment)
- #self.giveGift()
- 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
- 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
- def __init__(self,roomName):
- #object attributes
- self.name = roomName
- self.description = None
- self.items = None
- self.exits = None
- self.linkedRooms = {}
- self.character = None
- def set_description(self,description):
- self.description = description
- def set_name(self,name):
- self.name = name
- def get_name(self):
- return str(self.name)
- def get_description(self):
- return str(self.description)
- def get_character(self):
- return self.character
- def set_character(self,character):
- self.character = character
- def describe(self):
- print(self.get_name().upper() + '\n' + '----------')
- print(self.get_description())
- if self.get_character() is not None:
- character = self.get_character()
- print('*** INHABITANT IN ROOM ***')
- character.describe()
- for direction in self.linkedRooms:
- room = self.linkedRooms[direction]
- print( "The " + room.get_name() + " is " + direction)
- #for direction in self.linkedRooms:
- # room = self.linkedRooms[direction]
- # print(self.linkedRooms[direction] + ' > ' + direction)
- #print(room.get_name())
- #print('Directions: ' + repr(self.linkedRooms) + '\n')
- 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 room import Room
- from character import Enemy
- from character import Friend
- def createMap(kitchen,diningroom,ballroom):
- kitchen.set_linkedRooms('south',diningroom)
- diningroom.set_linkedRooms('north',kitchen)
- diningroom.set_linkedRooms('west',ballroom)
- ballroom.set_linkedRooms('east',diningroom)
- def addDescriptions(kitched,diningroom,ballroom):
- kitchen.set_description('A small room with a big table')
- diningroom.set_description('A large room with ornate golden decorations on each wall')
- ballroom.set_description('A vast room with a shiny wooden floor; huge candlesticks guard the entrance')
- def fullMap(rooms):
- for r in rooms:
- r.describe()
- def getAction():
- response = input('Enter Action: ').strip().lower()
- return response
- def displayActions():
- print('exit > Leave Game' + '\n' + 'north, south, east, west > Move in that direction' + '\n' + 'display > show room details')
- def prepareEnemy():
- zach = Enemy('Zach','A Zombie and he wants to eat your brain')
- zach.set_weapon('scissors')
- zach.set_conversation('Grrr')
- #zach.talk()
- #zach.describe()
- return zach
- def prepareFriend():
- fred = Friend('Fred','A friendly person who will make you a brew')
- fred.set_items('rock')
- fred.set_conversation('Hello')
- #fred.talk()
- #fred.describe()
- return fred
- def checkForCharacter(currentRoom):
- if currentRoom.get_character() == None:
- return False
- else:
- return True
- #Main Program
- kitchen = Room('Kitchen')
- ballroom = Room('Ballroom')
- diningroom = Room('Dining Room')
- rooms = [kitchen,ballroom, diningroom]
- createMap(kitchen,diningroom,ballroom)
- addDescriptions(kitchen,diningroom,ballroom)
- zach = prepareEnemy()
- fred = prepareFriend()
- diningroom.set_character(fred)
- ballroom.set_character(zach)
- currentRoom = kitchen
- currentRoom.describe()
- #fullMap(rooms)
- play = True
- while play:
- action = getAction()
- play = not (action == 'exit')
- if action == 'exit':
- play = False
- elif action == 'display':
- currentRoom.describe()
- elif action == 'help':
- displayActions()
- elif action == 'fight':
- if checkForCharacter(currentRoom):
- character = currentRoom.get_character()
- if character.characterType() == 'Enemy':
- weapon = input('Choose your weapon (rock/paper/scissors)').strip().lower()
- result = character.fight(weapon)
- if result == 'Win':
- currentRoom.set_character(None)
- elif result == 'Lose':
- play = False
- else:
- currentRoom.describe()
- else:
- character.fight()
- else:
- print('Nobody here to fight')
- elif action == 'talk':
- if checkForCharacter(currentRoom):
- character = currentRoom.get_character()
- character.talk()
- if character.characterType() == 'Friend':
- character.giveGift()
- 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()
- else:
- print('{} is an invalid action'.format(action))
- print('Possible Actions - display, help, fight, talk, north, south, east, west, exit')
- print('End of Game')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement