Skip to content

Tuesday py #517

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
267 changes: 235 additions & 32 deletions src/adv.py
Original file line number Diff line number Diff line change
@@ -1,51 +1,254 @@
from player import Player
from room import Room
from item import Item, Trinket, VitaminPack, Container

#Items List
necklace = Trinket(
-1,
"a priceless necklace is in one of these rooms somewhere",
1
)

notebook = Trinket(
-1,
"no one knows how old this notebook is or who even wrote it... there appears to be strange characters on several pages...and is that.. blood?!?",
1
)

backpack = Container(
1,
"backpack",
"this backpack can hold everything except maybe your sanity",
[necklace]
)

items = [necklace.name, notebook.name, backpack.name]

# Declare all the rooms
rooms = {
'outside': Room(
"Outside Cave Entrance",
"North of you, the cave mount beckons. The path is steep and treacherous, but the mystery draws you in."),

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. There is an aging yellow velvet couch on the corner and it that... a backpack?!?""",
[backpack]

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

'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. Or is that just the mold in the corners...? Through cobwebs and patch of dirt in the corner of the floor a small flash catches your eyes. Is that...no it can't be. It is a priceless antinque necklace that you quickly stash into your backpack."
""",
[necklace]
),

'treasure': Room("Treasure Chamber", """You've found the long-lost treasure
'treasure': Room(
"Treasure Chamber",
"""
You've found the long-lost rumored treasure
chamber! Sadly, it has already been completely emptied by
earlier adventurers. The only exit is to the south."""),
earlier adventurers. There are scribblings on the wall. It looks like nothing at first, but then you realize that it is a map of another complex entirely. You quickly sketch down the map in your notebook. The eery stillness of the chamber creeps you out majorly. Happy with the necklace and the mystery of the notebook's characters, as well as the enigma of the new map, you set off as fast as you can to figure out what it all means. You exit to find out what the next level brings.
"""),
}


# Link rooms together

room['outside'].n_to = room['foyer']
room['foyer'].s_to = room['outside']
room['foyer'].n_to = room['overlook']
room['foyer'].e_to = room['narrow']
room['overlook'].s_to = room['foyer']
room['narrow'].w_to = room['foyer']
room['narrow'].n_to = room['treasure']
room['treasure'].s_to = room['narrow']
rooms['outside'].n_to = rooms['foyer']
rooms['foyer'].s_to = rooms['outside']
rooms['foyer'].n_to = rooms['overlook']
rooms['foyer'].e_to = rooms['narrow']
rooms['overlook'].s_to = rooms['foyer']
rooms['narrow'].w_to = rooms['foyer']
rooms['narrow'].n_to = rooms['treasure']
rooms['treasure'].s_to = rooms['narrow']
#initiate the room sequence
room = rooms['outside']

#
# Main
#

# Make a new player object that is currently in the 'outside' room.
player = Player(room['outside'], "player one has arrived outside the room.")

i = input("Welcome to Mystery Volumes I: what should we call you?\n")
player = Player(i, room, 100, 0, 0)

print(f"Goodnight and goodluck, {player.name}")

def instructions():
return """
Welcome to the labyrinth dear wanderer...

* use [L] to look around
* [N,S,E,W] [North, South, East, West] [Up, Down, Right, Left] to travel in those directions
* [q] to quit
"""

def current_dirs():
currentDirs = directions()

if currentDirs.__contains__("n"):
currentDirs.extend(["north", "up", "forward", "forwards"])
if currentDirs.__contains__("s"):
currentDirs.extend(["south", "down", "backward", "backwards"])
if currentDirs.__contains__("e"):
currentDirs.extend(["east", "right"])
if currentDirs.__contains__("w"):
currentDirs.extend(["west", "left"])

return currentDirs

def directions():
directions = []

if hasattr(player.current_room, "n_to"):
directions.append("n")
if hasattr(player.current_room, "s_to"):
directions.append("s")
if hasattr(player.current_room, "e_to"):
directions.append("e")
if hasattr(player.current_room, "w_to"):
directions.append("w")

return directions

def travel(input):

input = input.lower()
if input in current_dirs():
if input == "n" or input == "north" or input == "up" or input == "forward" or input == "forwards":
player.current_room = player.current_room.n_to
elif input == "s" or input == "south" or input == "down" or input == "backward" or input == "backwards":
player.current_room = player.current_room.s_to
elif input == "e" or input == "east" or input == "right":
player.current_room = player.current_room.e_to
elif input == "w" or input == "west" or input == "left":
player.current_room = player.current_room.w_to
else:
print("Wrong Way! There's nothing over there.")

