我目前正在尝试用纯 python 编写基于文本的冒险。因此,我有一个 Room 类,看起来有点像这样(缩短):
class Room1(Room):
def __init__(self):
Room.__init__(self)
self.init_objects()
self.description = """Looks like a living room. There's a lamp, a
cupboard, and
a door to your right. On the floor, there's a carpet."""
self.door_state = 1
self.carpet_state = 0
self.images = [(text-based images)]
def init_objects(self):
self.objects = {"door" : Door1(),
"frontdoor" : FrontDoor(),
"carpet" : Carpet(),
"lamp" : Lamp1(),
"clock" : Clock(),
"escritoire" : Escritoire(),
"booklet" : Booklet(),
"screws" : Screws(),
"trapdoor" : TrapDoor()}
def update_image(self):
IMG_MAPPER = {(0, 0, 0) : 0,
(0, 0, 1) : 1,
(1, 0, 0) : 2,
(1, 0, 1) : 3,
(1, 1, 1) : 4,
(1, 1, 0) : 5,
(0, 1, 1) : 6,
(0, 1, 0) : 7}
key = (self.objects["door"].state, self.objects["carpet"].state)
self.img = img_mapper[key]
我的问题是 Room 的 update_image() 方法。我需要一个映射器存储在那里,以根据对象的状态(打开/关闭)找出正确的图像,如果我把这个映射器放在方法的开头,每次方法都是由 python 读取和构造这个字典叫,对吗?那么我是否应该将此映射器字典存储为实例变量,如 self.img_mapper_dict = {(0, 0, 0) : 0, ...}?
也许有人对此有任何想法?