diff --git a/src/adv.py b/src/adv.py index c9e26b0f85..363637d9f2 100644 --- a/src/adv.py +++ b/src/adv.py @@ -1,4 +1,6 @@ from room import Room +from player import Player +from item import Item # Declare all the rooms @@ -23,25 +25,45 @@ # Link rooms together +def set_up_rooms(): -room['outside'].n_to = room['foyer'] -room['foyer'].s_to = room['outside'] -room['foyer'].n_to = room['overlook'] -room['foyer'].e_to = room['narrow'] -room['overlook'].s_to = room['foyer'] -room['narrow'].w_to = room['foyer'] -room['narrow'].n_to = room['treasure'] -room['treasure'].s_to = room['narrow'] + room['outside'].n_to = room['foyer'] + room['foyer'].s_to = room['outside'] + room['foyer'].n_to = room['overlook'] + room['foyer'].e_to = room['narrow'] + room['overlook'].s_to = room['foyer'] + room['narrow'].w_to = room['foyer'] + room['narrow'].n_to = room['treasure'] + room['treasure'].s_to = room['narrow'] + print("Have set up rooms") +# Add items to room: +def add_room_items(): + room['outside'].add_item(Item("Sword", f"a close range weapon used to defeat enemies, cut tall grass and break open clay pots")) + room['foyer'].add_item(Item("Rupee", f"this is the primary local unit of currency and can be used to purchase items from the local store")) + room['overlook'].add_item(Item("Hookshot", f"a spring-loaded, trigger -pulled hook attached to some lengthy chains. It can atttack enemies at a distance")) + room['treasure'].add_item(Item("Key", f"this key looks like it would fit into a lock on a treasure chest")) + room['narrow'].add_item(Item("Potion", f"drink this potion to replenish your health if you are running low")) + print("Have added items to rooms") # # Main # # Make a new player object that is currently in the 'outside' room. +possible_directions = ['n', 's', 'e', 'w'] +actions = ['take', 'drop', 't', 'd'] + +player_name = input('What is your name, adventure? ') +player = Player(player_name, room['outside']) +print(f"To move around the map press {possible_directions}. Look for items to help on your way. For help try 'Help' or 'h'.") +print(f"Good luck, {player.name}") + + # Write a loop that: # # * Prints the current room name + # * Prints the current description (the textwrap module might be useful here). # * Waits for user input and decides what to do. # @@ -49,3 +71,54 @@ # Print an error message if the movement isn't allowed. # # If the user enters "q", quit the game. + +set_up_rooms() +add_room_items() +player.look() + # Program Start: + + + + + # REPL Start: +while True: + user_input = input(f"What would you like to do {player.name}? ").lower().split() + + if len(user_input) > 2 or len(user_input) < 1: + print(f"Sorry, {player.name}, thats not a valid one or two word command. Would you like 'help'?") + + elif len(user_input) == 2: + if user_input[0] in actions: + if user_input[0] == "t" or user_input[0] == "take": + item = player.select_item(user_input[1]) + player.take(item) + elif user_input[0] == "d" or user_input[0] == "drop": + item = player.select_inventory_item(user_input[1]) + player.drop(item) + + else: + if user_input[0] == "q" or user_input[0] == "quit": + print(f"Thanks for playing {player.name}!") + break + + elif user_input[0] == "h" or user_input[0] == "help": + print("Commands:\n'n' - Move North\n's' - Move South\n'e' - Move East\n'w' - Move West\n't' or 'take '' - Take Item\n'd' or 'drop' '' - Drop Item\n'inv' or 'inventory' - Inventory Items\n'l' or 'look' - Look around in current room\n'h' or 'help' - Help menu\n'q' or 'quit' - Exit Game\n") + continue + + elif user_input[0] == "l" or user_input[0] == "look": + player.look() + continue + + elif user_input[0] == "inv" or user_input[0] == "inventory": + player.show_inventory() + continue + elif user_input[0] in possible_directions: + try: + player.change_room(user_input[0]) + print(f"You are in the {player.current_room.name}. \n{player.current_room.description}") + except AttributeError: + print(f"{player.name}'s adventure lies elsewhere.'") + + else: + print(f"Movement not allowed! Please enter direction {possible_directions} to move around the map.") + diff --git a/src/item.py b/src/item.py new file mode 100644 index 0000000000..8e1158ee96 --- /dev/null +++ b/src/item.py @@ -0,0 +1,13 @@ +class Item: + def __init__(self, name, description): + self.name = name + self.description = description + + def taken(self): + print(f"Picked up {self.name}. May it serve you well.") + + def dropped(self): + print(f"Dropped {self.name}, Hope you didnt need that.") + + def examine(self): + print(f"Examining the {self.name}... \n{self.description}") \ No newline at end of file diff --git a/src/player.py b/src/player.py index d79a175029..df172c0994 100644 --- a/src/player.py +++ b/src/player.py @@ -1,2 +1,67 @@ # Write a class to hold player information, e.g. what room they are in # currently. +from room import Room +from item import Item + +class Player: + def __init__(self, name, current_room): + self.name = name + self.current_room = current_room + self.inventory = [] + + def __str__(self): + print(f"{self.name} is currently in room {self.current_room}.") + + def change_room(self, direction): + if getattr(self.current_room, f'{direction}_to'): + self.current_room = getattr(self.current_room, f'{direction}_to') + + def look(self): + if len(self.current_room.items) <= 0: + room_items = "Nothing." + elif len(self.current_room.items) == 1: + room_items = f"{self.current_room.items[0].name}" + else: + for item in self.current_room.items: + room_items += f"{item.name}\n" + + print(f"{self.name}, you find yourself in {self.current_room.name}. \n{self.current_room.description}") + print(f"You see in this room the following items: {room_items}") + + def show_inventory(self): + if len(self.inventory) <= 0: + print(f"You havent picked up anything yet, {self.name}.") + elif len(self.inventory) == 1: + print(f"You have 1 item in your inventory, {self.name}.") + print(f"{self.inventory[0].name}: {self.inventory[0].description}") + else: + print(f"You have {len(self.inventory)} items in your inventory, {self.name}") + for item in self.inventory: + print(f"\n{item.name}: {item.description}") + + def select_item(self, item): + for i in self.current_room.items: + if i.name.lower() == str(item).lower(): + return i + else: + print(f"{item} not found.") + return None + + def select_inventory_item(self, item): + for i in self.inventory: + if i.name.lower() == str(item).lower(): + return i + else: + print(f"{item} not found.") + return None + + def take(self, item): + self.inventory.append(item) + self.current_room.items.remove(item) + item.taken() + + def drop(self, item): + self.inventory.remove(item) + self.current_room.items.append(item) + item.dropped() + \ No newline at end of file diff --git a/src/room.py b/src/room.py index 24c07ad4c8..a35fbe986c 100644 --- a/src/room.py +++ b/src/room.py @@ -1,2 +1,16 @@ # Implement a class to hold room information. This should have name and -# description attributes. \ No newline at end of file +# description attributes. +from item import Item + +class Room: + def __init__(self, name, description): + self.name = name + self.description = description + self.items = [] + + def __str__(self): + return f"{self.name}: \n{self.description}" + + def add_item(self, item): + self.items.append(item) + print(f"{item.name} added to {self.name}") \ No newline at end of file