0% found this document useful (0 votes)
3 views6 pages

Room Code

The document defines a Room class for a text-based adventure game, allowing for the creation of rooms with items, exits, and grabbable objects. It includes methods for adding exits, items, and managing the player's inventory. The game loop facilitates player interaction by processing commands to navigate between rooms and interact with items.

Uploaded by

booanabelle
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views6 pages

Room Code

The document defines a Room class for a text-based adventure game, allowing for the creation of rooms with items, exits, and grabbable objects. It includes methods for adding exits, items, and managing the player's inventory. The game loop facilitates player interaction by processing commands to navigate between rooms and interact with items.

Uploaded by

booanabelle
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

class Room:

def __init__(self, name):


self.name = name # string
self.items = [] # list of strings
self.exits = []
self.grabbables = []
self.exitLocation = []
self.itemDescriptions = [] # list of strings

@property
def name(self):
return self._name

@name.setter
def name(self,name):
self._name = name

@property
def items(self):
return self._items

@items.setter
def items(self,items):
self._items = items

@property
def exits(self):
return self._exits

@exits.setter
def exits(self,exits):
self._exits = exits

@property
def grabbables(self):
return self._grabbables

@grabbables.setter
def grabbables(self, grabbable):
self._grabbables = grabbable

@property
def exitLocation(self):
return self._exitLocation

@exitLocation.setter
def exitLocation(self, exitLocation):
self._exitLocation = exitLocation

@property
def itemDescriptions(self):
return self._itemDescriptions

@itemDescriptions.setter
def itemDescriptions(self, description):
self._itemDescriptions = description

def addExit(self, exitName, destinationRoom):


self._exits.append(exitName)
self._exitLocation.append(destinationRoom)

def addItem(self, itemName, itemDescription):


self._items.append(itemName)
self._itemDescriptions.append(itemDescription)

def addGrabbable(self, grabbable):


self._grabbables.append(grabbable)

def delGrabbable(self, item):


self._grabbables.remove(item)

def __str__(self) -> str:


s = ''
s+="You are in "+self.name + "\n"

s+="You see: \n"


for item in self.items:
s+=item
s+="\n\n"

s += "Exits: \n"
for exit in self.exits:
s += exit + " "
s += "\n"

return s

def createRooms():
r1 = Room("Dining Room")
r2 = Room("Living Room")
r3 = Room("Office")
r4 = Room("Brewery")
r5 = Room("Taylor Swift Room") # new room
r6 = Room("Resting Room") # new room
r7 = Room("Bathroom") # new room
r8 = Room("Vault Room") # new room
r9 = Room("Basement") # new room (a basement)

r1.addExit("east", r2)
r1.addExit("south",r3)
r1.addExit("stairs",r5) # exit to stairs to a second floor
r2.addExit("west",r1)
r2.addExit("south",r4)
r2.addExit("downstairs",r9) # exit down to the basment
r3.addExit("north",r1)
r3.addExit("east",r4)
r4.addExit("north",r2)
r4.addExit("west",r3)
r4.addExit("south",None)
r5.addExit("stairs",r1)
r5.addExit("east",r6)
r5.addExit("south",r7)
r6.addExit("west",r5)
r6.addExit("south",r8)
r7.addExit("north",r5)
r7.addExit("east",r8)
r8.addExit("west",r7)
r8.addExit("north",r6)
r9.addExit("upstairs",r2) # exit up from the basement

r1.addGrabbable("key")
r3.addGrabbable("book")
r4.addGrabbable("6-pack")
r5.addGrabbable("taylor_swift_action_figure")
# new grabbable
r6.addGrabbable("coins")
# new grabbable
r7.addGrabbable("soap")
# new grabbable

r1.addItem("chair", "It is made of wicker and no one is sitting on it.")


