Skip to content Skip to sidebar Skip to footer

Learn Python The Hard Way Exercise 43

I do not understand how the script gets the next room, and generally how the 'Engine' and 'Map' classes work. Here's an excerpt: Class Map(object): scenes = { 'centra

Solution 1:

The Map object is a Class which maps your scenes. It has some scenes saved in an array.

scenes = {
    'central_corridor': CentralCorridor(),
    'laser_weapon_armory': LaserWeaponArmory(),
    'the_bridge': TheBridge(),
    'escape_pod': EscapePod(),
    'death': Death()
}

When an object of Map is made you also give it an opening scene as seen in the constructor

def__init__(self, start_scene):
    self.start_scene = start_scene

This creates a variable in Map called start_scene containing your opening scene.

Furthermore Map has 2 methods

# This one returns a scene based on its name or key in the scenes arraydefnext_scene(self, scene_name):
    return Map.scenes.get(scene_name)


# And this one  returns the opening scene which is set when you create the map.defopening_scene(self):
    returnself.next_scene(self.start_scene)

The Engine seems to be controlling the scenes when to play and what to play.

# When creating an Engine object you give the map containing scenes to its constructordef__init__(self, scene_map):
        self.scene_map = scene_map

# The method which starts playing the scenesdefplay(self):

    # the opening scene from the map is selected as the current scene
    current_scene = self.scene_map.opening_scene()

     # You loop all the scenes probably, conditions of this loop are unknown because you haven't posted it entirely.whileTrue:
         print"\n--------"# It seems the next scene name is known in the current scene
         next_scene_name = current_scene.enter()

         # It replaces current scene with the next scene from the map
         current_scene = self.scene_map.next_scene(next_scene_name)

why is it required to have these two classes anyway?

It isn't, unless it's required according to your assignment

Like you said it's possible to do it without, BUT there are good reasons to do so.

This way you make 2 separate classes with their own responsibilities. This way code is more readable when the application gets bigger and bigger. And it's easy to navigate through the application. You can easily change parts of your app etc etc. My advice is to keep practicing and reading about OOP, you will notice why you do the things you see.

Solution 2:

The Map class has a dictionary of scene names that are keys, and instances of different classes as the values. To retrieve one of the instances, you call a method passing in a string that is the scene name. The Map class then returns an instance of the corresponding class.

Couldn't you just do it with class methods instead (i.e. methods without self as a param.)? Then you could just call, for example, CentralCorridor.enter().

Yes, you could. The downside is that now you have hard coded the names of your scene classes in your code. If you later decide to rewrite your program to remove some scenes or add other scenes, you have to change all the places in your code where you hard coded the names. If you employ the Map class, then changes only have to be made to the dictionary. Although, you would still have to eliminate method calls that try to retrieve class instances that you've removed. Or, you could have the next_scene() method deal with attempts to retrieve scenes you've removed from the dictionary.

Another consideration with class methods is: will you need to store 'state'? Like, how many players are currently in the scene. And, will you need to create more than one scene of that type?

Post a Comment for "Learn Python The Hard Way Exercise 43"