def look():
if player.current_room == rooms['outside']:

print("Looking around the room you spot an object:")

item_instructions = "take the object"

#when there isn't anything in the room anyhow

if len(player.current_room.items) == 0:
print(f"Nothing is there...try another room to {item_instructions}")
item_instructions = ""

#displaying the descriptions of each room
item_desc = [x.room_description for x in player.current_room.items]
for i, _ in enumerate(item_desc):
print(f"{player.current_room.items[i].name}: {item_desc[i]}")
#empty line
print()


def prompt(s):
# print a quicklist of commands
commands = f"(L to look around | {' | '.join(directions())} to travel | Q to quit | [Help|?] for common commands): "
prompt = f"\nWhat would you like to do, {player.name}?\n{commands}"

return input(s + prompt)

def parse(inpt):
commands = [
"travel", "walk", "run",
"take", "drop",
"look"
]
commands.extend(current_dirs())

#add items available in this room to the command list
commands.extend([item.name for item in player.current_room.items])

#add the player's items
commands.extend([item.name for item in player.inventory])
commands.append(player.leftHandItem.name)
commands.append(player.rightHandItem.name)

inpt = inpt.lower()
# list of the available command separated by space
inputList = inpt.split()

#only allow commands to be parsed
commands = []
for cmd in inputList:
if cmd in commands or cmd in items:
commands.append(cmd)

if len(commands) >= 1:
cmd1 = commands[0]

if len(commands) > 1:
cmd2 = commands[1]
if len(commands) >2:
print("only two commands can be used at a time separated by a space")
#win the game and exit
if cmd1 == "take" and cmd2 == "drop":
if player.current_room != rooms['treasure']:
print("Can you decipher anything in here?")
else:
print("""
Do you see the writing's on the wall somewhere over here?

"No treasure, but some scratches on the wall...typical. Wait, is that a ... map?"

You appoach the wall in the dimly lit room and realize that you are in fact staring at a map in front of you. You quickly get out the old notebook and sketch a copy of the map as quickly as you can.
""")

#parse verb commands
if cmd1 == "travel" or cmd1 == "run" or cmd1 == "walk":
travel(cmd2)
return

if cmd1 == "take" or cmd1 == "drop":
player.take(cmd2)
return

elif cmd1 == "drop":
player.dropItem(cmd2)
return

#singular commands
else:
dirs = ["n", "north", "up", "e", "east", "right", "s", "south", "down", "w", "west", "left"]

if inpt == "q":
exit(0)
elif inpt == "instructions" or inpt == "?" or inpt == "h":
print(instructions())
return
elif inpt in dirs:
travel(inpt)
return
elif inpt == "l":
look()
return
elif inpt == "inventory" or inpt == "i":
player.list_inventory()
return
print("invalid command")




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

def __str__(self):
return f"{self.name} is worth {self.vitamins} vitamins"
def to_inventory(self):
print(f"{self.name} was placed into your inventory")
def to_floor(self):
print(f"{self.name} was dropped on the floor.")

class Container(Item):
def __init__(self, id, name, description, room_description, inventory):
super().__init__(id, name, description, room_description, 0)
self.inventory = inventory

def letter_to_int(self, letter):
alphabet = list('abcdefghijklmnopqrstuvwxyz')
if letter in alphabet:
return alphabet.index(letter)

def int_to_letter(self, index):
alphabet = list('abcdefghijklmnopqrstuvwxyz')
if len(alphabet) >= index + 1:
return alphabet[index]

def list_inventory(self, player):
#turning a letter into bytes
letter_i = self.letter_to_int('a')

for item in self.inventory:
#decode to string and make uppercase
letter_s = self.int_to_letter(letter_i)
upper_str = letter_s.upper()
#output
print(f"{upper_str}: {item.name}")
#increment to the next byte (this will convert to the next character)
letter_i += 1
if len(self.inventory) > 0:
treasure = input("select a letter to take an item: ").lower()
index = self.letter_to_int(treasure)

if isinstance(index, int) and len(self.inventory) >= index + 1:
player.take(self.inventory[index])
self.inventory.pop(index)
else:
print(f"{treasure} cannot be removed from this chest")
else:
print(f"{self.name} is empty!")


class Trinket(Item):
def __init__(self, id, name, description, room_description, sparkle):
super().__init__(id, name, description, room_description)
self.sparkle = sparkle

class VitaminPack(Item):
def __init__(self, id, room_description):
super().__init__(id, "vitamins", "", room_description)
self.description = f"vitamins - to keep you going in circles longer"

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, current_room):
self.name = name
self.current_room = current_room
7 changes: 6 additions & 1 deletion src/room.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
# 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