r1.addItem("table", "It is made of oak. A golden key rests on it.")
r2.addItem("rug", "It is nice and Indian. It also needs to be vacuumed.")
r2.addItem("fireplace", "It is full of ashes.")
r3.addItem("bookshelves", "They are empty. Go figure.")
r3.addItem("statue", "There is nothing special about it.")
r3.addItem("desk", "The statue is resting on it. So is a book.")
r4.addItem("brew_rig", "Gourd is brewing some sort of oatmeal stout on the brew
rig. A 6-pack is resting beside it.")
r5.addItem("taylor_swift_poster", "A poster of Taylor Swift")
# new item
r5.addItem("storage_bin", "Made of oak wood. Has a action figure of taylor
swift in it.") # new item
r6.addItem("couch", "Made with the best fabric. There are coins imbetween the
cushions.") # new item
r7.addItem("sink","Made of granite. Has bar of soap next to it.")
# new item
r7.addItem("toilet", "Just a plain white toilet.")
# new item
r7.addItem("bathtub", "A nice sized bathtub")
# new item
r8.addItem("vault", "No one can open it.")
# new item
r9.addItem("pile", "Just a pile of junk.")
# new item

currentRoom = r1

return currentRoom

def death():
print(" " * 17 + "u" * 7)
print(" " * 13 + "u" * 2 + "$" * 11 + "u" * 2)
print(" " * 10 + "u" * 2 + "$" * 17 + "u" * 2)
print(" " * 9 + "u" + "$" * 21 + "u")
print(" " * 8 + "u" + "$" * 23 + "u")
print(" " * 7 + "u" + "$" * 25 + "u")
print(" " * 7 + "u" + "$" * 25 + "u")
print(" " * 7 + "u" + "$" * 6 + "\"" + " " * 3 + "\"" + "$" * 3 + "\"" + " " *
3 + "\"" + "$" * 6 + "u")
print(" " * 7 + "\"" + "$" * 4 + "\"" + " " * 6 + "u$u" + " " * 7 + "$" * 4 +
"\"")
print(" " * 8 + "$" * 3 + "u" + " " * 7 + "u$u" + " " * 7 + "u" + "$" * 3)
print(" " * 8 + "$" * 3 + "u" + " " * 6 + "u" + "$" * 3 + "u" + " " * 6 + "u" +
"$" * 3)
print(" " * 9 + "\"" + "$" * 4 + "u" * 2 + "$" * 3 + " " * 3 + "$" * 3 + "u" *
2 + "$" * 4 + "\"")
print(" " * 10 + "\"" + "$" * 7 + "\"" + " " * 3 + "\"" + "$" * 7 + "\"")
print(" " * 12 + "u" + "$" * 7 + "u" + "$" * 7 + "u")
print(" " * 13 + "u$\"$\"$\"$\"$\"$\"$u")
print(" " * 2 + "u" * 3 + " " * 8 + "$" * 2 + "u$ $ $ $ $u" + "$" * 2 + " " * 7
+ "u" * 3)
print(" u" + "$" * 4 + " " * 8 + "$" * 5 + "u$u$u" + "$" * 3 + " " * 7 + "u" +
"$" * 4)
print(" " * 2 + "$" * 5 + "u" * 2 + " " * 6 + "\"" + "$" * 9 + "\"" + " " * 5 +
"u" * 2 + "$" * 6)
print("u" + "$" * 11 + "u" * 2 + " " * 4 + "\"" * 5 + " " * 4 + "u" * 4 + "$" *
10)
print("$" * 4 + "\"" * 3 + "$" * 10 + "u" * 3 + " " * 3 + "u" * 2 + "$" * 9 +
"\"" * 3 + "$" * 3 + "\"")
print(" " + "\"" * 3 + " " * 6 + "\"" * 2 + "$" * 11 + "u" * 2 + " " + "\"" * 2
+ "$" + "\"" * 3)
print(" " * 11 + "u" * 4 + " \"\"" + "$" * 10 + "u" * 3)
print(" " * 2 + "u" + "$" * 3 + "u" * 3 + "$" * 9 + "u" * 2 + " \"\"" + "$" *
11 + "u" * 3 + "$" * 3)
print(" " * 2 + "$" * 10 + "\"" * 4 + " " * 11 + "\"\"" + "$" * 11 + "\"")
print(" " * 3 + "\"" + "$" * 5 + "\"" + " " * 22 + "\"\"" + "$" * 4 + "\"\"")
print(" " * 5 + "$" * 3 + "\"" + " " * 25 + "$" * 4 + "\"")

def grababbleSearch(item:str, list:list) -> bool:


'''a basic sequential search function'''
i = 0
found = False

while i < len(list) and found == False:


if list[i] == item:
found = True
i += 1
return found

currentRoom = createRooms()
inventory = []

# core game loop


# go back and forth between
# printing the current room
#and receiving and executing input

while(True):
status = "{}\nYou are carrying: {}\n".format(currentRoom, inventory)

if (currentRoom == None):
death()
break

print("="*60)
print(status)

action = input("What to do? ")


action = action.lower()

if (action == "quit" or action == "exit" or action == "bye"):


break

response = "I don't understand. Try verb noun. Valid verbs are go, look, and
take"
words = action.split()

if (len(words) == 2):
verb = words[0]
noun = words[1]
if (verb == "go"):
response = "Invalid exit."
for i in range(len(currentRoom.exits)):
if (noun == currentRoom.exits[i]):
currentRoom = currentRoom.exitLocation[i]
response = "Room changed."
break

elif (verb == "look"):


response = "I don't see that item"
for i in range(len(currentRoom.items)):
if (currentRoom.items[i] == 'table' and grababbleSearch('key',
currentRoom.grabbables) == False): # Start of new
addition
currentRoom.itemDescriptions[i] = "It is made of oak. A golden
key is missing" #

#
if (currentRoom.items[i] == 'brew_rig' and grababbleSearch('6-
pack', currentRoom.grabbables) == False): # These if
statements
currentRoom.itemDescriptions[i] = "Gourd is brewing some sort
of oatmeal stout on the brew rig. A 6-pack is gone." # check to see if

# the item you are looking at


if (currentRoom.items[i] == 'desk' and grababbleSearch('book',
currentRoom.grabbables) == False): # need's its
description changed
currentRoom.itemDescriptions[i] = "The statue is resting on it.
A book has been taken." # based on whether
or not the

# grabbable in its description


if (currentRoom.items[i] == 'storage_bin' and
grababbleSearch('taylor_swift_action_figure', currentRoom.grabbables) == False):
# is missing from the
currentRoom.itemDescriptions[i] = "Made of oak wood. The action
figure of taylor swift is gone." # grabbables list.

#
if (currentRoom.items[i] == 'couch' and grababbleSearch('coins',
currentRoom.grabbables) == False): #
currentRoom.itemDescriptions[i] = "Made with the best fabric.
Somebody found the coins." #

#
if (currentRoom.items[i] == 'sink' and grababbleSearch('soap',
currentRoom.grabbables) == False): #
currentRoom.itemDescriptions[i] = "Made of granite. The soap
has been picked up." # end of new
addition

if (noun == currentRoom.items[i]):
response = currentRoom.itemDescriptions[i]
break

elif (verb == "take"):


response = "I don't see that item."
for grabbable in currentRoom.grabbables:
if (noun == grabbable):
inventory.append(grabbable)
currentRoom.delGrabbable(grabbable)
response = "Item grabbed."
break

print("\n{}".format(response))

You might also like