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
55 changes: 47 additions & 8 deletions src/adv.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
from room import Room
from player import Player
from item import Item

# Declare all the rooms
# Declare items
items = [
Item('umbrella', 'test description'),
Item('knife', 'test2 description'),
Item('watch', 'test3 description'),
Item('fork', 'test4 descritpion'),
Item('spoon', 'test5 description')
]
# print(items[0])

# Declare all the rooms
room = {
'outside': Room("Outside Cave Entrance",
"North of you, the cave mount beckons"),
'outside': Room("Outside Cave Entrance",
"North of you, the cave mount beckons", items),

'foyer': Room("Foyer", """Dim light filters in from the south. Dusty
'foyer': Room("Foyer", """Dim light filters in from the south. Dusty
passages run north and east."""),

'overlook': Room("Grand Overlook", """A steep cliff appears before you, falling
into the darkness. Ahead to the north, a light flickers in
'overlook': Room("Grand Overlook", """A steep cliff appears before you,
falling into the darkness. Ahead to the north, a light flickers in
the distance, but there is no way across the chasm."""),

'narrow': Room("Narrow Passage", """The narrow passage bends here from west
to north. The smell of gold permeates the air."""),
'narrow': Room("Narrow Passage", """The narrow passage bends here from
west to north. The smell of gold permeates the air."""),

'treasure': Room("Treasure Chamber", """You've found the long-lost treasure
chamber! Sadly, it has already been completely emptied by
Expand All @@ -33,11 +44,15 @@
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.
player = Player("Apetsi", room["outside"])
game_over = False
directions = ["n", "s", "e", "w"]

# Write a loop that:
#
Expand All @@ -49,3 +64,27 @@
# Print an error message if the movement isn't allowed.
#
# If the user enters "q", quit the game.
# direction = input("Enter a direction to move: ")

while not game_over:
print(f"You are currently in {player.current_room.name}\n")
print(f"Description: {player.current_room.description}\n")
print(f"These are the items in {player.current_room.name}")
for item in player.current_room.items:
print(item.name)
print("To move in any direction, use either of n, s, e, w\n")
print("Enter q or quit to exit\n")

direction = input("Enter a direction to move: ")

if direction == 'q' or direction == 'quit':
print("See you next time")
game_over = True
elif direction not in directions:
print("Enter a valid direction\n")
else:
direction += "_to"
if getattr(player.current_room, direction) is None:
print('No room in that direction, please try again')
else:
player.current_room = getattr(player.current_room, direction)
13 changes: 13 additions & 0 deletions src/item.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Item:
def __init__(self, name, description):
self.name = name
self.description = description

def on_take(self, item):
print(f"You have picked up {item}")

def on_drop(self, item):
print(f"You have dropped {item}")

def __str__(self):
return f"Name: {self.name}; Description: {self.description}"
20 changes: 20 additions & 0 deletions src/player.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,22 @@
# Write a class to hold player information, e.g. what room they are in
# currently.
from item import Item


class Player:
def __init__(self, name, current_room, inventory=[]):
self.name = name
self.current_room = current_room
self.inventory = inventory

def add_to_inventory(self, item):
new_item = Item(item.name, item.description)
self.inventory.append(new_item)

def get_item(self, item):
if item in [x.name for x in self.current_room.items]:
self.current_room.items.remove(item)
self.inventory.append(item)

def __str__(self):
return f"Name: {self.name}; current room: {self.current_room}"
21 changes: 20 additions & 1 deletion src/room.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,21 @@
# Implement a class to hold room information. This should have name and
# description attributes.
# description attributes.
from item import Item

class Room:
def __init__(self, name, description, items=[]):
self.name = name
self.description = description
self.n_to = None
self.e_to = None
self.w_to = None
self.s_to = None
self.items = items

def add_item(self, name, desc):
new_item = Item(name, desc)
self.items.append(new_item)

def __str__(self):
item_list = [item for item in self.items]
return f"Name: {self.name}; description: {self.description}; items: {item_list}\n"