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
75 changes: 65 additions & 10 deletions src/adv.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
from room import Room
from player import Player
from items import Items


#items

sword = Items("Sword","A powerful and deadly sword")
potion = Items("Potion", "Recovers a little bit of health")
shield = Items("Shield", "Protects you from light hits")



# Declare all the rooms

Expand All @@ -24,28 +35,72 @@

# 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']
room['outside'].connected_rooms["n"] = room['foyer']
room['foyer'].connected_rooms["s"] = room['outside']
room['foyer'].connected_rooms["n"] = room['overlook']
room['foyer'].connected_rooms["e"]= room['narrow']
room['overlook'].connected_rooms["s"] = room['foyer']
room['narrow'].connected_rooms["w"]= room['foyer']
room['narrow'].connected_rooms["n"] = room['treasure']
room['treasure'].connected_rooms["s"] = room['narrow']

#add items

room['outside'].add_item(sword)
room['foyer'].add_item(potion)
room['narrow'].add_item(shield)

#
# Main
#

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

myPlayer = Player("kevin",room["outside"])
command = ""
# Write a loop that:
while(command != "quit"):
#
# * Prints the current room name
# * Prints the current description (the textwrap module might be useful here).
print(myPlayer)
if myPlayer.location.name == "Treasure Chamber":
break
# * Waits for user input and decides what to do.
#
print("N = go north, S = go south, E = go east, W = go west\n")
print("Take items name or Drop items name")
command = input("Where do you wanna go now?")

# 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 command in set("nesw"):
myPlayer.move(command)
if len(command) > 1:
info = command.split()
term = ""
if info[0].lower() == "take" or "get":
if myPlayer.location.search_item(info[1].lower()):
myPlayer.add_item(myPlayer.location.search_item(info[1].lower()))
print(f"The item {info[1]} has been added")
myPlayer.location.remove_item(info[1].lower())
else:
print("The item asking for not here")

if info[0].lower() == "drop":
item = myPlayer.drop_item(info[1])
if item:
myPlayer.location.add_item(item)
print(f"Item {info[1]} has been added to the room")
else:
print("Item u wanna drop u dont have")








# If the user enters "q", quit the game.
7 changes: 7 additions & 0 deletions src/items.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Items():
def __init__(self, name, description):
self.name = name
self.description = description

def __str__(self):
return f"{self.name}\n{self.description}"
35 changes: 35 additions & 0 deletions src/player.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,37 @@
# Write a class to hold player information, e.g. what room they are in
# currently.

#Players should have a name and current_room attributes
class Player:
def __init__(self, name, location):
self.name = name
self.location = location
self.bag = []

def __str__(self):
output = ""
output += f"{self.location.name}\n"
output += f"{self.location.description}\n"
output += f"{self.location}\n"
output += "Items held: \n"
for x in self.bag:
output += x.name + "\n"

return output


def move(self, direction):
if self.location.connected_rooms[direction] is not None:
self.location = self.location.connected_rooms[direction]
else:
print("Sorry you have hit a wall, nothing in that direction...")

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

def drop_item(self, item_name):
for i in range(len(self.bag)):
if self.bag[i].name.lower() == item_name:
return self.bag.pop(i)
return None

54 changes: 53 additions & 1 deletion src/room.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,54 @@
# Implement a class to hold room information. This should have name and
# description attributes.
# description attributes.


'''

Put the Room class in room.py based on what you see in adv.py.
The room should have name and description attributes.
The room should also have n_to, s_to, e_to, and w_to attributes which point
to the room in that respective direction.
'''

class Room:
def __init__(self,name, description):
#attributes
self.name = name
self.description = description
self.connected_rooms = {
"n" : None,
"s" : None,
"w" : None,
"e" : None
}
self.items = []

def __str__(self):
output = ""
output += "Items in these room:" + "\n"
for i in self.items:
output += "--" + i.name + "\n"
return output

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

def search_item(self, item_name):
for i in self.items:
if(i.name.lower() == item_name):
return i
return None

def remove_item(self, item_name):
for i in range(len(self.items)):
if(self.items[i].name.lower() == item_name):
return self.items.pop(i)
return None