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

[dev-packages]
autopep8 = "*"

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

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

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Intro to Python II

# Jose Montero
Up to this point, you've gotten your feet wet by working on a bunch of small Python programs. In this module, we're going to continue to solidify your Python chops by implementing a full-featured project according to a provided specification.


Expand Down
40 changes: 38 additions & 2 deletions src/adv.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
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"),
"""North of you, the cave mount beckons"""),

'foyer': Room("Foyer", """Dim light filters in from the south. Dusty
passages run north and east."""),
Expand Down Expand Up @@ -33,12 +35,30 @@
room['narrow'].n_to = room['treasure']
room['treasure'].s_to = room['narrow']


# items

items = {
"torch": Item("torch", "The light will help you see the way"),
"sword": Item("sword", "Hattori Hanzo to defeat your enemies"),
"shield": Item("shield", "Block your enemy's blows"),
"crown": Item("crown", "Claim your rightful place on the throne"),
"ring": Item("ring", "family heirloom, reminder of your greatness")
}

# Add to rooms
room['outside'].add_item(items["torch"])
room['foyer'].add_item(items["sword"])
room['overlook'].add_item(items["shield"])
room['narrow'].add_item(items["crown"])
room['treasure'].add_item(items["ring"])
#
# Main
#

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

player = Player(input("Please enter your name: "), room['foyer'])
print(player.current_room)
# Write a loop that:
#
# * Prints the current room name
Expand All @@ -49,3 +69,19 @@
# Print an error message if the movement isn't allowed.
#
# If the user enters "q", quit the game.

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

while True:
# Read command
cmd = input("Please enter a direction: ").lower()
# Check if it's n/s/e/w/q
if cmd in directions:
# Make player travel in that direction
player.travel(cmd)
elif cmd == "q":
# Quit
print("Thanks for playing!")
exit()
else:
print("Invalid command, please try again")
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, description, name):
self.description = description
self.name = name

items = []

def on_take(self):
print(f"You have picked up {self.name}")

def on_drop(self):
print(f"You have dropped {self.name}")
16 changes: 16 additions & 0 deletions src/player.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,18 @@
# 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

def travel(self, direction):
# Player should be able to move in a direction
next_room = self.current_room.get_room_in_direction(direction)
if next_room is not None:
self.current_room = next_room
print(self.current_room)
else:
print("You cannot move in that direction.")
48 changes: 47 additions & 1 deletion src/room.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,48 @@
# Implement a class to hold room information. This should have name and
# description attributes.
# description attributes.


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

def __str__(self):
display_string = ""
display_string += f"\n----------------\n"
display_string += f"\n{self.name}\n"
display_string += f"\n{self.description}\n"
display_string += f"\n{self.get_exits_string()}\n"
return display_string

def get_room_in_direction(self, direction):

return getattr(self, f"{direction}_to", None)

def get_exits(self):
exits = []
if self.n_to:
exits.append("n")
if self.s_to:
exits.append("s")
if self.e_to:
exits.append("e")
if self.w_to:
exits.append("w")
return exits

def get_exits_string(self):
return f"Exits: {', '.join(self.get_exits())}"

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

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