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.

60 changes: 57 additions & 3 deletions src/adv.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
from room import Room
from player import Player
from item import Item

# Declare all the items
flashlight = Item('Flashlight', 'May it be a light to you in dark places')
compass = Item('Compass', 'May this point you in the right directions')
sword = Item('Sword', 'May this help in your defense')
# shield = Item('Shield', 'May this sheild you')
# amour = Item('Armour', 'May this protect you')

# Declare all the rooms

# Declare all the rooms
room = {
'outside': Room("Outside Cave Entrance",
"North of you, the cave mount beckons"),
Expand Down Expand Up @@ -33,19 +42,64 @@
room['narrow'].n_to = room['treasure']
room['treasure'].s_to = room['narrow']

# Link items to rroms
room['outside'].append_items(flashlight)
room['foyer'].append_items(compass)
room['overlook'].append_items()
room['narrow'].append_items(sword)
room['treasure'].append_items()
#
# Main
#

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


player = Player(input('\n-> Hi! Welcome to the game. To get started please enter your name: '), room['outside'])
print(f'\nHello, {player.name}!')
#direction = ''
# Write a loop that:
#
print('\n To move into a room your options are: [n] North [s] South [e] East [w] West [take item name] Add Item to Inventory [drop item name] Drop Item [i] Check Inventory or [q] Quit')
print(player.current_room)

while True:
if player.current_room.name == 'Treasure Chamber':
print('\n CONGRATS! YOU WIN!', player.name)
#print('\n', player.name, '!')
if len(player.current_room.items) >= 1:
print("\nItems in this room: ")
for item in player.current_room.items:
print(f' {item.name}: {item.description}')
cmd = input('--> ').lower()
cmd_words = cmd.split(' ')
if cmd in ['n', 's', 'e', 'w']:
player.travel(cmd)
elif len(cmd_words) > 1 and cmd_words[0] == 'take':
player.take_item(cmd_words[1])
elif len(cmd_words) > 1 and cmd_words[0] == 'drop':
player.drop_item(cmd_words[1])
elif cmd == 'i':
print('Your Inventory: ')
for item in player.inventory:
print(f'{item.name}')
elif cmd == 'q':
print('Goodbye!')
exit()
else:
print('Invalid Input, try again')
print('\nTo move into a room your options are: [n] North [s] South [e] East [w] West or [q] Quit')





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

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):
print(f'You picked up the {self.name}')

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

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

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

def travel(self, direction):
#Grab the n/s/e/w attribute from the room
next_room = getattr(self.current_room, f'{direction}_to')
#item = getattr(self.current_room, self.current_room.items)
if next_room is not None:
self.current_room = next_room
# self.current_room.items = item
print(self.current_room)
# print(self.current_room.items)
else:
print('You cannot go that direction')

def take_item(self, item):
found = False
for i in self.current_room.items:
if i.name.lower() == item:
self.inventory.append(i)
self.current_room.items.remove(i)
found = True
i.on_take()
if not found:
print(f'This {item} is not in this room')

def drop_item(self, item):
found = False
for i in self.inventory:
if i.name.lower() == item:
self.inventory.remove(i)
self.current_room.items.append(i)
found = True
i.on_drop()
if not found:
print(f'The {item} was not found in your inventory')

22 changes: 21 additions & 1 deletion src/room.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,22 @@
# Implement a class to hold room information. This should have name and
# description attributes.
# description attributes.

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

def append_items(self, *item):
for i in item:
self.items.append(i)

def __str__(self):
return (f'\n{self.name}:\n{self.description}\n')