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..97771227e3 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,364 @@ # # 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) + +spyglass = Item("Spyglass", "useful for reading") +narrow.stealthy_add(spyglass) + +note = Item("Note", "drops of blood with faint, shaky writing") +foyer.stealthy_add(note) +# MAKE EVERY NEW ITEM NAME ONE WORD!!! + +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. + +user_prompt = None + +while user_prompt != "q": + + # * 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: ")) + + nope ="\n---- YOU SHALL NOT PASS ----\n---- CHOOSE ANOTHER DIRECTION ----\n" + + player_items = [f"{data.name.lower()}" for data in new_player.player_items] + + + # If the user enters a cardinal direction, attempt to move to the room there. + + # NORTH + if user_prompt.lower() == 'n': + #OUTSIDE + if new_player.current_room == outside: + print("\n---- LET US GO NORTH ----\n") + new_player.current_room = foyer + print(new_player.current_room) + #FOYER + elif new_player.current_room == foyer: + print("\n---- LET US GO NORTH ----\n") + new_player.current_room = overlook + print(new_player.current_room) + #OVERLOOK + elif new_player.current_room == overlook: + print(nope) + print(new_player.current_room) + #NARROW + elif new_player.current_room == narrow: + print("\n---- LET US GO NORTH ----\n") + new_player.current_room = treasure + print(new_player.current_room) + #TREASURE + elif new_player.current_room == treasure: + print(nope) + print(new_player.current_room) + # SOUTH + elif user_prompt.lower() == 's': + #OUTSIDE + if new_player.current_room == outside: + print(nope) + print(new_player.current_room) + #FOYER + elif new_player.current_room == foyer: + print("\n---- LET US GO SOUTH ----\n") + new_player.current_room = outside + print(new_player.current_room) + #OVERLOOK + elif new_player.current_room == overlook: + print("\n---- LET US GO SOUTH ----\n") + new_player.current_room = foyer + print(new_player.current_room) + #NARROW + elif new_player.current_room == narrow: + print(nope) + print(new_player.current_room) + #TREASURE + elif new_player.current_room == treasure: + print("\n---- LET US GO SOUTH ----\n") + new_player.current_room = narrow + print(new_player.current_room) + # EAST + elif user_prompt.lower() == 'e': + #OUTSIDE + if new_player.current_room == outside: + print(nope) + print(new_player.current_room) + #FOYER + elif new_player.current_room == foyer: + print("\n---- LET US GO EAST ----\n") + new_player.current_room = narrow + print(new_player.current_room) + #OVERLOOK + elif new_player.current_room == overlook: + print(nope) + print(new_player.current_room) + #NARROW + elif new_player.current_room == narrow: + print(nope) + print(new_player.current_room) + #TREASURE + elif new_player.current_room == treasure: + print(nope) + print(new_player.current_room) + # WEST + elif user_prompt.lower() == 'w': + #OUTSIDE + if new_player.current_room == outside: + print(nope) + print(new_player.current_room) + #FOYER + elif new_player.current_room == foyer: + print(nope) + print(new_player.current_room) + #OVERLOOK + elif new_player.current_room == overlook: + print(nope) + print(new_player.current_room) + #NARROW + elif new_player.current_room == narrow: + print("\n---- LET US GO WEST ----\n") + new_player.current_room = foyer + print(new_player.current_room) + #TREASURE + elif new_player.current_room == treasure: + print(nope) + print(new_player.current_room) + + # 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() + if 'note' and 'spyglass' in player_items: + print(f"\n\t.. [UNLOCKED]\n\tNow you have the spyglass and the note, you can select [READ] to read the note!!") + + # ITEM ONLY + + # AXE + elif user_prompt.lower() == 'axe': + 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\tIf 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\tIf 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\tIf 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\tIf 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\tIf 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\tIf 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] + + # player_items = [f"{data.name.lower()}" for data in new_player.player_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) + elif item_to_grab == 'spyglass': + print(f"\nYou have {item_to_grab}, now go find something to read!!\n") + new_player.add_player_item(spyglass) + elif item_to_grab == 'note': + new_player.add_player_item(note) + + elif item_to_grab in player_items: + print(f"\n>> You already have --{item_to_grab}-- in your stash, ya numpty!!\n\t>> Type [inv] to see whatcha got!\n") + + 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) + elif item_to_grab == 'spyglass': + new_player.drop_player_item(spyglass) + elif item_to_grab == 'note': + new_player.drop_player_item(note) + else: + print("\nThat item is not found here!!\nSearch for it elsewhere\n") + + # READ + elif user_prompt.lower() == 'read': + # player_items = [f"{data.name.lower()}" for data in new_player.player_items] + + if 'spyglass' in player_items and 'note' in player_items: + print("\nHere is what the note says:\n") + with open('src/text.txt') as text: + text_doc = text.read() + print(text_doc) + print("\n") + text.close() + + elif 'spyglass' in player_items and 'note' not in player_items: + print("\nGo get the note!!\n") + elif 'note' in player_items and 'spyglass' not in player_items: + print(f"\n\t>> The note's too faint.\n\t>> You need something to help you read.\n\t>> Look around!\n") + else: + print(f"\n\t>> You can't use this.\n\t>> You ain't got the gear yet!! Go look!!!\n") + + #UNLOCK + # elif 'spyglass' and 'note' in player_items: + # print(f"\n [UNLOCKED]\nNow you have the spyglass and the note, you can select [READ] to read the note!!") + + + # If the user enters "q", quit the game + elif user_prompt.lower() == 'q': + print("Thank you for playing \nGoodbye!") + break + # 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..3f2ff99556 100644 --- a/src/player.py +++ b/src/player.py @@ -1,2 +1,49 @@ # 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 + # print(self.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 diff --git a/src/text.txt b/src/text.txt new file mode 100644 index 0000000000..60624f7e70 --- /dev/null +++ b/src/text.txt @@ -0,0 +1,3 @@ +WATCH OUT +HOLY CRAP +THERE'S A MONSTER!! \ No newline at end of file