I recently did the lpthw tutorial. What Zed does is create a third class that maps out the levels in the game, that way they can be instantiated after the rest of the classes are defined. That should fix the 'not defined' error.
But another issue is you are creating an engine object at the end:
game = Engine()
Then when you initialize the MainCorridor class you create a separate engine object:
class MainCorridor(object):
def __init__(self):
self.engine = Engine()
A solution is to create the engine class in the MainCorridor class only:
class Engine(object):
def test(self):
print "Calling Engine() from MainCorridor()"
class MainCorridor(object):
def __init__(self):
self.engine = Engine()
def start(self):
self.engine.test()
class Map(object):
location_dict = {
'main_corridor': MainCorridor(),
}
def goto(self, location):
map = self.get_location(location)
return map.start()
def get_location(self, location):
return self.location_dict[location]
game = Map()
game.goto('main_corridor')
But this way, each location instance you put in the location_dict will have its own unique Engine object.
What you could do to give every location the single Engine object is pass 'self' from engine into the map class, and have the map class instantiate location_dict by passing the same engine object to each location in the list:
class Engine(object):
def __init__(self):
self.map = Map(self)
def test(self):
print "Calling Engine() from MainCorridor()"
class MainCorridor(object):
def __init__(self, engine):
self.engine = engine
def start(self):
self.engine.test()
class Map(object):
def __init__(self, engine):
self.location_dict = {
'main_corridor': MainCorridor(engine),
}
def goto(self, location):
map = self.get_location(location)
return map.start()
def get_location(self, location):
return self.location_dict[location]
game = Engine()
game.map.goto('main_corridor')