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: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
.vscode
*.pyc
__pycache__
__pycache__
intro3.py
.DS_Store
store.py
milk.py
category.py
/item.py
177 changes: 162 additions & 15 deletions src/adv.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,64 @@
from os import system, name
from room import Room
from player import Player
from item import Item

# Declare all the rooms
direction = ''
verb = ''
target = ''

def clear():
if name == 'nt':
_ = system('cls')
else:
_ = system('clear')

# Items in each room

items_outside = [
Item('hammer', 'Squash your opponents'),
Item('potion', 'Makes your enemies dizzy'),
Item('sawdust', 'It blinds your enemies')
]

items_foyer = [
Item('potion', 'Makes your enemies dizzy')
]

items_overlook = [
Item('rock', 'You can throw it'),
Item('sand', 'You can eat it')
]

items_narrow = [
Item('sawdust', 'It blinds your enemies')
]

items_treasure = [
Item('chest', 'It\'s empty, though'),
Item('dagger', 'To stab lizards')
]

# Declare all the rooms
# dictionary of rooms
room = {
'outside': Room("Outside Cave Entrance",
"North of you, the cave mount beckons"),
"North of you, the cave mount beckons",
items_outside),

'foyer': Room("Foyer", """Dim light filters in from the south. Dusty
passages run north and east."""),
passages run north and east.""", items_foyer),

'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."""),
the distance, but there is no way across the chasm.""", items_overlook),

'narrow': Room("Narrow Passage", """The narrow passage bends here from west
to north. The smell of gold permeates the air."""),
to north. The smell of gold permeates the air.""", items_narrow),

'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."""),
earlier adventurers. The only exit is to the south.""", items_treasure),
}


Expand All @@ -31,21 +71,128 @@
room['overlook'].s_to = room['foyer']
room['narrow'].w_to = room['foyer']
room['narrow'].n_to = room['treasure']
room['treasure'].s_to = room['narrow']
room['treasure'].s_to = room['narrow']

#
# Main
#

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

# clear screen
clear()

player = Player("George", room['outside'])


# Write a loop that:
selection = ''

while selection is not 'q':

# * Prints the current room name
print("\nCurrent room: " + player.current_room.name)

# * Prints the current description (the textwrap module might be useful here).
print(player.current_room.description)

# Print items in the room
print("\nItems in this room:")
for item in player.current_room.get_items():
print(item)

# Print Player items
print("\nPlayer has these items:")
for item in player.get_item_names():
print(item)

# * Waits for user input and decides what to do.
## dir = input("prompt").lower() # downcase # remove spaces .trim() .strip

# direction = input('\nEnter [n, s, e, w] to play, [q] to quit: ').lower().strip()
command = input('\nEnter [n, s, e, w] to play, [q] to quit: ').lower().split()
print('You entered ' + ' '.join(command))

if len(command) == 1:
direction = command[0]
elif len(command) > 1:
verb = command[0]
target = command[1]


# 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 len(direction) > 0:
if direction == 'q':
print('Goodbye!')
break

if direction == 'n':
if player.current_room.n_to is not None:
player.current_room = player.current_room.n_to
print(player.current_room.name)
else:
print("Can't move in that direction")

if direction == 's':
if player.current_room.s_to is not None:
player.current_room = player.current_room.s_to
else:
print("Can't move in that direction")

if direction == 'e':
if player.current_room.e_to is not None:
player.current_room = player.current_room.e_to
else:
print("Can't move in that direction")

if direction == 'w':
if player.current_room.w_to is not None:
player.current_room = player.current_room.w_to
else:
print("Can't move in that direction")

if direction not in ('n', 's', 'e', 'w'):
print('Wrong selection, please enter [n, s, e, w] only!')


if len(verb) > 0:
print(verb + ' ' + target)

if verb == 'get' and target in player.current_room.get_item_names():
# find item to remove
item = player.current_room.get_item_by_name(target)
print(player.current_room.get_item_names())
print(item)
# remove item from room
player.current_room.remove_item(item)
# add item to player
player.add_item(item)

print("Added item to player")
print("Player has:")
print(player.get_item_names())
print("Room has:")
print(player.current_room.get_item_names())


# consider switch statement

## if dir == 'n':
## # move north
## # is there a north room?
## # check if n_to has a value or if it's None
## # if this value is equal to None
## # .n_to NOT None
## # Move the player to a different room
## # update player room
## # print new room info
##
## # if there isn't a north
## # print error message, don't update player room
##
## elif...

#
# * 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.
# If the user enters "q", quit the game.
12 changes: 12 additions & 0 deletions src/item.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Item:
def __init__(self, name, description):
self.name = name
self.description = description

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


# my_item = Item("bucket", "A bucket of paint")
# print(my_item)

39 changes: 39 additions & 0 deletions src/player.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,41 @@
from room import Room
from item import Item

# Write a class to hold player information, e.g. what room they are in
# currently.

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

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

def get_item_names(self):
names = []

for item in self.items:
names.append(item.name)

return names


# For testing class
# my_items = [
# Item('hammer', 'A hammer'),
# Item('potion', 'Abracadabra'),
# Item('sawdust', 'It blinds')
# ]
#
# my_room = Room('outside', 'this is out', my_items)
#
# my_player = Player('Winnie', my_room)
#
# print(my_player.name)
# print(my_player.get_item_names())
#
# my_player.add_item(my_items[0])
#
# print(my_player.get_item_names())
59 changes: 58 additions & 1 deletion src/room.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,59 @@
from item import Item

# Implement a class to hold room information. This should have name and
# description attributes.
# description attributes.

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

def __str__(self):
item_list = ''

for item in self.items:
item_list += f'\nItem: {item.name} - {item.description}'

return f'{self.name}: {self.description} {item_list}'

def get_items(self):
return self.items

def get_item_names(self):
names = []

for item in self.items:
names.append(item.name)

return names

def get_item_by_name(self, name):
found_item = None

for item in self.items:
if item.name == name:
found_item = item

return found_item

def remove_item(self, item):
self.items.remove(item)



# For testing the class
# my_items = [
# Item('hammer', 'A hammer'),
# Item('potion', 'Abracadabra'),
# Item('sawdust', 'It blinds')
# ]
#
# my_room = Room('outside', 'this is out', my_items)
#
# print(my_room)
# print(my_room.get_item_names())