我在写一个小游戏。我在.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从随机进口随机
进口module_intro module_house_one进口module_package_zoom进口module_house_two进口module_unboxing进口module_key_letter进口module_letter_key进口module_house_three进口module_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()
}发布于 2016-11-11 12:33:21
首先,您应该将Scene类移动到其他文件/包中。
要在module_intro中使用它,您必须通过以下方法导入它:
from package_name import Scene或者:
import package_name(然后使用场景作为package_name.Scene)
将Scene实现保持在主项目文件中将使leed创建一个循环引用(两个模块相互导入)
https://stackoverflow.com/questions/40547894
复制相似问题