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
4 changes: 3 additions & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ name = "pypi"
[packages]

[dev-packages]
pylint = "*"
autopep8 = "*"

[requires]
python_version = "3"
python_version = "3"
111 changes: 111 additions & 0 deletions Pipfile.lock

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

90 changes: 52 additions & 38 deletions src/adv.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,7 @@
from room import Room

# Declare all the rooms

room = {
'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."""),

'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."""),

'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."""),
}

from player import Player
from data import room_data as room, options_data as options, item_data as item
from utils import pretty_print

# Link rooms together

Expand All @@ -33,19 +14,52 @@
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.
# Add items to rooms
room['foyer'].items = [item['sword'], item['potion'], item['book']]
room['overlook'].items = [item['book'], item['potion']]
room['narrow'].items = [item['bag']]
room['treasure'].items = [item['treasure']]

welcome = '*.*.*. ADVENTURE GAME .*.*.*'
exitMessage = 'Thank you for playing Adventure Game!'
player1 = Player('Bob', room['outside'])

pretty_print(welcome)
pretty_print(*options.values())
pretty_print(player1.room.name, player1.room.description)

selection = ''

while selection != 'q':

selection = input(
'\nWhat would you like to do? ')

selection_words = selection.split(' ')
selection_word_length = len(selection_words)

try:
if selection_word_length == 1:
if selection == 'q':
pretty_print(exitMessage)
elif selection in ['n', 'e', 's', 'w']:
player1.travel(selection)
elif selection == 'i':
player1.show_inventory()
elif selection == 'r':
player1.room.show_items()
elif selection == 'o':
pretty_print(*options.values())
else:
pretty_print('You didn\'t enter a valid option. Please try again.')
pretty_print(*options.values())
elif selection_word_length == 2:
if selection_words[0] == 'drop':
player1.drop_item(selection_words[1])
elif selection_words[0] == 'take':
player1.take_item(selection_words[1])
else:
pretty_print('You didn\'t enter a valid option. Please try again.')
pretty_print(*options.values())
except ValueError:
pretty_print('Please choose one of the available options')
40 changes: 40 additions & 0 deletions src/data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from room import Room
from item import Item

options_data = {
'title': '- - - Options - - -',
'travel': '* Travel *',
'travel_options': '| [n] North | [e] East | [s] South | [w] West |',
'items': '* Items *',
'items_options': '| [i] Show Inventory | [r] Room Items |',
'other': '* Other *',
'other_options': '| [o] Options | [q] Quit |'
}

item_data = {
'sword': Item("Sword", "A plain old rusty sword"),
'book': Item("Book", "It doesn't look like anyone has read this in a while..."),
'bag': Item("Bag", "A small satchel."),
'potion': Item("Potion", "A small bottle of glowing, green liquid."),
'treasure': Item('Treasure', "Oooh... It's shiny....")
}
# Declare all the rooms

room_data = {
'outside': Room("Outside Cave Entrance",
"North of you, the cave mouth beckons"),

'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."""),

'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."""),
}
7 changes: 7 additions & 0 deletions src/item.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Item:
def __init__(self, name, description):
self.name = name
self.description = description

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

from utils import pretty_print


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

def travel(self, direction):
next_room = self.room.get_next_room(direction)
if next_room:
self.room = next_room
pretty_print(self.room.name, self.room.description)
else:
pretty_print('\nOh no! You can\'t go that way...\n')

def take_item(self, item):
for i in self.room.items:
if item.lower() == i.name.lower():
self.inventory.append(i)
self.room.items.remove(i)
pretty_print(
f'You have picked up the {i.name} in {self.room.name}. {i.name} has been added to your inventory.')

def drop_item(self, item):
for i in self.inventory:
if item.lower() == i.name.lower():
self.inventory.remove(i)
self.room.items.append(i)
pretty_print(
f'You have dropped the {i.name} in {self.room.name}. {i.name} has been removed from your inventory.')

def show_inventory(self):
if len(self.inventory) > 0:
pretty_print('- - Inventory - -')
for i in self.inventory:
pretty_print(i.name, i.description)
pretty_print(
'To drop an item, use the command \'drop item_name\' where item_name is the name of the item you wish to drop')
else:
pretty_print("There are no items in your inventory.")

def __str__(self):
return f"{self.name} is currently in {self.room.name}"
39 changes: 38 additions & 1 deletion src/room.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,39 @@
# Implement a class to hold room information. This should have name and
# description attributes.
# description attributes.

from utils import pretty_print


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

def get_next_room(self, direction):
if direction == 'n':
return self.n_to
elif direction == 's':
return self.s_to
elif direction == 'e':
return self.e_to
elif direction == 'w':
return self.w_to
else:
return None

def show_items(self):
if self.items:
pretty_print('- - Room Items - -')
for i in self.items:
pretty_print(i.name, i.description)
pretty_print('To pick up an item, use the command \'take item_name\' where item_name is the name of the item you wish to pick up')
else:
pretty_print("There are no items this room.")

def __str__(self):
return f"{self.name}\n{self.description}"
11 changes: 11 additions & 0 deletions src/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from textwrap import wrap

def pretty_print(*content):
width = 50

print('\n+-' + '-' * width + '-+')

for c in content:
for line in wrap(c, width):
print('| {0:^{1}} |'.format(line, width))
print('+-' + '-'*(width) + '-+')