我在这里写一个小游戏。我已经在.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()
}
首先,您应该将Scene
类移至其他文件/包。
要在您中使用它,您module_intro
必须通过以下方式将其导入:
from package_name import Scene
或者:
import package_name
(但随后将Scene用作package_name.Scene
)
将Scene
实施保存在主项目文件中将创建一个循环引用(两个模块互相导入)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句