Python文本游戏可启动但无法切换房间与拾取物品问题排查
Python文本游戏问题解答
问题描述
我正在完成一门Python脚本课程的项目,目标是制作一款收集物品后对抗最终BOSS的文本类游戏。目前游戏代码已基本完成,但测试时始终无法切换房间,也无法拾取物品。以下是我的代码,希望能得到带解释的解答,以便我理解自己的错误。
# game start def game_insturctions(): # introducing players to game and controls print("You've been hired to investigate this light house") print("Scavenge for 6 items you'll need to complete your investigation.") print("Be warned of the dangers lurking beneath the light house") print("without all 6 items you will not make it out alive.") print('Movement Controls: north, south, east, west') print("Pick up items: Get 'item name' ") print("Quit Game: quit") print("--------------------\n") game_insturctions() # adding list of rooms and items rooms = { 'Dock': {'south': 'LH exterior', 'item': 'Flashlight'}, 'LH exterior': {'north': 'Dock', 'east': 'Living Quarters'}, 'Living Quarters': {'north': 'Basement', 'east': 'Kitchen', 'south': 'Service Room', 'west': 'LH exterior', 'item' : 'Fire Axe'}, 'Basement': {'south': 'Living Quarters', 'item': 'Eldritch Horror'}, 'Kitchen': {'west': 'Kitchen', 'item': 'First Aid Kit'}, 'Service Room': {'north': 'Living Quarters', 'east': 'Watch Room', 'west': 'Lantern Room'}, 'Watch Room': {'west': 'Service Room', 'item': "Keeper's Journal"}, 'Lantern Room': {'west': 'Service Room', 'item': 'Strange Necklace'}, } current_room = 'Dock' # starts player on dock at game start inventory = [] # player inventory def get_new_room(current_room, direction): # allow players to enter new rooms new_room = current_room for i in rooms: if i == current_room: if direction in rooms[i]: new_room = rooms[i][direction] return new_room def get_item(current_room): # allow player to get items if 'item' in rooms[current_room]: return rooms[current_room]['item'] if 'item' not in rooms[current_room]: return 'There is nothing to find here.' # no item message while (current_room): print("You're at the", current_room) # tells current room print('Inventory:', inventory) # shows inventory item = get_item(current_room) if item !='Eldritch Horror': if current_room == 'Basement': print("Nothing in here.") if current_room != 'Basement': if item in inventory: print('You already have this item. Better check out the rest of the grounds.') else: print('You found a', item , "you should bring it along.") pickup = input() pickup = pickup.title() if pickup == str('Get' + item): # input for item pickup inventory.append(item) print('You picked up the' + item + '.') if pickup == 'quit': print("The horror's persist.") break if item == 'Eldritch Horror': if len(inventory) == 6: print("You have successfully defeated the horror of the lighthouse and solved the case.") break if len(inventory) != 6: print("You ventured into the horrors lair unprepared and thus have lost your mind to the void. Game Over...") break direction = input('Where would you like to go? ') # prompt input from the player direction = direction.capitalize() if direction == 'quit': print("The horror's persist.") break if (direction == 'north' or direction == 'south' or direction == 'east' or direction == 'west'): new_room = get_new_room(current_room, direction) if new_room == current_room: print('No where to go that way.') else: current_room = new_room else: print('No where to go that way.') # end of code
错误原因与解释
1. 物品拾取失败的核心原因
你在判断拾取输入时用了pickup == str('Get' + item),但玩家实际需要输入的是Get Flashlight(带空格),而代码拼接出来的是GetFlashlight(无空格),两者永远无法匹配,导致无法拾取物品。
2. 无法切换房间的核心原因
处理方向输入时使用了direction.capitalize(),把玩家输入的方向变成首字母大写(比如North),但房间字典中的方向键都是小写(比如'south'),导致get_new_room函数无法找到对应的方向,房间无法切换。
3. 其他影响游戏体验的问题
- 厨房的方向设置错误:
'Kitchen': {'west': 'Kitchen', ...},west方向指向自身,导致无法从厨房返回生活区 - 物品拾取后未从房间移除,会重复提示发现同一物品
- Basement的判断逻辑冗余:已经通过
item != 'Eldritch Horror'过滤,无需再单独判断当前房间是否为Basement
修正后的完整代码
# game start def game_instructions(): # 修正函数名拼写错误 print("You've been hired to investigate this lighthouse") print("Scavenge for 6 items you'll need to complete your investigation.") print("Be warned of the dangers lurking beneath the lighthouse") print("without all 6 items you will not make it out alive.") print('Movement Controls: north, south, east, west') print("Pick up items: Get 'item name' ") print("Quit Game: quit") print("--------------------\n") game_instructions() rooms = { 'Dock': {'south': 'LH exterior', 'item': 'Flashlight'}, 'LH exterior': {'north': 'Dock', 'east': 'Living Quarters'}, 'Living Quarters': {'north': 'Basement', 'east': 'Kitchen', 'south': 'Service Room', 'west': 'LH exterior', 'item' : 'Fire Axe'}, 'Basement': {'south': 'Living Quarters', 'item': 'Eldritch Horror'}, 'Kitchen': {'west': 'Living Quarters', 'item': 'First Aid Kit'}, # 修正厨房west方向指向 'Service Room': {'north': 'Living Quarters', 'east': 'Watch Room', 'west': 'Lantern Room'}, 'Watch Room': {'west': 'Service Room', 'item': "Keeper's Journal"}, 'Lantern Room': {'east': 'Service Room', 'item': 'Strange Necklace'}, # 修正灯塔室方向(原west指向错误) } current_room = 'Dock' inventory = [] def get_new_room(current_room, direction): # 简化函数逻辑,无需遍历所有房间 if direction in rooms[current_room]: return rooms[current_room][direction] return current_room def get_item(current_room): if 'item' in rooms[current_room]: return rooms[current_room]['item'] return 'There is nothing to find here.' while current_room: print(f"You're at the {current_room}") print(f'Inventory: {inventory}') item = get_item(current_room) if item != 'Eldritch Horror': if item in inventory: print('You already have this item. Better check out the rest of the grounds.') else: print(f'You found a {item}, you should bring it along.') pickup = input('Enter command: ') pickup = pickup.strip().title() # 修正拾取判断,加入空格 if pickup == f'Get {item}': inventory.append(item) print(f'You picked up the {item}.') # 拾取后从房间移除物品,避免重复提示 del rooms[current_room]['item'] if pickup == 'Quit': print("The horror's persist.") break else: if len(inventory) == 6: print("You have successfully defeated the horror of the lighthouse and solved the case.") break else: print("You ventured into the horror's lair unprepared and thus have lost your mind to the void. Game Over...") break direction = input('Where would you like to go? ') # 改为全小写,匹配房间字典中的方向键 direction = direction.strip().lower() if direction == 'quit': print("The horror's persist.") break if direction in ['north', 'south', 'east', 'west']: new_room = get_new_room(current_room, direction) if new_room == current_room: print('Nowhere to go that way.') else: current_room = new_room else: print('Invalid command. Use north/south/east/west or quit.') # end of code
修正要点说明
- 修正了函数名拼写错误(
game_insturctions→game_instructions) - 调整方向输入处理为全小写,匹配房间字典的键
- 修正物品拾取的判断逻辑,加入空格匹配玩家输入
- 拾取物品后从房间中移除该物品,避免重复提示
- 修正厨房和灯塔室的方向错误,保证移动逻辑正常
- 简化
get_new_room函数逻辑,无需遍历所有房间
内容的提问来源于stack exchange,提问作者Mica
相关产品推荐
相关产品推荐

