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
80 changes: 64 additions & 16 deletions src/adv.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from room import Room
from player import Player
from item import Item

# Declare all the rooms

Expand All @@ -21,6 +23,16 @@
earlier adventurers. The only exit is to the south."""),
}

# key = Item('key', "Old Key")
# old_latern = Item(
# 'old latern', "A latern sits on an old table in the foyer, cobb webs hang off it. Doesn't seem like some one has been in here in a while")

item = {
'key': Item('key', "Old Key"),
'latern': Item('latern', 'An old latern covered in dust & cob webs. The room illuminates as the item is picked up!'),
'random': Item('random', 'iuhklciuyaghdsukfh')
}


# Link rooms together

Expand All @@ -33,19 +45,55 @@
room['narrow'].n_to = room['treasure']
room['treasure'].s_to = room['narrow']

#
# Main
#

# Make a new player object that is currently in the 'outside' room.

# 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.

# PLAYER DECLARATION
new_player = input('Give your player a name: ')
player = Player(new_player, room['outside'])


# Room Item Declarations
room['outside'].add_room_item(item['key'])
room['outside'].add_room_item(item['random'])
room['foyer'].add_room_item(item['latern'])

command = ''


while True:
print(player)
command = input(
' \n \n Insert q to quit, \n Use n, s, e, w to move through out the map \n To pick up an item type: grab, followed by the item name in sight! \n \n To drop an item type: drop, followed by the item in your inventory. \n Make a move: ').split(" ")

new_room = getattr(player.current_room, command[0].lower() + "_to", None)
player_inventory = [item for item in player.items]

if(len(command) == 1):
if new_room:
player.current_room = new_room
elif (command[0] == 'q'):
break
else:
print(' \n \n !!!INVALID COMMAND, PLEASE TRY AGAIN!!!')

elif(len(command) == 2 and command[0].lower() == 'grab'):
item_choice = command[1].lower()
item_name = [item.name
for item in player.current_room.items_in_room]

if item_choice in item_name:
player.pick_up_item(item[item_choice].name)
player.current_room.remove_room_item(item[item_choice])
print(f'\n \n You have picked up a {item_choice}!')
# print('ITEMS:', str(command[1].lower()))
else:
print(
f' \n \n Sorry but {item_choice} does not exist in {player.current_room}')

elif(len(command) == 2 and command[0].lower() == 'drop'):
item_choice = command[1].lower()
if item_choice in player_inventory:
player.drop_item(item[item_choice].name)
player.current_room.add_room_item(item[item_choice])
else:
print(
f' \n \n SORRY, BUT {item_choice} DOES NOT EXIST IN YOUR INVENTORY \n \n')
9 changes: 9 additions & 0 deletions src/item.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Item base class should have name & description(single word).

class Item:
def __init__(self, name, description):
self.name = name
self.description = description

def __str__(self):
return f'Name: {self.name}, {self.description}'
18 changes: 17 additions & 1 deletion src/player.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,18 @@
# Write a class to hold player information, e.g. what room they are in
# currently.
# currently. Change

class Player:
# attributes: name, current_room, items,
def __init__(self, name, current_room):
self.name = name
self.current_room = current_room
self.items = []

def __str__(self):
return f' \n \n Name: {self.name}. \n {self.current_room} \n Inventorty: {self.items}'

def pick_up_item(self, item):
self.items.append(item)

def drop_item(self, item):
self.items.remove(item)
24 changes: 23 additions & 1 deletion src/room.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,24 @@
# Implement a class to hold room information. This should have name and
# description attributes.
# description attributes.

class Room:
# Room contains: name, description, items_in_room. Methods: add_item
def __init__(self, name, description):
self.name = name
self.description = description
self.items_in_room = []
self.n_to = ''
self.s_to = ''
self.e_to = ''
self.w_to = ''

def __str__(self):
item_names = [str(item.name) for item in self.items_in_room]
return f'Location: {self.name} \n Hint: {self.description} \n. Items in sight: {item_names}'

def add_room_item(self, item):
self.items_in_room.append(item)

def remove_room_item(self, item):
self.items_in_room.remove(item)