Python文本冒险游戏代码三类问题修复求助
Python文本冒险游戏问题修复
问题根因对应
- 主循环无效命令误触发:循环内指令判断全部使用独立
if语句,没有做分支互斥。用户输入移动指令执行完成后,代码仍会继续向下执行拾取指令的判断逻辑,移动指令拆分后首词不是get,直接命中最后else分支抛出无效命令提示。 - 拾取物品后字典更新报错:一是拾取逻辑仅往背包列表追加物品,没有同步删除对应房间的物品条目;二是初始房间
Bedroom未配置items字典对应键,进入房间后执行拾取判断会触发KeyError;三是重复拾取时没有做空值判断,找不到物品直接抛错。 - 双词物品无法拾取:现有逻辑取空格分割后的第二个元素作为物品名,输入
get fire spell时只会拿到fire,丢失后半段spell,无法匹配完整双词物品名。
修复点说明
- 把主循环的指令判断改为
if-elif-else互斥结构,匹配到对应指令执行完后直接跳过其他分支判断 - 给
items字典补充Bedroom空物品配置,拾取成功后直接清空当前房间的物品条目,拾取前先判断房间是否存在可拾取物品,避免KeyError - 处理
get指令时使用split(maxsplit=1)做分割,只拆分第一个空格,保留空格后的完整物品名做匹配 - 补充背包满6个物品自动传送至Evil Lair的逻辑(原需求提及但未实现)
修复后完整源码
rooms = { 'Bedroom': {'South': 'Old Living Room'}, 'Old Living Room': {'South': 'Old Office', 'East': 'Old Kitchen', 'West': 'Old Garage'}, 'Old Garage': {'East': 'Old Living Room'}, 'Old Kitchen': {'West': 'Old Living Room', 'North': 'Pulsing Room', 'South': 'Freezer Room'}, 'Old Office': {'North': 'Old Living Room'}, 'Pulsing Room': {'West': 'Dining Room', 'South': 'Old Kitchen'}, 'Freezer Room': {'North': 'Old Kitchen'}, 'Evil Lair': {'Teleported here after all items gathered'} } story = { 'Bedroom': {'text': 'You see your old childhood bedroom and a mysterious door.'}, 'Old Living Room': {'text': 'You enter the old living room and the bedroom door disappears behind you. You see a stick that appears to be a wand on a coffee table.'}, 'Old Garage': {'text': 'You enter an old garage. On an old moving box you see a glowing red health potion'}, 'Old Kitchen': {'text': 'You are in an old familiar kitchen. On top of the oven you see a scroll for a fire spell'}, 'Old Office': {'text': 'You are in your fathers old office. On his old desk you see a blue glowing magic shield'}, 'Pulsing Room': {'text': 'You enter an unfamiliar purple pulsing room. In the center of the room you see a portal key'}, 'Freezer Room': {'text': 'You are in a freezer room. The cold appears to be coming from a scroll that has an ice spell written on it.'}, 'Evil Lair': {'text': 'You are teleported to the Evil Lair. You see Lazarus in front of you.'} } # 补充Bedroom空物品配置,避免KeyError items = { 'Bedroom': {'contents': ''}, 'Old Living Room': {'contents': 'Wand'}, 'Old Garage': {'contents': 'Health Potion'}, 'Old Kitchen': {'contents': 'Fire Spell'}, 'Old Office': {'contents': 'Magic Shield'}, 'Pulsing Room': {'contents': 'Portal Key'}, 'Freezer Room': {'contents': 'Ice Spell'}, 'Evil Lair': {'contents': 'Lazarus, the Demon Mage'} } current_room = 'Bedroom' inventory = [] def get_next_room(current_room, direction): next_room = current_room for x in rooms: if x == current_room: if direction in rooms[x]: next_room = rooms[x][direction] return next_room def move_instructions(): print('########################################################################') print('# Text based Adventure game #') print('# Collect 6 items to face the final boss, Lazarus!! #') print('# Use movement commands North, South, East, West to move between rooms #') print("# 'Add to inventory: get 'item name' #") print('########################################################################\n') move_instructions() input('Press Enter to begin your adventure!!') print('####################################################################################################################') print('You awaken in your childhood room, but something is different about it. It is dark and appears that the room is \n' 'falling apart. There are cracks in the walls the glow purple. Unsure of how you got here you stand up and look out \n' 'the only window in the room to see you are floating through what looks like a void. As you look out the window you \n' 'see a mysterious figure begin to come toward the window. As it gets closer you realize it is not a person but a \n' 'monster of some sort. It has tentacles for legs, human like upper body and arms. Its head is human like with no \n' 'hair or eyes. It comes to the window and laughs as it says “You shall never leave this place. This is my domain. \n' 'MUHAHAHA”. The figure suddenly disappears. Your task is to gather the items to access the portal that is protecting \n' 'the creature. To do this, you must collect the items to battle the evil demon wizard Lazarus as well as the portal key. \n' 'You will need a wand, magic shield, a fire spell, a ice spell, a health potion, and the pulsing purple portal key. \n' 'Once all items are collected the key will begin pulsing and teleport the player to the Evil Lair.') print('####################################################################################################################\n') print('You see your old childhood bedroom and a mysterious door.') while True: print('Inventory: ', inventory) # 收集满6件物品自动传送至boss房 if len(inventory) == 6 and current_room != 'Evil Lair': current_room = 'Evil Lair' print(*story[current_room].values()) movement = input('What would you like to do?') movement = movement.capitalize() # 改为elif互斥分支,避免误触发无效命令 if movement == 'Exit': print('Game ended') exit(0) elif movement in ['North', 'South', 'East', 'West']: next_room = get_next_room(current_room, movement) if next_room == current_room: print('There is a wall there. Try a different movement command.') else: current_room = next_room print(*story[current_room].values()) elif movement.lower().split()[0] == 'get': # 用maxsplit=1只拆分第一个空格,保留完整物品名 parts = movement.lower().split(maxsplit=1) if len(parts) < 2: print('Please specify the item you want to get') continue item = parts[1].title() current_item = items[current_room]['contents'] if current_item and item == current_item.lower(): inventory.append(current_item) # 拾取后清空当前房间物品 items[current_room]['contents'] = '' print(f'You have obtained the {current_item}') else: print('Invalid item') else: print('Invalid Command')
内容的提问来源于stack exchange,提问作者Larry Hearold
相关产品推荐
相关产品推荐

