【发布时间】:2017-03-25 16:17:47
【问题描述】:
我在这里写一个小游戏。我已经在 .py 文件中描述了游戏场景,以将它们作为模块导入,以使代码更清晰。 我还尝试将场景定义放入单独的 .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
导入模块介绍 导入模块_house_one 导入 module_package_zoom 导入模块_house_two 导入模块_拆箱 导入 module_key_letter 导入模块字母键 导入 module_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()
类映射(对象):
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
类介绍(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()
}
【问题讨论】:
-
您是否将 Scene 导入到定义 Intro 的模块中?
-
@DanielRoseman 是的,我也尝试过这样做。没有结果
-
从错误中我们看到问题是模块导入不知道场景是什么,所以我决定做的第一件事就是通过将场景导入其中来告诉他这是什么。但正如我所说,它并没有解决问题
-
啊,你现在有一个循环导入,因为主模块正在导入 module_scene。那是行不通的。
标签: python class oop import module