文本冒险游戏拾取物品后房间物品未删除的技术问题
解决文本冒险游戏拾取物品后房间物品未清除的问题
你的核心问题是拾取物品的逻辑仅将物品添加到玩家背包,但未同步更新房间的物品状态,且原代码中删除房间物品的逻辑位置错误,导致只有输入非grab指令时才会尝试修改房间状态,完全不符合需求。
问题分析
- 拾取物品时仅执行
inventory.append(),未修改房间内的item字段,导致show_status()始终显示原有物品。 - 原代码中删除房间物品的代码位于
direction_input != 'grab'的分支中,逻辑完全颠倒。
修复方案
在成功将物品添加到玩家背包后,立即将当前房间的item字段设置为'Nothing'(与初始无物品房间的状态保持一致),确保show_status()能正确显示房间已无物品。同时调整指令判断顺序,避免逻辑冲突。
完整修改后的代码
rooms = { # 创建房间字典 'Center of the Village': {'go north': "Village Scribe", 'go south': "Mage Tower", 'go east': 'Village Dining Hall', 'go west': 'Village Tailor', 'item': 'Nothing'}, 'Village Scribe': {'go east': 'Bridge to the Castle', 'go south': 'Center of the Village', 'item': 'The Monster Hunting Guidebook'}, 'Mage Tower': {'go north': 'Center of the Village', 'go east': 'Mage Library', 'item': 'The Magic Staff'}, 'Village Dining Hall': {'go north': 'Village Elders', 'go west': 'Center of the Village', 'item': 'The Hearty Stew'}, 'Village Tailor': {'go east': 'Center of the Village', 'item': 'The Magic Cloak'}, 'Village Elders': {'go south': 'Village Dining Hall', 'item': 'The Amulet of Protection'}, 'Mage Library': {'go west': 'Mage Tower', 'item': 'The Spell Caster Scrolls'}, 'Bridge to the Castle': {'go west': 'Village Scribe', 'item': 'The Bridge Troll'} } def main_menu(): # 游戏启动主菜单 print('---------------------------') print("*** Magicians Adventure Text Game ***\n") print("You Must Collect All Six Magic Items To Defeat The Bridge Troll\n") print("Movement Commands: \n" "\nType: 'go' Followed by: 'north', 'south', 'east', or 'west' to move through the village. " "\nType: 'grab' to pick up an item. " "\nType: 'exit' to end the game.") input('\nPress Enter to continue...\n---------------------------\n') return "Good Luck!" def show_status(): # 显示当前房间和背包状态 print('\n-------------------------') print('You are at the {}'.format(current_room)) print('In this space you see {}'.format(rooms[current_room]['item'])) print('Inventory:', inventory) print('-------------------------------') print(main_menu()) # 打印主菜单 print() current_room = 'Center of the Village' # 玩家初始房间 commands = ['go north', 'go south', 'go east', 'go west', 'exit', 'grab'] # 有效指令列表 inventory = [] # 玩家背包列表 def move_rooms(current_room, directions): # 房间移动逻辑 current_room = rooms[current_room] new_room = current_room[directions] return new_room while True: show_status() # 触发Boss房间逻辑 if current_room == 'Bridge to the Castle': if len(inventory) < 6: print('You Did Not Have Enough Magic Power! \nYou have been crushed by the Troll!!!') break else: print('You Have Slain The Bridge Troll!!! \nYou Are Victorious!!!') break direction_input = input('What is your move? \n \n> ') print('\n--------------------------') # 优先处理退出指令 if direction_input == 'exit': print('Thanks for playing!') print('---------------------------') break # 处理拾取物品指令 if direction_input == 'grab': current_item = rooms[current_room]['item'] if current_item == 'Nothing': print('There are no items here...') elif current_item in inventory: print('You already have this item!') else: inventory.append(current_item) rooms[current_room]['item'] = 'Nothing' print(f'You grabbed {current_item}!') continue # 处理完拾取后直接进入下一轮循环 # 处理移动指令 if direction_input in rooms[current_room]: current_room = move_rooms(current_room, direction_input) else: # 处理无效指令 if direction_input not in commands: print('Invalid move.\n') else: print(f'You can not go that way.') print('---------------------------\n')
修复说明
- 重构
grab指令逻辑:成功拾取物品后立即将房间的item设为'Nothing',确保房间状态同步更新。 - 调整指令判断顺序:先处理
exit和grab指令,再处理移动操作,避免逻辑冲突。 - 移除原代码中错误位置的物品删除逻辑,避免因误删字典键导致报错。
内容的提问来源于stack exchange,提问作者harperwill7
相关产品推荐
相关产品推荐

