文本冒险游戏:如何让数据结构中变量随重定义自动更新?
解决文本冒险游戏中地点引用的自动更新问题
方案1:利用字典的可变性手动更新引用
namedtuple的实例本身不可变,但它的dirloc是可变字典,可以先创建所有地点,再手动修正字典里的指向:
from collections import namedtuple Location = namedtuple('Location', ['desc', 'ldesc', 'func', 'dirloc']) # 先创建所有地点,dirloc暂时用None占位 entrance = Location( 'Dungeon Entrance', 'You are in a small clearing in a forest. To the north, a large iron-studded door is visible, embedded in a small hill that rises up in the middle of the clearing. The sun shines down upon you and birds sing.', None, {'north': None, 'south': None, 'east': None, 'west': None, 'up': None, 'down': None} ) foyer = Location( 'Foyer', 'You are in a medium-sized room made of stone. Alcoves are carved along the wall at all angles. One passage leads west. Another leads north. Footsteps on the ground lead towards the west passage.', None, {'north': None, 'south': None, 'east': None, 'west': None, 'up': None, 'down': None} ) # 手动更新双向引用 entrance.dirloc['north'] = foyer foyer.dirloc['south'] = entrance
这个方法简单直接,适合小型游戏的地点定义,缺点是地点较多时需要手动维护所有连接,容易遗漏。
方案2:改用可变类替代namedtuple
如果namedtuple的不可变性限制了扩展,可以定义一个普通的Location类,属性完全可变,方便后续修改连接:
class Location: def __init__(self, desc, ldesc, func=None, dirloc=None): self.desc = desc self.ldesc = ldesc self.func = func self.dirloc = dirloc or {'north': None, 'south': None, 'east': None, 'west': None, 'up': None, 'down': None} # 先创建实例 entrance = Location( 'Dungeon Entrance', 'You are in a small clearing in a forest. To the north, a large iron-studded door is visible, embedded in a small hill that rises up in the middle of the clearing. The sun shines down upon you and birds sing.' ) foyer = Location( 'Foyer', 'You are in a medium-sized room made of stone. Alcoves are carved along the wall at all angles. One passage leads west. Another leads north. Footsteps on the ground lead towards the west passage.' ) # 设置双向连接 entrance.dirloc['north'] = foyer foyer.dirloc['south'] = entrance
这种方式灵活性更高,后续修改地点属性或连接时直接赋值即可,适合复杂游戏的扩展需求。
方案3:使用字符串延迟引用(适合大量地点)
如果游戏地点数量多,可以用地点名称字符串代替直接引用,维护一个全局地点字典,在需要移动时再解析字符串获取对应地点:
from collections import namedtuple Location = namedtuple('Location', ['desc', 'ldesc', 'func', 'dirloc']) # 全局地点字典,存储所有地点实例 locations = {} # 定义地点时,dirloc存目标地点的名称字符串 entrance = Location( 'Dungeon Entrance', 'You are in a small clearing in a forest. To the north, a large iron-studded door is visible, embedded in a small hill that rises up in the middle of the clearing. The sun shines down upon you and birds sing.', None, {'north': 'foyer', 'south': None, 'east': None, 'west': None, 'up': None, 'down': None} ) locations['entrance'] = entrance foyer = Location( 'Foyer', 'You are in a medium-sized room made of stone. Alcoves are carved along the wall at all angles. One passage leads west. Another leads north. Footsteps on the ground lead towards the west passage.', None, {'north': None, 'south': 'entrance', 'east': None, 'west': None, 'up': None, 'down': None} ) locations['foyer'] = foyer # 处理移动的函数,解析字符串引用 def move(current_loc, direction): dest_name = current_loc.dirloc.get(direction) if not dest_name: return None, 'You can\'t go that way!' return locations.get(dest_name), f'You move {direction} to the {dest_name}.'
这个方法的优势是定义地点时无需考虑顺序,所有地点可独立定义,最后通过字典统一管理,适合大型游戏的地点架构。
内容的提问来源于stack exchange,提问作者Odoul
相关产品推荐
相关产品推荐

