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
107 changes: 86 additions & 21 deletions src/adv.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
import sys
import textwrap
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
passages run north and east."""),
'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
the distance, but there is no way across the chasm."""),
'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
earlier adventurers. The only exit is to the south."""),
'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."""),
}


# Link rooms together

room['outside'].n_to = room['foyer']
room['foyer'].s_to = room['outside']
room['foyer'].n_to = room['overlook']
Expand All @@ -33,14 +27,9 @@
room['narrow'].n_to = room['treasure']
room['treasure'].s_to = room['narrow']

#
# Main
#

# 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.
Expand All @@ -49,3 +38,79 @@
# Print an error message if the movement isn't allowed.
#
# If the user enters "q", quit the game.

playing = False

game_start = input('Would you like to start the adventure game? [y] [n]\n').lower()
if game_start == 'y':
playing = True
else:
print('Maybe next time then!')
sys.exit()

name = input('Greetings adventurer! What is your name? ')
player = Player(name, room['outside'])
print('-\n(Press [q] at anytime to quit the game)\n-\n')
print(f'{player}\n')

sword = Item('sword', 'a shiny sword')
shield = Item('shield', 'a sturdy shield')
spear = Item('spear', 'a pointy spear')
chest = Item('chest', 'an empty chest')

room['foyer'].add_item(spear)
room['overlook'].add_item(sword)
room['overlook'].add_item(shield)
room['treasure'].add_item(chest)

while playing == True:
action = input('---\nWhich way would you like to go? [n] [s] [e] [w]\nEnter [i] or [inventory] to view your items\n---\n').lower()

if action == 'q':
print(f'Bye {player.name}!')
sys.exit()
elif action in {'n', 's', 'e', 'w'}:
player.move(action)
player.explore()
elif len(action) > 1:
if action.split(' ')[0] in {'get', 'take'}:
item = [i for i in player.current_room.items if i.name == action.split(' ')[1]]
player.take_item(item[0])
else:
item = [i for i in player.items if i.name == action.split(' ')[1]]
player.drop_item(item[0])
elif action in {'i', 'inventory'}:
player.list_inventory()
else:
continue

# OLD ATTEMPT -------------------------------------------------------------------------
# if choice == 'q':
# playing = False
# print(f'Thanks for playing, {player.name}!')
# elif choice == 'n':
# if hasattr(player.current_room, 'n_to'):
# player.current_room = player.current_room.n_to
# print('\n---\n~ You enter the ' + player.current_room.name + ' ~\n---\n')
# else:
# print('[X] There is no path this way.')
# elif choice == 's':
# if hasattr(player.current_room, 's_to'):
# player.current_room = player.current_room.s_to
# print('\n---\n~ You enter the ' + player.current_room.name + ' ~\n---\n')
# else:
# print('[X] There is no way to get through.')
# elif choice == 'e':
# if hasattr(player.current_room, 'e_to'):
# player.current_room = player.current_room.e_to
# print('\n---\n~ You enter the ' + player.current_room.name + ' ~\n---\n')
# else:
# print('[X] There is no door this way.')
# elif choice == 'w':
# if hasattr(player.current_room, 'w_to'):
# player.current_room = player.current_room.w_to
# print('\n---\n~ You enter the ' + player.current_room.name + ' ~\n---\n')
# else:
# print('[X] This is a dead end.')
# else:
# print('Please enter a valid direction: [n] [s] [e] [w]\nOr press [q] to quit\n')
16 changes: 16 additions & 0 deletions src/item.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Item:
def __init__(self, name, description):
self.name = name
self.description = description

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

def on_drop(self, item):
print(f'You dropped the {item.name}')

def print_name(self):
print(f'• {self.name}')

def describe(self):
print(f'• {self.name}: {self.description}')
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, items=None):
self.name = name
self.current_room = current_room

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

def __str__(self):
return f"Name: {self.name}, Location: {self.current_room.name}"

def list_inventory(self):
print('Use [drop] [item] to drop items')
print('Your inventory:')
for i in self.items:
i.describe()

def move(self, direction):
next_room = getattr(self.current_room, direction + '_to', None)
if next_room != None:
self.current_room = next_room
print(f'---\nYou enter the {self.current_room.name}:')
print(f'{self.current_room.description}')
else:
print('---\nThere is no path this way.')

def explore(self):
self.current_room.list_inventory()

def take_item(self, item):
self.current_room.items.remove(item)
self.items.append(item)
action_item = [i for i in self.items if i.name == item.name]
action_item[0].on_take(action_item[0])

def drop_item(self, item):
self.items.remove(item)
self.current_room.items.append(item)
action_item = [i for i in self.current_room.items if i.name == item.name]
action_item[0].on_drop(action_item[0])
27 changes: 26 additions & 1 deletion src/room.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,27 @@
# Implement a class to hold room information. This should have name and
# description attributes.
# description attributes.
class Room:
def __init__(self, name, description, items=None):
self.name = name
self.description = description

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

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

def list_inventory(self):
if len(self.items) == 0:
print('> There are no items here.')
elif len(self.items) == 1:
print('\nYou found an item in this room!')
print('Use [get] [item] or [take] [item] to pick up items')
else:
print('\nYou found items in this room!')
print('Use [get] [item] or [take] [item] to pick up items')
for i in self.items:
i.print_name()
print('---')