diff --git a/README.md b/README.md index 182debf5ff..0af7a45c9a 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,8 @@ # Intro to Python II + + + Up to this point, you've gotten your feet wet by working on a bunch of small Python programs. In this module, we're going to continue to solidify your Python chops by implementing a full-featured project according to a provided specification. diff --git a/src/adv.py b/src/adv.py index c9e26b0f85..2fb8678a11 100644 --- a/src/adv.py +++ b/src/adv.py @@ -1,4 +1,7 @@ from room import Room +from player import Player +from item import Item +import textwrap # Declare all the rooms @@ -38,14 +41,249 @@ # # Make a new player object that is currently in the 'outside' room. +new_player = Player("Jayne", room['outside']) + + +# CREATE VARIABLES FOR ROOMS +outside = room['outside'] +foyer = room['foyer'] +overlook = room['overlook'] +narrow = room['narrow'] +treasure = room['treasure'] + + +# ADD ITEMS TO ROOMS +axe = Item("Axe", "by jove, it is sharp") +outside.stealthy_add(axe) + +compass = Item("Compass", "weathered, but still works") +foyer.stealthy_add(compass) + +binoculars = Item("Binoculars", "handy for spying") +overlook.stealthy_add(binoculars) + +torch = Item("Torch", "batteries running low") +narrow.stealthy_add(torch) + +gold = Item("Gold", "a small canvas sack filled with gold coins, they are worth a fortune!") +treasure.stealthy_add(gold) + +knife = Item("Knife", "pocket knife, could be hidden in your socks") +new_player.stealthy_add(knife) + +print("\n------- WELCOME TO JAYNE'S GAME ---------\n") # 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. -# -# If the user enters a cardinal direction, attempt to move to the room there. -# Print an error message if the movement isn't allowed. -# -# If the user enters "q", quit the game. + +game_playing = True + +while game_playing == True: + + # * Prints the current room name + print(new_player) + + # * Prints the current description (the textwrap module might be useful here). + room_description = new_player.current_room.description + wrapper = textwrap.TextWrapper(width=25) + word_list = wrapper.wrap(text=room_description) + for element in word_list: + print(" > " + element) + + # USER PROMPT + + # * Waits for user input and decides what to do + user_prompt = (input("\n---- WHAT NOW ? ---- \n[n], [s], [e] or [w] to move\n\n[look] to search room\n[inv] to see your stash\n[grab] [item] to grab\n[drop] [item] to drop\n[q] to quit: ")) + + + # If the user enters a cardinal direction, attempt to move to the room there. + + # NORTH + if user_prompt.lower() == 'n': + if hasattr(new_player.current_room, 'n_to'): + print("\n---- LET US GO NORTH ----\n") + new_player.current_room = new_player.current_room.n_to + else: + print("\n---- YOU SHALL NOT PASS ----") + print("---- CHOOSE ANOTHER DIRECTION ----\n") + + # SOUTH + elif user_prompt.lower() == 's': + if hasattr(new_player.current_room, 's_to'): + print("\n---- LET US GO SOUTH ----\n") + new_player.current_room = new_player.current_room.s_to + else: + print("\n---- YOU SHALL NOT PASS ----") + print("---- CHOOSE ANOTHER DIRECTION ----\n") + # EAST + elif user_prompt.lower() == 'e': + if hasattr(new_player.current_room, 'e_to'): + print("\n---- LET US GO EAST ----\n") + new_player.current_room = new_player.current_room.e_to + else: + print("\n---- YOU SHALL NOT PASS ----") + print("---- CHOOSE ANOTHER DIRECTION ----\n") + # WEST + if user_prompt.lower() == 'w': + if hasattr(new_player.current_room, 'w_to'): + print("\n---- LET US GO WEST ----\n") + new_player.current_room = new_player.current_room.w_to + else: + print("\n---- YOU SHALL NOT PASS ----") + print("---- CHOOSE ANOTHER DIRECTION ----\n") + + + # LOOK + elif user_prompt.lower() == 'look': + #OUTSIDE + if new_player.current_room == outside: + outside.room_inventory() + # FOYER + elif new_player.current_room == foyer: + foyer.room_inventory() + # OVERLOOK + elif new_player.current_room == overlook: + overlook.room_inventory() + # NARROW + elif new_player.current_room == narrow: + narrow.room_inventory() + # TREASURE + elif new_player.current_room == treasure: + treasure.room_inventory() + # INV + elif user_prompt.lower() == 'inv': + new_player.player_inventory() + + # ITEM ONLY + + # AXE + elif user_prompt.lower() == 'axe': + typed_item = user_prompt.lower() + + player_items = [data for data in new_player.player_items] + + if typed_item in player_items: + print(f"\n\t>> What do you want me to do with {typed_item}? \n\t>> You need to tell me!!\n") + else: + print(f"\n\t>> If you want to do something with {typed_item}, YOU NEED TO GO FIND IT FIRST!!!!\n") + # COMPASS + elif user_prompt.lower() == 'compass': + typed_item = user_prompt.lower() + + player_items = [f"{data.name.lower()}" for data in new_player.player_items] + + if typed_item in player_items: + print(f"\n\t>> What do you want me to do with {typed_item}? \n\t>> You need to tell me!!\n") + else: + print(f"\n\t>> If you want to do something with {typed_item}, YOU NEED TO GO FIND IT FIRST!!!!\n") + # BINOCULARS + elif user_prompt.lower() == 'binoculars': + typed_item = user_prompt.lower() + + player_items = [f"{data.name.lower()}" for data in new_player.player_items] + + if typed_item in player_items: + print(f"\n\t>> What do you want me to do with {typed_item}? \n\t>> You need to tell me!!\n") + else: + print(f"\n\t>> If you want to do something with {typed_item}, YOU NEED TO GO FIND IT FIRST!!!!\n") + # TORCH + elif user_prompt.lower() == 'torch': + typed_item = user_prompt.lower() + + player_items = [f"{data.name.lower()}" for data in new_player.player_items] + + if typed_item in player_items: + print(f"\n\t>> What do you want me to do with {typed_item}? \n\t>>You need to tell me!!\n") + else: + print(f"\n\t>> If you want to do something with {typed_item}, YOU NEED TO GO FIND IT FIRST!!!!\n") + # GOLD + elif user_prompt.lower() == 'gold': + typed_item = user_prompt.lower() + + player_items = [f"{data.name.lower()}" for data in new_player.player_items] + + if typed_item in player_items: + print(f"\n\t>> What do you want me to do with {typed_item}? \n\t>> You need to tell me!!\n") + else: + print(f"\n\t>> If you want to do something with {typed_item}, YOU NEED TO GO FIND IT FIRST!!!!\n") + # KNIFE + elif user_prompt.lower() == 'knife': + typed_item = user_prompt.lower() + + player_items = [f"{data.name.lower()}" for data in new_player.player_items] + + if typed_item in player_items: + print(f"\n\t>> What do you want me to do with {typed_item}? \n\t>> YOU NEED TO TELL ME!!\n") + else: + print(f"\n\t>> If you want to do something with {typed_item}, YOU NEED TO GO FIND IT FIRST!!!!\n") + + # VERB NOUN + + # GRAB ONLY + elif user_prompt.lower() == 'grab': + print("\n\tGRAB WHAT..?\nYou need to tell me!!\n") + + # GRAB + elif user_prompt.lower()[:4] == 'grab': + # GRAB + grab_separate_words = user_prompt.split(' ') + command = grab_separate_words[0] + item_to_grab = grab_separate_words[1].lower() + + current_room_items = [f"{data.name.lower()}" for data in new_player.current_room.room_items] + + if item_to_grab in current_room_items: + print(f"\n\t>> YOU GRAB: {item_to_grab}") + if item_to_grab == 'axe': + new_player.add_player_item(axe) + elif item_to_grab == 'compass': + new_player.add_player_item(compass) + elif item_to_grab == 'binoculars': + new_player.add_player_item(binoculars) + elif item_to_grab == 'torch': + new_player.add_player_item(torch) + elif item_to_grab == 'gold': + new_player.add_player_item(gold) + elif item_to_grab == 'knife': + new_player.add_player_item(knife) + else: + print("\nThat item is not found here!!\nSearch for it elsewhere\n") + + # DROP ONLY + elif user_prompt.lower() == 'drop': + print("\n\tDROP WHAT..?\nYou need to specify - I'm not psychic!\n") + + # DROP + elif user_prompt.lower()[:4] == 'drop': + print("\nDrop!") + # new_player.drop_player_item(knife) + grab_separate_words = user_prompt.split(' ') + command = grab_separate_words[0] + item_to_grab = grab_separate_words[1].lower() + + current_room_items = [f"{data.name.lower()}" for data in new_player.player_items] + + if item_to_grab in current_room_items: + print(f"\t>> YOU DROP: {item_to_grab}") + if item_to_grab == 'axe': + new_player.drop_player_item(axe) + elif item_to_grab == 'compass': + new_player.drop_player_item(compass) + elif item_to_grab == 'binoculars': + new_player.drop_player_item(binoculars) + elif item_to_grab == 'torch': + new_player.drop_player_item(torch) + elif item_to_grab == 'gold': + new_player.drop_player_item(gold) + elif item_to_grab == 'knife': + new_player.drop_player_item(knife) + else: + print("\nThat item is not found here!!\nSearch for it elsewhere\n") + + # If the user enters "q", quit the game + elif user_prompt.lower() == 'q': + print("Thank you for playing \nGoodbye!") + game_playing = False + # Print an error message if the movement isn't allowed. + else: + print("\n---- INVALID COMMAND ----\n") + print("PLEASE SELECT FROM: n, s, e, w or q\n") diff --git a/src/item.py b/src/item.py new file mode 100644 index 0000000000..46fe928f4e --- /dev/null +++ b/src/item.py @@ -0,0 +1,7 @@ +class Item: + def __init__(self, name, description): + self.name = name + self.description = description + + def __str__(self): + return f">> {self.name}: {self.description}" \ No newline at end of file diff --git a/src/player.py b/src/player.py index d79a175029..3bc3979344 100644 --- a/src/player.py +++ b/src/player.py @@ -1,2 +1,48 @@ # Write a class to hold player information, e.g. what room they are in # currently. +from item import Item + +class Player: + # constructor function + def __init__(self, name, room): + self.name = name + self.current_room = room + self.player_items = [] + + # string value + def __str__(self): + return f"{self.name} is currently located: {self.current_room.name}" + + def stealthy_add(self, item): + self.item = item + self.player_items.append(item) + + def add_player_item(self, item): + self.item = item + self.player_items.append(item) + for i in self.player_items: + print(f"\t>> {i.name} is in {self.name}'s stash") + print("\n") + self.current_room.room_items.remove(item) + + def drop_player_item(self, item): + self.item = item + self.player_items.remove(item) + print(f"{item.name} removed from your stash") + self.current_room.room_items.append(item) + print(f"{item.name} left in {self.current_room.name}") + print("\n") + + def player_inventory(self): + if (len(self.player_items) >= 1): + print(f"\n>> Here is what you have on your person:") + for i in self.player_items: + print(f"\t{i}") + else: + print(f"\n>> You dont have anything on your person.\nLook in rooms!\n") + print("\n") + + pass + + + diff --git a/src/room.py b/src/room.py index 24c07ad4c8..7dc1421880 100644 --- a/src/room.py +++ b/src/room.py @@ -1,2 +1,42 @@ # 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: + # construction function + def __init__(self, name, description): + self.name = name + self.description = description + self.room_items = [] + + def __str__(self): + return f"room name is {self.name}" + + def stealthy_add(self, item): + self.item = item + self.room_items.append(item) + + def add_room_item(self, item): + self.item = item + self.room_items.append(item) + for i in self.room_items: + print(f"{i.name} added to {self.name}") + + def room_inventory(self): + if (len(self.room_items) == 0): + print(f"\n\t>> There's nothing in this room. That sucks. Search other rooms!\n") + elif (len(self.room_items) > 0): + print(f"\n>> Look what you found at {self.name}:") + for i in self.room_items: + print(f"\t{i}") + print(f"\n") + else: + print(f"\n\t>> There's nothing in this room. That sucks. Search other rooms!\n") + + + + + + # def add_item(self, item): + # self.item = item + pass