如何解决Python文字游戏中无法离开初始房间的问题?
文字游戏无法移动问题修复方案
问题简述
为Python课程制作的文字游戏,玩家需在房间间移动收集物品,集齐后击败反派获胜。当前代码可运行,但输入初始房间的北向指令"N"后,无法离开初始房间。
问题根源
- 大小写不匹配:房间出口的方向键定义为大写(如
"N"),但用户输入的命令被强制转为小写(command = input(...).strip().lower()),导致小写的"n"无法匹配字典中的大写键,移动逻辑未触发。 - 缺失方法调用:拾取物品逻辑中调用了
current_room.has_item(item_name),但Room类未定义该方法,会触发属性错误。
修复方案
修复步骤
- 统一命令大小写判断:将移动逻辑的判断改为忽略大小写,无需修改已定义的出口键。
- 为Room类添加has_item方法:实现房间内物品的查找逻辑。
修复后的完整代码
class Room: def __init__(self, name, description): self.name = name self.description = description self.exits = {} self.items = [] def add_exit(self, direction, room): self.exits[direction] = room def add_item(self, item): self.items.append(item) def remove_item(self, item): self.items.remove(item) # 添加缺失的has_item方法 def has_item(self, item_name): return any(item.name.lower() == item_name.lower() for item in self.items) def __str__(self): return f"{self.name}\n\n{self.description}" class Item: def __init__(self, name): self.name = name def __str__(self): return self.name class Player: def __init__(self): self.inventory = [] def add_item(self, item): self.inventory.append(item) def has_item(self, item_name): return any(item.name.lower() == item_name.lower() for item in self.inventory) # 创建房间 start_room = Room("起始房间", "你身处一段危险旅程的起点。") enchanted_forest = Room("魔法森林", "你被神秘树木和微弱低语包围。") ancient_library = Room("古老图书馆", "书架上堆满了布满灰尘的典籍与古老知识。") underground_caverns = Room("地下洞穴", "钟乳石和石笋构成了一片阴影迷宫。") tower_of_wisdom = Room("智慧之塔", "高耸的建筑里满是古老文物与奥术符号。") forgotten_crypt = Room("被遗忘的墓穴", "空气中弥漫着腐朽与被遗忘灵魂的气息。") labyrinth_of_shadows = Room("阴影迷宫", "扭曲的迷宫中,黑暗隐藏着未知危险。") chamber_of_the_balrog = Room("炎魔密室", "地面震颤,一个炽热的存在正在等待。") # 连接房间 start_room.add_exit("N", enchanted_forest) enchanted_forest.add_exit("S", start_room) enchanted_forest.add_exit("E", ancient_library) ancient_library.add_exit("W", enchanted_forest) ancient_library.add_exit("S", underground_caverns) underground_caverns.add_exit("N", ancient_library) underground_caverns.add_exit("E", tower_of_wisdom) tower_of_wisdom.add_exit("W", underground_caverns) tower_of_wisdom.add_exit("E", forgotten_crypt) forgotten_crypt.add_exit("W", tower_of_wisdom) forgotten_crypt.add_exit("S", labyrinth_of_shadows) labyrinth_of_shadows.add_exit("N", forgotten_crypt) labyrinth_of_shadows.add_exit("E", chamber_of_the_balrog) chamber_of_the_balrog.add_exit("W", labyrinth_of_shadows) # 创建物品 magic_amulet = Item("魔法护身符") spellbook = Item("法术书") crystal_sword = Item("水晶剑") enchanted_staff = Item("魔法法杖") healing_potion = Item("治疗药水") cloak_of_invisibility = Item("隐形斗篷") # 为房间添加物品 enchanted_forest.add_item(magic_amulet) ancient_library.add_item(spellbook) underground_caverns.add_item(crystal_sword) tower_of_wisdom.add_item(enchanted_staff) forgotten_crypt.add_item(healing_potion) labyrinth_of_shadows.add_item(cloak_of_invisibility) # 初始化玩家 player = Player() # 游戏循环 current_room = start_room while True: print(current_room) print("可用出口:", ", ".join(current_room.exits.keys())) print("你的背包:", [str(item) for item in player.inventory]) command = input("输入指令: ").strip().lower() # 修改移动逻辑:将命令转为大写匹配出口键 if command.upper() in current_room.exits: current_room = current_room.exits[command.upper()] elif command.startswith("pickup "): item_name = command.split(" ", 1)[1] if current_room.has_item(item_name): item = next(item for item in current_room.items if item.name.lower() == item_name.lower()) player.add_item(item) current_room.remove_item(item) print(f"你捡起了{item}。") else: print("这个房间里没有这个物品。") else: print("无效指令,请输入正确的指令。") if current_room is chamber_of_the_balrog: if len(player.inventory) == 6: print("你收集了所有物品!是时候面对炎魔了!") break else: print("你还没准备好面对炎魔,先收集所有物品吧。") print("恭喜!你击败了炎魔,赢得了游戏!")
额外优化建议
- 可以在输出可用出口时,同时显示对应的房间名称,提升用户体验:比如
print("可用出口:", ", ".join([f"{dir} -> {room.name}" for dir, room in current_room.exits.items()])) - 增加命令提示,比如在输入错误时告知可用命令格式(如
"n/s/e/w"或"pickup [物品名称]")
内容的提问来源于stack exchange,提问作者the_kwillis
相关产品推荐
相关产品推荐

