Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from textwrap import dedent
- from sys import exit
- class Player(object):
- actions = {
- 'QUIT': quit
- }
- def __init__(self, actions):
- self.actions = actions
- # Want actions to be a list of actions like in the Room Class
- #
- def quit(self):
- # Quits the game
- exit(0)
- class Room(object):
- # Description is just a basic room description. No items needed to be added here.
- def __init__(self, desc, exits, exitdesc):
- self.desc = desc
- self.exits = exits
- self.exitdesc = exitdesc
- # Also want list of general actions for a room here.
- def enterroom(self):
- #First print the description of the room
- print(self.desc)
- #Then print the list of exits.
- if len(self.exits) > 1:
- print(f"You see the following exits:")
- for exd in self.exitdesc:
- print(self.exitdesc[exd])
- elif len(self.exits) == 1:
- print(f"There is one exit:")
- for exd in self.exitdesc:
- print(self.exitdesc[exd])
- else:
- print("There are no exits.")
- # Then allow the player to make a choice.
- self.roomactivity()
- # Here's what I mean about calling the methods via a dictionary
- def roomactivity(self):
- while True:
- print("What do you want to do?")
- choice = input("> ").upper()
- if choice in self.exits:
- self.exits[choice].enterroom()
- #And here's where I want to call actions other than directions.
- elif choice in player.actions:
- player.actions[choice]
- else:
- print("I don't understand.")
- class VoidRoom(Room):
- def __init__(self):
- super().__init__(
- desc = "ONLY VOID.",
- exits = {},
- exitdesc = {})
- class TestRoom(Room):
- def __init__(self):
- super().__init__(
- desc = dedent("""
- This room is only a test room.
- It has pure white walls and a pure white floor.
- Nothing is in it and you can hear faint echoes
- of some mad sounds."""),
- exitdesc = {
- 'NORTH': 'To the NORTH is a black door.',
- 'SOUTH': 'To the SOUTH is a high window.',
- 'EAST': 'To the EAST is a red door.',
- 'WEST': 'To the WEST is a blue door.'},
- exits = {
- 'NORTH': void_room,
- 'SOUTH': void_room,
- 'EAST': void_room,
- 'WEST': void_room})
- void_room = VoidRoom()
- test_room = TestRoom()
- player = Player()
- test_room.enterroom()
Add Comment
Please, Sign In to add comment