Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- class Room:
- """Stores information about the locations that the player and monster
- will be moving through.
- attributes: name (str), room_number (int)"""
- def __init__(self, name, room_number):
- """creates a Room object with a set name, room number, and list
- of other rooms this Room is connected to.
- Room, str, int, list_of_Rooms -> None"""
- self.name = name
- self.room_number = room_number
- self.__other_rooms = []
- def connect_to(self, other_room):
- """takes another room or an iterable of Rooms and adds connections
- between them.
- Room, Room -> None"""
- if isinstance(other_room, Room):
- self.__other_rooms.append(other_room)
- other_room.__other_rooms.append(self)
- elif isinstance(other_room, tuple):
- for room in other_room:
- self.__other_rooms.append(room)
- room.__other_rooms.append(self)
- elif isinstance(other_room, list):
- for room in other_room:
- self.__other_rooms.append(room)
- room.__other_rooms.append(self)
- def get_exit_list(self):
- """returns the list of the current Room's exits.
- Room -> list_of_str"""
- exitlist = []
- for room in self.__other_rooms:
- exitlist.append(room.room_number)
- return exitlist
- def is_connected_to(self, other_room):
- """determines whether or not two Rooms are connected.
- Room, Room -> bool"""
- return other_room in self.__other_rooms
- def description(self):
- """returns a string representing a description of the Room. The
- description includes the name of the Room and a list of the room
- numbers of adjacent rooms.
- Room -> str"""
- room_numbers = []
- for room in self.__other_rooms:
- room_numbers.append(str(room.room_number))
- return "You are in " + str(self.name) + ". Valid exits: " + ", ".join(room_numbers)
- def __repr__(self):
- """returns a string representing an object in Room form.
- Room -> str"""
- return "Room(" + repr(self.name) + ", " + repr(self.room_number) + ")"
- lab = Room("the lab", 0)
- bedroom = Room("the bedroom", 1)
- bathroom = Room("the bathroom", 2)
- studio = Room("the studio", 3)
- basement = Room("the basement", 4)
- garage = Room("the garage", 5)
- lab.connect_to((bedroom, bathroom, basement))
- bedroom.connect_to((bathroom, garage))
- bathroom.connect_to(studio)
- studio.connect_to((basement, garage))
- basement.connect_to(garage)
- Roomlist = [lab, bedroom, bathroom, studio, basement, garage]
- class Command:
- """Stores types of commands.
- attributes: action (str), destination (Room)"""
- def __init__(self, action, destination):
- """creates a new Command object with a set action and destination
- Command, str, Room -> None"""
- self.action = action
- self.destination = destination
- def __repr__(self):
- """returns a string representing an object in Command form
- Command -> str"""
- return "Command(" + repr(self.action) +", " + repr(self.destination) + ")"
- class Movable(Command):
- """Represents objects and characters that have changeable locations.
- setters: .move_to()"""
- def move_to(self, location):
- """serves as a setter for the location of the Movable object.
- Movable -> None"""
- self.__location = location
- def __init__(self, location):
- """Creates a new instance of Movable in a given location
- Movable, Room -> None"""
- self.move_to(location)
- def get_location(self):
- """Returns the location of the Movable object.
- Movable -> Room"""
- return self.__location
- def update(self):
- """Updates a parameter of the location.
- Movable -> None"""
- pass
- def __repr__(self):
- """returns a string representing an object in Movable form
- Movable -> str"""
- return "Movable(" + repr(self.get_location()) + ")"
- class Wanderer(Movable):
- """This class is used for things that move around randomly on their own
- additional attributes: is_awake (boolean)"""
- def __init__(self, location = random.choice(Roomlist), is_awake = True):
- """Creates a new instance of Wanderer at a certain location with
- an optional boolean determining whether it is awake or not.
- Wanderer, [Room, bool] -> None"""
- super().__init__(location)
- self.is_awake = is_awake
- def update(self):
- """Checks if the Wanderer is awake, moving it to a random adjacent
- room and making it go back to sleep if it was originally awake.
- Wanderer -> None"""
- if self.is_awake == True:
- self.move_to(random.choice(Roomlist))
- self.is_awake = False
- def __repr__(self):
- """returns a string representing an object in Wanderer form
- Wanderer -> str"""
- return "Wanderer( " + repr(self.__location) + ", " + repr(self.is_awake) + ")"
- monster = Wanderer()
- IS_MONSTER_SHOT = False
- class Player(Movable):
- """This class is used for the player character.
- Attributes: num_darts (int)"""
- def __init__(self, num_darts, location = random.choice(Roomlist)):
- """Creates a new Player object with a set number of darts and
- an optional room
- Player, int, [Room] -> None"""
- super().__init__(location)
- self.num_darts = num_darts
- def shoot_into(self, destination):
- """Shoots at a monster, decreasing the number of darts by one
- and changing the variable of whether it is shot or not depending
- on whether it was in the right room"""
- self.num_darts -= 1
- global IS_MONSTER_SHOT
- if monster.__location() == destination:
- IS_MONSTER_SHOT = True
- return True
- return False
- def get_command(self):
- """Communicates with the user to figure out what they want to do
- and returns an appropriate Command object once a valid command
- has been entered.
- Player -> Command"""
- while True:
- print("What do you want to do?")
- user_input = input(">")
- if "shoot into" in user_input.lower():
- if int(user_input[11]) in self.get_location().get_exit_list():
- if Roomlist[int(user_input[11])].is_connected_to(self.get_location()):
- if self.num_darts > 0:
- command1 = Command("shoot", Roomlist[int(user_input[11])])
- return command1
- break
- else:
- print("You are out of darts.")
- else:
- print("You are not connected to that room.")
- else:
- print("That is not a valid room.")
- elif "go to" in user_input.lower():
- if int(user_input[6]) in self.get_location().get_exit_list():
- if Roomlist[int(user_input[6])].is_connected_to(self.get_location()):
- command1 = Command("move", Roomlist[int(user_input[6])])
- return command1
- break
- else:
- print("You are not connected to that room.")
- else:
- print("That is not a valid room.")
- else:
- print("That is not a valid command.")
- def execute_command(self, command):
- """Takes a Command object and carries out the corresponding action.
- Command -> str"""
- if command.action == "shoot":
- self.shoot_into(command.destination)
- print("You shoot a dart into room " + command.destination.room_number + ".")
- if IS_MONSTER_SHOT == False:
- print("It doesn't sound like you hit anything.")
- print("You can hear the monster moving about.")
- else:
- print("You've shot the monster!")
- elif command.action == "move":
- self.move_to(command.destination)
- print("You walk into room number " + str(command.destination.room_number) + ".")
- if monster.__location.is_connected_to(self.__location):
- print("The monster is close, you can smell it.")
- elif monster.__location == self.__location:
- print("You are in the same room as the monster!")
- def update(self):
- """Displays a description of the Player's room, obtains a command,
- and uses execute_command to carry it out.
- Player -> str"""
- print(self.get_location().description())
- command1 = self.get_command()
- self.execute_command(command1)
- def __repr__(self):
- """returns a string representing an object in Player form
- Player -> String"""
- return "Player(" + repr(self.num_darts) + ", " + repr(self.__location) + ")"
- player1 = Player(2)
- UPDATE_LIST = [player1, monster]
- print("Welcome to Monster Capture!\n")
- print("""A wild monster is loose in the area, and it's your job to capture it without
- getting eaten. Travel around the map, and when you have figured out which
- room the monster is in, go to an adjacent room and shoot a tranquilizer
- dart into that room. If you hit the monster, you win! But be careful; you
- only have two darts!""")
- print("You begin the game in room " + str(player1.get_location().room_number) + ".")
- player1.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement