Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
253 changes: 242 additions & 11 deletions src/adv.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
from room import Room
from player import Player
from item import Item

import textwrap


# Declare all the rooms

Expand All @@ -22,8 +27,7 @@
}


# Link rooms together

# START Link rooms together
room['outside'].n_to = room['foyer']
room['foyer'].s_to = room['outside']
room['foyer'].n_to = room['overlook']
Expand All @@ -32,20 +36,247 @@
room['narrow'].w_to = room['foyer']
room['narrow'].n_to = room['treasure']
room['treasure'].s_to = room['narrow']
# END Link rooms together

# START Define Items
items = {
"map" : Item("map","So you can find your way around"),
"sword" : Item("sword","A Warrior's Tool"),

"coins" : Item("coins", "Money to Buy Things"),
"food" : Item("food", "In case you get hungry"),

"rope" : Item("rope", "To Climb"),
"telescope" : Item("telecope", "So you can see far"),

"torch" : Item("torch", "To see in the dark"),
"potion" : Item("potion", "Gives you a boost of energy"),

"treasure" : Item("treasure", "You found it")
}
# END Define Items

# START Set Items in Rooms
room['outside'].set_items([items["sword"], items["map"]])
room['foyer'].set_items([items["coins"], items["food"]])
room['overlook'].set_items([items["rope"], items["telescope"]])
room['narrow'].set_items([items["torch"], items["potion"]])
room['treasure'].set_items([items["treasure"]])
# END Set Items in Rooms

#
# Main
#

# Make a new player object that is currently in the 'outside' room.
player = Player("player_1", room["outside"])


# 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.
quit = False


while quit == False:

current_room = player._get_current_room()

# * Prints the current room name

current_room_items = ''

if (len(current_room.items) == 0):

current_room_items = "Room is Empty"

else:

for item in current_room.items:

current_room_items = current_room_items + item.name + ", "


print("\n ----Current Location---- \n\n","-", current_room.name,"-", "\n\n", textwrap.fill(current_room.description, width=50), "\n\n Items in this Room -> ", current_room_items,"\n\n------------------------" )


# * Waits for user input and decides what to do.

x = input("\n ----MENU---- \n N: Travel North \n S: Travel South \n E: Travel East \n W: Travel West \n I: Check Room for Items \n P: Check Players Inventory \n Take [Item]: Take Item \n Drop [Item]: Drop Item \n Q: Quit Game \n\n Enter Your Choice: ")

#
# 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.
if (x.lower() == 'n'):

try:

player._set_current_room(current_room.n_to)

except:

print("Invalid Room, Try Again")

x = input("\n ----MENU---- \n N: Travel North \n S: Travel South \n E: Travel East \n W: Travel West \n I: Check Room for Items \n P: Check Players Inventory \n Take [Item]: Take Item \n Drop [Item]: Drop Item \n Q: Quit Game \n\n Enter Your Choice: ")

elif (x.lower() == 's'):

try:

player._set_current_room(current_room.s_to)

except:

print("Invalid Room, Try Again")

x = input("\n ----MENU---- \n N: Travel North \n S: Travel South \n E: Travel East \n W: Travel West \n I: Check Room for Items \n P: Check Players Inventory \n Take [Item]: Take Item \n Drop [Item]: Drop Item \n Q: Quit Game \n\n Enter Your Choice: ")

elif (x.lower() == 'e'):

try:

player._set_current_room(current_room.e_to)

except:

print("Invalid Room, Try Again")

x = input("\n ----MENU---- \n N: Travel North \n S: Travel South \n E: Travel East \n W: Travel West \n I: Check Room for Items \n P: Check Players Inventory \n Take [Item]: Take Item \n Drop [Item]: Drop Item \n Q: Quit Game \n\n Enter Your Choice: ")

elif (x.lower() == 'w'):

try:

player._set_current_room(current_room.w_to)

except:

print("Invalid Room, Try Again")

x = input("\n ----MENU---- \n N: Travel North \n S: Travel South \n E: Travel East \n W: Travel West \n I: Check Room for Items \n P: Check Players Inventory \n Take [Item]: Take Item \n Drop [Item]: Drop Item \n Q: Quit Game \n\n Enter Your Choice: ")

elif (x.lower() == 'i'):

try:

print("\n Items In This Room Are: \n ")

if len(current_room.items) == 0:

print("This room is Empty ")

else:

for idx, item in enumerate(current_room.items):

print(idx+1, " - Item Name: ", item.name)

print("\n")

except:

print("Error Getting Items, Try Again")

