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
66 changes: 66 additions & 0 deletions src/adv.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
from room import Room

from player import Player
from item import Item
import textwrap

# Declare all the rooms

room = {
Expand Down Expand Up @@ -38,6 +42,7 @@
#

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

# Write a loop that:
#
Expand All @@ -49,3 +54,64 @@
# Print an error message if the movement isn't allowed.
#
# If the user enters "q", quit the game.


player = Player("Blair", room["outside"])

room["foyer"].items = ["machete", "sword"]


while True:
#
# * Prints the current room name
# * Prints the current description (the textwrap module might be useful here).
print("\n")
print(player.location)
user_input = input(
"\nEnter (n), (s), (e), (w), (d), (i) for inventory, (take [item]), (drop [item]), (add [item]) or (q) to quit: ").split()
if len(user_input) == 1:

if user_input[0] == 'q':
print("Thanks for playing. We hope to see you again soon!")
break

if user_input[0] == 'n':

# move to the north
player.move(user_input[0])
if user_input[0] == 's':
# move to the south
player.move(user_input[0])
if user_input[0] == 'e':
# move to the east
player.move(user_input[0])
if user_input[0] == 'w':
# move to the west
player.move(user_input[0])
elif user_input[0] == 'i':
player.print_inv()

elif len(user_input) == 2:
if user_input[0] == "take" or user_input[0] == "get":
for item in player.location.items:
if item == user_input[1]:
player.inventory.append(item)
player.location.items.remove(item)
print(f"You have picked up {item}")
else:
print("No such item in this room")
elif user_input[0] == "drop":
for item in player.inventory:
if item == user_input[1]:
player.drop_item(user_input[1])
print(f"You have dropped {item}")
else:
print(
f"{player.name} doesn't have that item in their inventory - Sorry!")
elif user_input[0] == "add":
player.location.items.append(user_input[1])

else:
print("Invalid user entry enter drop or get followed by the item name")
else:
print("Invalid user entry")
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):
print(f"You have picked up {self.name}")

def on_drop(self):
print(f"You have dropped {self.name}")
29 changes: 29 additions & 0 deletions src/player.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,31 @@
from room import Room

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


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

def move(self, direction):

if hasattr(self.location, f'{direction}_to'):
self.location = getattr(self.location, f'{direction}_to')

else:
print("You can't go that direction")

def drop_item(self, item_d):
for item_of_player in self.inventory:
if item_of_player == item_d:
self.inventory.remove(item_of_player)
self.location.items.append(item_of_player)

def print_inv(self):
curr_inventory = "Current Inventory: "
for i, item in enumerate(self.inventory):
curr_inventory += " " + str(i+1) + "-" + item
print(curr_inventory)
22 changes: 21 additions & 1 deletion src/room.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,22 @@
# 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
self.n_to = None
self.s_to = None
self.e_to = None
self.w_to = None
self.items = []

def __str__(self):
# return f"Your current location is: {self.name} and description is: {self.description}"

if len(self.items) > 0:
item_list = "This room has the following: "
for i, item in enumerate(self.items):
item_list += " " + str(i+1) + "-" + item
return self.name + " " + self.description + "\n" + item_list
else:
return self.name + " " + self.description + "\n" + "There's currently no items in this room"