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
8 changes: 4 additions & 4 deletions Pipfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]

[dev-packages]

[packages]

[requires]
python_version = "3"
python_version = "3.7"
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.

59 changes: 43 additions & 16 deletions src/adv.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from room import Room
from player import Player
from item import Item

# Declare all the rooms

Expand Down Expand Up @@ -33,19 +35,44 @@
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.

# 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.
sword = Item('Sword', 'An old rusty sword')
book = Item('Book', 'A book about treasure')
rock = Item('Rock', 'This is a rock')
key = Item('Key', 'I wonder what this could be used for')
lighter = Item('Lighter', 'This could come in handy')

room['outside'].items.append(rock)
room['outside'].items.append(sword)
room['foyer'].items.append(lighter)
room['foyer'].items.append(book)
room['narrow'].items.append(key)


player_1 = Player('Liam', room['outside'])

current_room = player_1.current_room

print(current_room)

directions = ['n', 's', 'e', 'w']

while True:
user_input = input('--> ').strip().lower().split(' ')

if len(user_input) == 1:
if user_input[0] in directions:
player_1.move_player(user_input[0])
elif user_input[0] == 'i':
player_1.print_inventory()
elif user_input[0] == 'q':
print('Goodbye!')
exit()
else:
print('I did not recognise that command')
elif len(user_input) == 2:
if user_input[0] == 'get':
player_1.get_item(user_input[1])
elif user_input[0] == 'drop':
player_1.drop_item(user_input[1])
else:
print('I did not recognise that 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, description):
self.name = name
self.description = description

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

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


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

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

def __repr__(self):
return f'Player({self.name}, {self.current_room})'

def move_player(self, direction):
if getattr(self.current_room, f'{direction}_to') is not None:
self.current_room = getattr(self.current_room, f'{direction}_to')
print(self.current_room)
else:
print('Sorry! Unable to go that way.', '\n')

def print_inventory(self):
if len(self.items) > 0:
print('\nYou are carrying:\n ' +
', '.join([item.name for item in self.items]) + '\n')
else:
print('\nYou have 0 items\n')

def get_item(self, item_to_get):
items_in_room = [item.name.lower() for item in self.current_room.items]
if item_to_get in items_in_room:
for item in self.current_room.items:
if item.name.lower() == item_to_get:
player_item = item
self.items.append(player_item)
print(f'\nYou aquired a {player_item.name}\n')
self.current_room.items.remove(player_item)

# WIP
def drop_item(self, item_to_drop):
player_items = [item.name.lower() for item in self.items]
if item_to_drop in player_items:
for item in self.items:
if item.name.lower() == item_to_drop:
self.current_room.items.append(item)
self.items.remove(item)
print(f'\nYou dropped {item.name}\n')
47 changes: 46 additions & 1 deletion src/room.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,47 @@
# 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 __str__(self):
str = f'''\n--------------------------------'
\n{self.name}
\n {self.description}\n
\n{self.get_paths()}\n
{self._get_item_string()}'''
return str

def __repr__(self):
return f'Room({self.name}, {self.description})'

def _get_item_string(self):
if len(self.items) > 0:
return '\nItems in room:\n\n ' + ', '.join([item.name for item in self.items]) + '\n'
else:
return ''

def get_paths(self):
paths = []
if self.n_to is not None:
paths.append('n')
if self.s_to is not None:
paths.append('s')
if self.e_to is not None:
paths.append('e')
if self.w_to is not None:
paths.append('w')
return 'Paths: ' + ', '.join(paths)


# room_1 = Room('Testing', 'This room is for testing purposes')
# print(room_1)
# print(repr(room_1))