为什么模块在引擎代码中看不到类?

金属

我在这里写一个小游戏。我已经在.py文件中描述了游戏场景,然后将它们作为模块导入,以使代码更简洁。我还尝试将Scene定义放入一个单独的.py文件中,并将其作为模块导入到这些模块中,甚至将其与引擎代码中的模块一起导入。错误是一样的:

Traceback (most recent call last):
  File "engine_chapter_1.py", line 4, i
    import module_intro
  File "C:\Users\r.hrytskiv\Documents\P
    class Intro(Scene):
NameError: name 'Scene' is not defined

这是引擎代码本身:

from sys import exit
from random import randint

import module_intro
import module_house_one
import module_package_zoom
import module_house_two
import module_unboxing
import module_key_letter
import module_letter_key
import module_house_three

class Scene(object):

    def enter(self):
        print "Subclass and implement"
        exit(1)

class EndGame(Scene):

    cast = [
            "Roman Hrytskiv"
            "Dmytro Litvyn"
    ]

    def enter(self):
        print EndGame.cast
        exit(1)

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)

        current_scene.enter()

class Map(object):

    scenes = {
    'intro': Intro(),
    'house_one': HouseOne(),
    'package_zoom': PackageZoom(),
    'house_two': HouseTwo(),
    'unboxing': Unboxing(),
    'key_letter': KeyLetter(),
    'letter_key': LetterKey(),
    'house_three': HouseThree(),
    'end_game': EndGame()
    }

    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('intro')
    a_game = Engine(a_map)
    a_game.play()

和模块之一:

class Intro(Scene):

def enter(self):
    print "City of Bend, OR. Morning. Nowadays."
    print "City panorama, people on the streets, around noon."
    print "Roland is the hero of this story. He is 27 years old guy."
    print "He has recently left University. He's always looking for"
    print "something interesting and fun. Doesn't like to sit in one place."
    print "For now he's unemployed and struggling to pay for rent so"
    print "he decided to sell some stuff from his house. He went to the"
    print "attic to check if there is something he might sell."
    print "He found some antique stuff and went to pawn shop..."
    return 'house_one'

更新1

新引擎代码:

from sys import exit

来自随机进口randint

导入模块_intro导入模块_house_one导入模块_package_zoom导入模块_house_two导入模块_取消装箱导入模块_key_letter导入模块_letter_key导入模块_house_three导入模块_end_game

引擎(对象)类:

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)

    current_scene.enter()

Map(对象)类:

scenes = {
'intro': Intro(),
'house_one': HouseOne(),
'package_zoom': PackageZoom(),
'house_two': HouseTwo(),
'unboxing': Unboxing(),
'key_letter': KeyLetter(),
'letter_key': LetterKey(),
'house_three': HouseThree(),
'end_game': EndGame()
}

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('intro')
a_game = Engine(a_map)
a_game.play()

新模块代码:

import module_scene

类Intro(module_scene.Scene):

def enter(self):
    print "City of Bend, OR. Morning. Nowadays."
    print "City panorama, people on the streets, around noon."
    print "Roland is the hero of this story. He is 27 years old guy."
    print "He has recently left University. He's always looking for"
    print "something interesting and fun. Doesn't like to sit in one place."
    print "For now he's unemployed and struggling to pay for rent so"
    print "he decided to sell some stuff from his house. He went to the"
    print "attic to check if there is something he might sell."
    print "He found some antique stuff and went to pawn shop..."
    return 'house_one'

解决方案:只需在package_name中引用它们即可。

scenes = {
'intro': module_intro.Intro(),
'house_one': module_house_one.HouseOne(),
'package_zoom': module_package_zoom.PackageZoom(),
'house_two': module_house_two.HouseTwo(),
'unboxing': module_unboxing.Unboxing(),
'key_letter': module_key_letter.KeyLetter(),
'letter_key': module_letter_key.LetterKey(),
'house_three': module_house_three.HouseThree(),
'end_game': module_end_game.EndGame()
}
woockashek

首先,您应该将Scene类移至其他文件/包。

要在您中使用它,您module_intro必须通过以下方式将其导入:

 from package_name import Scene

或者:

import package_name

(但随后将Scene用作package_name.Scene

Scene实施保存在主项目文件中将创建一个循环引用(两个模块互相导入)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么我的GridView对象在代码中看不到?

为什么在C#中看不到Ping类?

为什么我的Objective C模块在Xcode 8中看不到我的Swift 3类?

为什么我在全局节点模块列表中看不到 Node 和 Yarn?

为什么我在 R 中的函数中看不到函数的源代码?

为什么在基类构造函数中看不到派生类属性值?

为什么我在 Delegate 类中看不到 BeginInvoke 和 EndInvoke 方法的存在?

为什么在TFS中看不到分支树?

为什么在SSMS结果列表中看不到全名?

为什么在图形绘制中看不到弯曲的边缘?

为什么在 QGraphicsScene 中看不到 QGraphicsWidget 的选择边框?

为什么在PowerShell中看不到脚本的参数?

为什么在UIScrollView中看不到UIStackView?

为什么我在gdb backtrace中看不到行号?

为什么在Swift中看不到图像?

为什么在Nautilus中看不到缩略图?

为什么在cloudboost中看不到我的表?

在PHP中看不到Python模块

gcc编译错误:在嵌套的朋友类中看不到模板类Table中的嵌套类A的成员。为什么?

在Android中看不到ViewModel类

为什么我在文本编辑器中看不到输出,即使我确定代码是正确的?

为什么我的C ++代码看不到这个C#类成员?

为什么 Python 看不到现有模块

为什么 awx 看不到 pip 模块?

从模块中看不到的其他模块

在Android应用程序中看不到导入的依赖模块类LibGDX

为什么trap命令看不到退出代码?

为什么子类看不到父类的方法?

React 组件看不到辅助类。为什么?