x = input("\n ----MENU---- \n N: Travel North \n S: Travel South \n E: Travel East \n W: Travel West \n I: Check Room for Items \n P: Check Players Inventory \n Take [Item]: Take Item \n Drop [Item]: Drop Item \n Q: Quit Game \n\n Enter Your Choice: ")

elif (x.lower() == 'p'):

try:

print("\n Items In Player Inventory Are: \n ")

if len(player.items) == 0:

print("Your Inventory is Empty ")

else:

for idx, item in enumerate(player.items):

print(idx+1, " - Item Name: ", item.name)

print("\n")

except:

print("Error Checking Player Inventory")

x = input("\n ----MENU---- \n N: Travel North \n S: Travel South \n E: Travel East \n W: Travel West \n I: Check Room for Items \n P: Check Players Inventory \n Take [Item]: Take Item \n Drop [Item]: Drop Item \n Q: Quit Game \n\n Enter Your Choice: ")

elif (x.lower().strip().split()[0] == 'take'):

found = False

item_name = x.lower().strip().split()[1]

try:

for item in current_room.items:

if item.name == item_name:

found = True

player.items.append(item)

new_room_contents = [item for item in current_room.items if item.name != item_name]

current_room.items = new_room_contents

item.on_take()

if found == False:

print(f'\n {item_name} not in the Room')

except:

print("Error Taking Items, Try Again")

x = input("\n ----MENU---- \n N: Travel North \n S: Travel South \n E: Travel East \n W: Travel West \n I: Check Room for Items \n P: Check Players Inventory \n Take [Item]: Take Item \n Drop [Item]: Drop Item \n Q: Quit Game \n\n Enter Your Choice: ")


elif (x.lower().strip().split()[0] == 'drop'):

found = False

drop_item_name = x.lower().strip().split()[1]

try:

for item in player.items:

if item.name == drop_item_name:

found = True

new_list = [item for item in player.items if item.name != drop_item_name]

player.set_items(new_list)

item.on_drop()

if found == False:

print(f'\n {drop_item_name} not in Player Inventory')

except:

print("Error Dropping Items, Try Again")

x = input("\n ----MENU---- \n N: Travel North \n S: Travel South \n E: Travel East \n W: Travel West \n I: Check Room for Items \n P: Check Players Inventory \n Take [Item]: Take Item \n Drop [Item]: Drop Item \n Q: Quit Game \n\n Enter Your Choice: ")


elif (x.lower() == 'q'):

quit = True

print("Game Has Been Quit")

else:

print("Error Quitting Game")




19 changes: 19 additions & 0 deletions src/item.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Item:
def __init__(self, name, description):

# Define Attributes
self.name = name
self.description = description

# object.__str__ method to allow for direct printing of object contents
def __str__(self):
return f'Item Name: {self.name} - Item Description: {self.description}'

# Prints the Item Name on_take
def on_take(self):
print(f'\n You have picked up a {self.name}')

# Prints the Item Name on_drop
def on_drop(self):
print(f'\n You have dropped the {self.name}')

42 changes: 42 additions & 0 deletions src/player.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,44 @@
# Write a class to hold player information, e.g. what room they are in
# currently.

class Player:
def __init__(self, name, current_room):

# Define Attributes
self._name = name
self._current_room = current_room
self.items = []

# object.__str__ method to allow for direct printing of object contents
def __str__(self):
return f'Current Room: {self._current_room}'

# Define Methods

# Set Current Room
def _set_current_room(self, new_room):
if not new_room:
raise Exception("Invalid Room")
self._current_room = new_room
def _get_current_room(self):
return self._current_room

# Set Name
def _set_name(self, name):
if not name:
raise Exception("Invalid Name")
self._name = name
def _get_name(self):
return self._name

#Sets Items
def set_items(self, items):
self.items = items


#Get List of Items
def get_items(self):
return self.items

# property keyword wires up the getter and setter
room = property(_get_name, _set_name)
29 changes: 28 additions & 1 deletion src/room.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,29 @@
# Implement a class to hold room information. This should have name and
# description attributes.
# description attributes.

class Room:
def __init__(self, name, description):

# Define attributes
self.name = name
self.description = description
self.n_to = ''
self.s_to = ''
self.e_to = ''
self.w_to = ''
self.items = []


# object.__str__ method to allow for direct printing of object contents
def __str__(self):
return f'Name: {self.name} -> Desc: {self.description} -> Items: {self.items}'

# Gets Description
def get_description(self):
return self.description

#Sets Items
def set_items(self, items):
self.items.extend(items)