我是否错误地导入了我的模块?

Am I incorrectly importing my module?

我一直在研究 Learn Python the Hard Way,目前正处于您必须编写自己的基于文本的游戏代码的部分。我从作者提供的游戏中获取代码 "abstracted" ,直到我得到一种可以导入到我自己的游戏文件中的模板。我想使用模板模块中的 classes 和方法,并简单地提供特定于我的游戏的详细信息。这样,我就不必重写模板中的所有内容。我使用的模板如下所示:

""" This module provides some classes for constructing an interactive,
text-based game. You should ideally subclass what's provided here, and
override methods or provide new methods as you see fit.
"""
from sys import exit
from random import randint

# TODO: keep Scene class and Map class here, move everything else outside and just import (?)
# TODO: move dialogs to text files (?)
# TODO: remove 'random' import, 'sys' probably needs to stay

class Scene(object):
    """ A basic class describing a scene in your game. You should subclass this and implement your own version of enter(). """
    def enter(self):
        """ Play out the scene and return a string key referring to the next scene."""
        print "This scene is not yet configured. Subclass it and implement enter()."
        exit(1)

class Engine(object):
    """ The main engine that runs the actual game. This is where scenes are
    stepped through. You could add a system to change the starting point of
    your game depending on a save state, etc.
    """

    # initialize the engine with a map of scenes
    def __init__(self, scene_map):
        """ Initialize the engine with a map of all scenes in your game. """
        self.scene_map = scene_map

    def play(self):
        """ Start the game with the map set at the opening scene.
        Also grab the final scene of your game. Continue through the game
        as long as you aren't at the final scene. Then, make sure you
        actually play the final scene.
        """
        # current_scene is the results of scene_map's opening_scene()
        current_scene = self.scene_map.opening_scene()

        # last_scene is grabbing the next scene, with param of 'finished'
        last_scene = self.scene_map.next_scene('scene')

        # while you're not on the final scene/room
        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()


# TODO: abstract this class
class Map(object):
    """ Essentially holds all the scenes for your game and provides methods for grabbing the opening scene and next scene.
    Subclass this and provide your own scenes.
    """
    #scenes = {"scene": Scene()}

    def __init__(self, start_scene, scenes={"scene": Scene()}):
        # set the dictionary of scenes
        self.scenes = scenes
        """ Initialize the map with whatever your starting scene is. """
        self.start_scene = start_scene

    def next_scene(self, scene_name):
        """ Get the value of scene_name and return it. """
        val = self.scenes.get(scene_name)
        return val

    def opening_scene(self):
        """ Return the scene your game will start with. """
        return self.next_scene(self.start_scene)

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

我自己的游戏文件是这样的:

from sys import exit
from random import randint
from game_template import *

scenes = {
        "RudeWakeUp": RudeWakeUp(),
        "Fridge": Fridge(),
        "PlugFix": PlugFix(),
        "PlumbingFix": PlumbingFix(),
        "FridgeRelocation": FridgeRelocation(),
        "Final": Final(),
        "Fail": Fail()
        }
class Fail(Scene):
    def enter(self):
        print "Well, it looks like the fridge is staying right where it is."
        exit(1)

class RudeWakeUp(Scene):
    pass

class Fridge(Scene):
    pass

class PlugFix(Scene):
    pass

class PlumbingFix(Scene):
    pass

class FridgeRelocation(Scene):
    pass

class Final(Scene):
    pass

# TEST AREA
game_map = Map('Fail', scenes)
game = Engine(game_map)
game.play()

我目前只在测试 Fail()。问题是当我执行我的游戏文件时,我从模板中的 Scene() 基 class 而不是 Fail() 获得输出。这是因为我导入不正确、继承不正确还是其他原因?我以前在 python 中做过编程,但是我已经有一段时间没有用它做出任何重要的事情了,我可能已经忘记了一些关于 class 是如何工作的事情。如有任何帮助,我们将不胜感激。

很久以前,您可能已经了解到python是一种声明式语言,代码是逐行执行的。

class Fail(Scene):
    def enter(self):
        print "Well, it looks like the fridge is staying right where it is."
        exit(1)
scenes = {
    "RudeWakeUp": RudeWakeUp(),
    "Fridge": Fridge(),
    "PlugFix": PlugFix(),
    "PlumbingFix": PlumbingFix(),
    "FridgeRelocation": FridgeRelocation(),
    "Final": Final(),
    "Fail": Fail()
    }

只需更改顺序,以便在分配 class 之前声明 Fail 即可解决问题。事实上,您可以在声明所有其他 class 之后设置 scenes