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
39 changes: 38 additions & 1 deletion src/adv.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
from room import Room
from player import Player

# Declare all the rooms

room = {
'outside': Room("Outside Cave Entrance",
"North of you, the cave mount beckons"),
"North of you, the cave mount beckons",
[
['food', 'you eat it'],
['drink', 'you drink it'],
]),

'foyer': Room("Foyer", """Dim light filters in from the south. Dusty
passages run north and east."""),
Expand Down Expand Up @@ -49,3 +54,35 @@
# Print an error message if the movement isn't allowed.
#
# If the user enters "q", quit the game.

player = Player("You", room['outside'])

print("-------------------")
print("TEXT ADVENTURE GAME")
print("-------------------\n")
print(f"Welcome, {player.name}\n")
print(player.current_room)

choices = ('n', 's', 'e', 'w')

# LOOP
while True:

# READ
cmd = input("\nEnter direction (options above, or q to quit): ")
cmd = cmd.split()
# EVAL
# If q, quit the loop
if cmd[0] == "q":
print("\nGoodbye!\n")
break
elif cmd[0] in choices:
player.travel(cmd[0])
elif cmd[0] == "i":
player.printInventory()
elif cmd[0] == "get" or cmd[0] == "take":
player.getItem(cmd[1])
elif cmd[0] == "drop":
player.dropItem(cmd[1])
else:
print("I did not understand that command\n")
4 changes: 4 additions & 0 deletions src/item.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Item:
def __init__(self, name, description):
self.name = name
self.current_room = description
40 changes: 40 additions & 0 deletions src/player.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,42 @@
# Write a class to hold player information, e.g. what room they are in
# currently.


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

def travel(self, direction):
if getattr(self.current_room, f"{direction}_to") is not None:
self.current_room = getattr(self.current_room, f"{direction}_to")
print(self.current_room)
else:
print("\nThere is no room in that direction.")

def printInventory(self):
print(f"\nYour inventory: {[item[0] for item in self.inventory]}")

def getInventory(self):
return [item[0] for item in self.inventory]

def getItem(self, item):
if item in self.current_room.getItemNames():
position = self.current_room.getItemNames().index(item)
self.inventory.append(self.current_room.items[self.current_room.getItemNames().index(item)])
self.current_room.items.pop(position)
print(f"\nYou got {item}\n")
print(self.current_room)
else:
print(f"\nYou cannot take {item} because it's not in this room.")

def dropItem(self, item):
if item in self.getInventory():
position = self.getInventory().index(item)
self.current_room.items.append(self.inventory[self.getInventory().index(item)])
self.inventory.pop(position)
print(f"\nYou dropped {item}\n")
print(self.current_room)
else:
print(f"\nYou cannot drop {item} because you don't have it.")
45 changes: 44 additions & 1 deletion src/room.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,45 @@
# Implement a class to hold room information. This should have name and
# description attributes.
# description attributes.
from item import Item


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

def __str__(self):
return_string = "---------"
return_string += "\n\n"
return_string += f"Currently in: {self.name}"
return_string += "\n"
return_string += self.description
return_string += "\n\n"
return_string += "Items in room: "
return_string += ", ".join(x for x in self.getItemNames())
return_string += "\n"
return_string += f"{self.get_exits_string()}"
return return_string

def get_exits_string(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 getItemNames(self):
return [item[0] for item in self.items]

def getItemDescriptions(self):
return [item[1] for item in self.items]