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
81 changes: 71 additions & 10 deletions src/adv.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from room import Room

from player import Player
# Declare all the rooms

room = {
'outside': Room("Outside Cave Entrance",
"North of you, the cave mount beckons"),
"North of you, the cave mount beckons", ['rock', 'sword']),

'foyer': Room("Foyer", """Dim light filters in from the south. Dusty
passages run north and east."""),
Expand Down Expand Up @@ -38,14 +38,75 @@
#

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


def check_move(current_room, direction):
move = direction + '_to'
if hasattr(current_room, move):
return getattr(current_room, move)
else:
print('You cannot move there!')
return current_room


# print(player)
# 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.
while True:
# * Prints the current room name
print(f'{player.name} is now in the {player.current_room.name}.')
# * Prints the current description (the textwrap module might be useful here).
print(player.current_room.description)
# * Waits for user input and decides what to do.
move = input("\n>").lower().split()
if len(move) == 1:
move = move[0][0]
if move is 'inventory' or 'i':
if len(player.inventory) == 0:
print("There are no items in your inventory.")
else:
print(f'Your items: {player.inventory}')
elif move == 'q':
print('Exiting game')
break
player.current_room = check_move(player.current_room, move)
elif len(move) == 2:
item = move[1]
move = move[0]
if move == 'get' or 'take':
# ADD ITEM TO PLAYER'S INVENTORY
print(item)
player.get_item(item)
print(f'You picked up a {item}')
if move == 'q':
print('Exiting game')
break
player.current_room = check_move(player.current_room, move)

# move = input("\n>").lower()[0]
# if move == 'n' or 's' or 'e' or 'w':
# if player.current_room.n_to is None:
# print('You cannot move there!')
# else:
# player.current_room = player.current_room.n_to
# elif move == 's':
# if player.current_room.s_to is None:
# print('You cannot move there!')
# else:
# player.current_room = player.current_room.s_to
# elif move == 'e':
# player.current_room = player.current_room.e_to
# elif move == 'w':
# player.current_room = player.current_room.w_to

# elif move == 'q':
# print('Exiting game')
# break

# else:
# print('Invalid direction')
# 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.
4 changes: 4 additions & 0 deletions src/item.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Item:
def __init__(self, name, description):
self.name = name
self.description = description
14 changes: 14 additions & 0 deletions src/player.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,16 @@
# Write a class to hold player information, e.g. what room they are in
# currently.


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

def __str__(self):
return f'{self.name} is in {self.current_room.name}'

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


class Room:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice; if this is working how you want it to, the next step will be to add functionality for an item to be added and removed from the room class.

def __init__(self, name, description, items=None):
self.name = name
self.description = description
self.n_to = None
self.s_to = None
self.e_to = None
self.w_to = None
if items is None:
self.items = []
def remove_item(self, item):
self.items.remove(item)