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
20 changes: 20 additions & 0 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

104 changes: 98 additions & 6 deletions src/adv.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
from room import Room
from player import Player
from item import Item

# 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"),

'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
'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
'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
'treasure': Room("Treasure Chamber", """You've found the long-lost treasure
chamber! Sadly, it has already been completely emptied by
earlier adventurers. The only exit is to the south."""),
}
Expand All @@ -33,11 +34,19 @@
room['narrow'].n_to = room['treasure']
room['treasure'].s_to = room['narrow']

# Add items to rooms

room['outside'].items.append(Item('rock', "Just a rock, it's pretty small. More of a pebble really."))
room['foyer'].items.append(Item('sword', "It's pretty rusty. More likely to give you tetanus than cut anything."))
room['treasure'].items.extend([Item('gold coin', 'A single shiny coin.'), Item('stick', 'A small piece of wood.')])


#
# Main
#

# Make a new player object that is currently in the 'outside' room.
player = Player(input('What is your name?\n'), room['outside'])

# Write a loop that:
#
Expand All @@ -49,3 +58,86 @@
# Print an error message if the movement isn't allowed.
#
# If the user enters "q", quit the game.

'''
when there is no input, command will be an empty string inside a list
command will have 1 arg for movement and 2 args for interacting with items so it's necessary to make command a list
'''
command = ['']


while command[0] != 'q':
print(f'\n*** {player.current_room.name} ***')
print(f'{player.current_room.desc}')

if len(player.current_room.items) > 0:
print("\nOn the ground in front of you is: ")
for item in player.current_room.items:
print(f'{item.name}: {item.desc}')

command = input('\nCommands:\nMove: [n]orth, [s]outh, [e]ast, [w]est\nPick up item: [take item_name]\nDrop item: [drop item_name]\nOpen inventory: [i] or [inventory]\nQuit game: [q]\n').split()
# ^^^^^ splitting this input(hopefully) puts the commands into list form for when items are added

if len(command) < 2:
if command[0] == 'n':
try:
player.current_room = player.current_room.n_to
except AttributeError:
print("\nYou can't go north")

elif command[0] == 's':
try:
player.current_room = player.current_room.s_to
except AttributeError:
print("\nYou can't go south")

elif command[0] == 'e':
try:
player.current_room = player.current_room.e_to
except AttributeError:
print("\nYou can't go east")

elif command[0] == 'w':
try:
player.current_room = player.current_room.w_to
except AttributeError:
print("\nYou can't go west")

elif command[0] == 'i' or 'inventory':
if len(player.items) > 0:
print(f"\n{player.name}'s items:")
for item in player.items:
print(f"{item.name}: {item.desc}")
else:
print(f"\n{player.name}'s inventory is empty.")

elif command[0] != 'q':
print('\ninvalid command')

elif len(command) == 2:
if command[0] == 'take':
for item in player.current_room.items:
if item.name == command[1]:
try:
player.items.append(item)
player.current_room.items.remove(item)
item.on_take()
except AttributeError:
print("\nYou can't pick that up.")
else:
print("\nNo item here by that name.")

elif command[0] == 'drop':
for item in player.current_room.items:
if item.name == command[1]:
try:
player.items.remove(item)
player.current_room.items.append(item)
item.on_drop()
except AttributeError:
print("\nYou can't drop that.")
else:
print("\nYou don't have that item to drop!")

elif command[0] != 'q':
print('\ninvalid command')
10 changes: 10 additions & 0 deletions src/item.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Item:
def __init__(self, name, desc):
self.name = name
self.desc = desc

def on_take(self):
print(f'You picked up {self.name}')

def on_drop(self):
print(f'You dropped {self.name}')
10 changes: 10 additions & 0 deletions src/player.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
# Write a class to hold player information, e.g. what room they are in
# currently.

class Player:
def __init__(self, name, current_room, items=None):
self.name = name
self.current_room = current_room

if items is None:
self.items = []
else:
self.items = items
12 changes: 11 additions & 1 deletion src/room.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
# Implement a class to hold room information. This should have name and
# description attributes.
# description attributes.

class Room:
def __init__(self, name, desc, items=None):
self.name = name
self.desc = desc

if items is None:
self.items = []
else:
self.items = items