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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.vscode
*.pyc
__pycache__
__pycache__
99 changes: 89 additions & 10 deletions src/adv.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
from room import Room
from player import Player
from item import Item

# Declare all the rooms

room = {
'outside': Room("Outside Cave Entrance",
"North of you, the cave mount beckons"),
'outside': Room("Outside Cave Entrance",
"North of you, the cave mount beckons", [Item('backpack', 'put in stuff'), Item('pencil', 'stab someone')]),

'foyer': Room("Foyer", """Dim light filters in from the south. Dusty
'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
'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.""", [Item('gun', 'make holes in stuff')]),

'narrow': Room("Narrow Passage", """The narrow passage bends here from west
to north. The smell of gold permeates the air."""),
'narrow': Room("Narrow Passage", """The narrow passage bends here from west
to north. The smell of gold permeates the air.""", [Item('potion', 'heal yo\'self')]),

'treasure': Room("Treasure Chamber", """You've found the long-lost treasure
'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.""", [Item('dust', 'nothing 4 u')]),
}


Expand All @@ -38,7 +40,7 @@
#

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

player = Player('Adam', room['outside'])
# Write a loop that:
#
# * Prints the current room name
Expand All @@ -49,3 +51,80 @@
# Print an error message if the movement isn't allowed.
#
# If the user enters "q", quit the game.
eligibleMoves = {
'n': 'n_to',
's': 's_to',
'e': 'e_to',
'w': 'w_to',
't': True,
'd': True
}

def getItem(item):
for idx, obj in enumerate(player.currentRoom.items):
if obj.name == item:
player.items.append(obj)
obj.on_take()
del player.currentRoom.items[idx]
break

def dropItem(item):
for idx, obj in enumerate(player.items):
if obj.name == item:
player.currentRoom.items.append(obj)
obj.on_drop()
del player.items[idx]
break

def invalidCommand():
input('Error, please enter a valid command. Press [ENTER] to try again ')

input('Introduction: Move north, south, east, or west with: n, s, e, or w. Take or drop an item by typing: t item or d item. q to quit and inv for current inventory. Press [ENTER] to start game ')
while True:
print('----------------------------')
print(f'Room: {player.currentRoom.name}')
print(f'Description: {player.currentRoom.description}\n')

if len(player.currentRoom.items) != 0:
print("Items in the room: ")
for i, v in enumerate(player.currentRoom.items):
print(f'{i+1}. {v.name}')
else:
print('No items in room')
response = input("\nWhat do you want to do? ").lower().split()
if response[0] == 'q':
print('Goodbye')
break
if response[0] == 'inv':
if len(player.items) != 0:
print("\nItems in your inventory: ")
for i, v in enumerate(player.items):
print(f'{i+1}. {v.name}')
else:
print('\nYou have no items.')
input('Press [ENTER] to continue ')

elif len(response) == 1:
if response[0] in eligibleMoves.keys():
checkMove = getattr(player.currentRoom, eligibleMoves[response[0]])

if checkMove != None:
# change current room
player.currentRoom = getattr(player.currentRoom, eligibleMoves[response[0]])
else:
input('Cant move in this direction. Press [ENTER] to try again ')
else:
invalidCommand()
elif len(response) == 2:
if response[0] in eligibleMoves.keys():

if response[0] == 't' and response[1] in [item.name for item in player.currentRoom.items]:
getItem(response[1])
elif response[0] == 'd' and response[1] in [item.name for item in player.items]:
dropItem(response[1])
else:
input('Item not found there. Press [ENTER] to try again ')
else:
invalidCommand()
else:
invalidCommand()
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 on_take(self):
input(f"\nYou have picked up {self.name}. Press [ENTER] to continue ")

def on_drop(self):
input(f"\nYou have dropped {self.name}. Press [ENTER] to continue ")
6 changes: 6 additions & 0 deletions src/player.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
# Write a class to hold player information, e.g. what room they are in
# currently.

class Player:
def __init__(self, name, currentRoom, items=[]):
self.name = name
self.currentRoom = currentRoom
self.items = items
12 changes: 11 additions & 1 deletion src/room.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
# Implement a class to hold room information. This should have name and
# description attributes.
# description attributes.

class Room:
def __init__(self, name, description, items=[], n_to=None, s_to=None, e_to=None, w_to=None):
self.name = name
self.description = description
self.items = items
self.n_to = n_to
self.s_to = s_to
self.e_to = e_to
self.w_to = w_to
10 changes: 10 additions & 0 deletions znote
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
form
use 2 spaces
4 vs 2 spaces
uncomment intro
textwrap
drier code
using str in adv.py

google search for spacing adjustments:
change file from 4 to 2 spaces sublime