LPTHW Ex.43更改类初始参数

时间:2015-03-26 01:54:14

标签: python function class methods

我试图克服这个练习的最后一个障碍,我无法弄清楚!我无法理解类Engine中的play()方法。特别是" next_screen_name"。

查看以下代码:

class Engine(object):

def __init__(self, scene_map):
    self.scene_map = scene_map

def play(self):
    current_scene = self.scene_map.opening_scene()
    last_scene = self.scene_map.next_scene('finished')

    while current_scene != last_scene:
        next_scene_name = current_scene.enter()
        current_scene = self.scene_map.next_scene(next_scene_name)

    # be sure to print out the last scene
    current_scene.enter()

class Map(object):

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

def __init__(self, start_scene):
    self.start_scene = start_scene

def next_scene(self, scene_name):
    val = Map.scenes.get(scene_name)
    return val

def opening_scene(self):
    return self.next_scene(self.start_scene)

a_map = Map('central_corridor')
a_game = Engine(a_map)
a_game.play()

如果你运行上面的代码和" current_scene.enter()"回归'死亡'来自enter()方法。代码将继续并返回下一个场景,即Death()。

但是,我不明白的是,如果你最初设置了#a; game"初始参数为" a_map"这将是' central_corridor&#39 ;;初始" scene_map"改为"死亡" ??

此处链接到完整代码:

http://learnpythonthehardway.org/book/ex43.html

先谢谢大家。

1 个答案:

答案 0 :(得分:0)

我正在经历LPTHW,过去几天一直在关注这个游戏,试图了解正在发生的事情。对于OOP来说,我显然不是很有经验,所以要把这一切都拿出来。

我发现最难掌握的是play()方法中使用的scene_map只是Map类的一个实例,它接受一个变量aMap,它是一个Map对象。

a_map = Map('central_corridor')
a_game = Engine(a_map)
a_game.play()

以下对代码的评论试图显示在执行上述代码时开始执行的步骤。

class Engine(object):

    # Step 3 - This requires a variable with a Map object to be passed in.
    # Each Engine 'has-a' Map to navigate the scenes. this is passed into 
    # the engine class through an object scene_map 
    def __init__(self, scene_map)
        self.scene_map = scene_map

    # Step 4 - Once an instance of the map and scenese has been initialized
    # the play() method of the Engine class can be called to start the game.
    # First the current_scene attribute is created by running the 
    # opening_opening scene method from the Map class. This will be used 
    # once we enter the while loop to start the game. the last_scene attribute
    # is also set at this point to be used in the while loop so the program
    # knows when you have finished or won the game.
    def play(self):
        current_scene = self.scene_map.opening_scene()
        last_scene = self.scene_map.next_scene('finished')

        # The while loop runs while the game is in play. In the first
        # iteration the current_scene attribute is set above with the 
        # start scene. After that the current_scene is re-set to the 
        # return value at the end of every scene object or 'death' when
        # you die which takes you to the Death object.
        while current_scene != last_scene:
            next_scene_name = current_scene.enter()
            current_scene = self.scene_map.next_scene(next_scene_name)

        # be sure to print out the last scene
        current_scene.enter() 

class Map(object):
    # sete scenes dictionary property for Map class
    scenes ='central_corridor': CentralCorridor(),
        'laser_weapon_armory': LaserWeaponArmory(),
        'the_bridge': TheBridge(),
        'death': Death(),
        'finished': Finished(), 
    }

    # Step 1
    # Each Map 'has-a' scene(s) The scene objects are stored in the scenes dict
    # The start scene passed in as a string will corrispond to an object 
    # by going through the scenes dict. This will be done in the 
    # next_scene method of the Map class
    def __init__ (self, start_scene):
        self.start_scene = start_scene

    # Step 2a
    # Looks up the scene string in dict(scenes) and returns the 
    # object for that scene.
    # This will be passed into the Engine Class to move to the next scene
    # through the val attribute
    def next_scene(self, scene_name):
        val = Map.scenes.get(scene_name)

    # Step 2b
    # This is a special instance to look up the starting scene based on 
    # the string 'start_scene' passed through the __init__ Metho to start
    # the game. 
    # This will be passed into the Engine to start the game
    def opening_scene(self):
        return self.next_scene(self.start_scene)

询问有关它如何更改Death()对象的问题。一旦你死在任何你死的场景中它就会回归“死亡”。解释如下:

current_scene = self.scene_map.next_scene(next_scene_name)

我希望这会有所帮助。尝试解释它对我来说是一个很好的练习。