I am having a very bad time with this exercise. So far, all I can manage is more or less an imitation of Exercise 43. Mine is a generic castle adventure, complete with items, trap doors, hidden passageways (to be triggered by a cheesy hidden book in the library, naturally), etc.
What I’d like to do is, use an Engine() class to keep track of/update some things that I can then display at every user interaction. For example, current room, inventory, and (eventually) player health. Engine() runs over and over until the user either wins, or dies. A lot like Ex. 43.
The problem is: I have no idea how to pass values in and out of functions/classes and/or call them properly, apparently.
So far here’s my code:
# Welcome to Generic Castle Adventure!
# details to be implemented later
from trapdoor import TrapDoor
# we will use the below for error-handling
from sys import exit
# and generating random numbers, for combat/puzzles etc.
from random import randint
from time import sleep
health = 100
inventory = 'Empty'
condition = 'headache'
start_room = 'broom_closet'
class Room(object):
def enter(self):
print "TBD, come back later"
exit(1)
class BroomCloset(Room):
def room(self):
print "You wake up in a puddle of your own sick."
print "Your head is pounding.\n"
sleep(1)
print "The last thing you remember is, mistakenly stumbling"
print "in here at the feast last night, thinking it was the garderobe.\n"
sleep(1)
print "You're not sure what else happened, but you're"
print "alone now. It smells like urine AND vomit in here, yuck.\n"
sleep(1)
print "You are in a broom closet just off the"
print "Great Hall of a generic European style castle.\n"
sleep(2)
print "There are 2 things here: %s and %s\n" % ('dusty shelf', 'old broom')
sleep(1)
print "The door back into the Great Hall is cracked open."
print "You can see that the broom closet is in a small alcove, under"
print "the stairs to the upper floor of the castle.\n"
sleep(1)
print "Choose an action:"
print "[open door, take broom, die]"
action = raw_input("> ")
if action == 'open door':
nextroom = GreatHall()
nextroom.room()
return 'great_hall'
# currentroom = '
elif action == 'take broom':
nextroom = GreatHall()
inventory = 'old broom'
print "You take the also-dusty, well-worn broom."
# return inventory
return 'great_hall'
elif action == 'die':
nextroom = Death()
nextroom.room()
return 'death'
else:
print "What are you doing dude?"
return 'broom_closet'
class UpperHallway(Room):
#trapdoor_result = TrapDoor().trap_opens()
def creaking_floorboards(self):
print "As you walk down the drafty hallway,"
print "you hear the floorboards creak."
class ThroneRoom(Room):
def room(self):
print "The ornate throne room lies before you.\n"
sleep(1)
print "This space for rent"
return 'throne_room'
pass
class Dungeon(Room):
pass
class HiddenRoom(Room):
pass
class LibraryRoom(Room):
pass
class GreatHall(Room):
def room(self):
# print "You are in the Great Hall of the castle.\n"
# sleep(2)
print "Description of Great Hall:"
print "The main space is filled with several"
print "long feasting tables, littered with debris from last night's"
print "festivities.\n"
sleep(1)
print "You find it odd that no one has cleaned up the mess. In fact,"
print "you hear no signs of normal life - the castle seems empty so far.\n"
sleep(1)
print "You see 2 possible ways to go:"
print "There is a stairway leading to the private quarters."
print "There is a grand archway leading to the throne room.\n"
sleep(1)
print "Which way will you go?"
print "[stairway, throne room, stay here]"
action = raw_input("> ")
sleep(2)
if action == 'stairway':
return 'stairway'
elif action == 'throne room':
return 'throne_room'
elif action == 'stay here':
return 'great_hall'
else:
print "Unexpected condition encountered, exiting"
print "Class GreatHall(), function room()"
exit(1)
pass
class Bedchamber(Room):
pass
# class Garderobe(Room)
# To be implemented later - optional
class Stairway(Room):
def room(self):
print "You follow a winding stairway to the upper hallway."
return 'stairway_landing'
pass
class StairwayLanding(Room):
pass
class Death(Room):
def room(self):
print "You died."
exit(0)
class Map(object):
rooms = {
'broom_closet': BroomCloset(),
'upper_hallway': UpperHallway(),
'throne_room': ThroneRoom(),
'dungeon': Dungeon(),
'hidden_room': HiddenRoom(),
'library_room': LibraryRoom(),
'great_hall': GreatHall(),
'bedchamber': Bedchamber(),
# 'garderobe': Garderobe(),
'stairway': Stairway(),
'stairway_landing': StairwayLanding(),
'death': Death()
}
# this will contain list of all possible rooms
def __init__(self, start_room):
self.start_room = start_room
def next_room(self, room_name):
value = Map.rooms.get(room_name)
return value
def opening_room(self):
return self.next_room(self.start_room)
class Engine(object):
def __init__(self, room_map):
self.room_map = room_map
def play(self):
#global inventory
current_room = self.room_map.opening_room()
# last_room = self.room_map.next_room('finished')
last_room = self.room_map.next_room('death')
while current_room != last_room:
next_room_name = current_room.room()
current_room = self.room_map.next_room(next_room_name)
print "You are in the %s\n" % str(current_room_name)
print "You have these items: %s\n" % inventory
if current_room == last_room:
print "Last room condition reached, this is only for debug."
else:
print "Unexpected condition, class Engine(), function play()."
print "Exiting."
exit(1)
current_room.room()
the_map = Map('broom_closet')
# print(the_map.__dict__)
# above line only for debugging
the_game = Engine(the_map)
the_game.play()
It doesn’t work, of course. For instance, here’s what happens if I try to take the broom while still in the broom closet:
> take broom
You take the also-dusty, well-worn broom.
You are in the <__main__.GreatHall object at 0x000000000295EF88>
You have these items: Empty
Description of Great Hall:
The main space is filled with several
long feasting tables, littered with debris from last night's
festivities.
You find it odd that no one has cleaned up the mess. In fact,
you hear no signs of normal life - the castle seems empty so far.
You see 2 possible ways to go:
There is a stairway leading to the private quarters.
There is a grand archway leading to the throne room.
Which way will you go?
[stairway, throne room, stay here]
>
See, inventory is still “empty.” And I don’t understand why that weird code prints (<main.GreatHall etc>) rather than simply saying “You are in the Great Hall.”
I have no idea how I would update the inventory, or exactly where the value of “inventory” is being stored so I could change it.
Help!