Python文本类课程游戏无法移动房间/拾取物品问题求助
文本类游戏卡初始界面问题排查与修复
问题描述
我正在开发一个Python课程项目的文本类游戏,玩法是玩家在不同房间间移动、收集各房间物品,最终进入反派所在的最终房间即可获胜。原本房间移动功能正常,但添加物品与玩家库存更新的代码后,游戏卡在初始界面,无法切换房间或执行任何操作。我怀疑问题出在字典上,但找不到具体原因,需要排查建议。
问题代码
def start_menu(): # Display instructions to game print('Operation Kill Hitler') print('You must collect 6 items before entering Hitlers bunker, ' 'or you will be discovered and killed before taking out the Fuhrer!') print('Instructions') print('move commands: go south, go north, go east, go west') print("Add to Inventory: get 'item name'") # moving between rooms def move_to_rooms(current_room, move, rooms): return rooms[current_room].get(move, current_room) # add item to inventory and remove from room def get_item(current_room, move, rooms, inventory): inventory.append(rooms[current_room]['item']) del rooms[current_room]['item'] # dictionary links a room to other rooms and items in rooms def main(): rooms = { 'Guest Room': {'name': 'Guest Room', 'go west': 'Library'}, 'Library': {'name': 'Library', 'go east': 'Guest Room', 'go south': 'Main Hall', 'item': 'silencer'}, 'Main Hall': {'name': 'Main Hall', 'go west': 'Officers Quarters', 'go north': 'Library', 'go east': 'Dining Hall', 'go south': 'Wine Cellar', 'item': 'bullets'}, 'Officers Quarters': {'name': 'Officers Quarters', 'go east': 'Main Hall', 'item': 'nazi uniform'}, 'Dining Hall': {'name': 'Dining Hall', 'go north': 'Kitchen', 'go west': 'Main Hall', 'item': 'pistol'}, 'Kitchen': {'name': 'Kitchen', 'go south': 'Dining Hall', 'item': 'smoke grenade'}, 'Wine Cellar': {'name': 'Wine Cellar', 'go north': 'Main Hall', 'go east': 'Bunker', 'item': 'glass of wine'}, 'Bunker': {'name': 'Bunker'} } start_menu() # list of current inventory inventory = [] # starting room current_room = 'Guest Room' # Game Loop while True: # display current location and inventory print('You are in the {}.'.format(current_room)) print(inventory) # show if there is an item in the room if current_room != 'Bunker' and 'item' in rooms[current_room].keys(): print('You see the {}'.format(rooms[current_room]['item'])) print('------------------------------') # handle if the user enters a command to move to a new room move = input('What do you do?:\n').title().split() if len(move) >= 2 and move[1] in rooms[current_room].keys(): current_room = move_to_rooms(current_room, move[1], rooms) # handle if the user enters a command to get an item elif len(move) >= 2 and move[0] == 'Get' and ' '.join(move[1:]) == rooms[current_room].get('item'): print('You pick up the {}'.format(rooms[current_room]['item'])) print('------------------------------') # get_item(current_room, move, rooms, inventory) continue # handle if the user enters an invalid command else: print('Invalid move, please try again') continue main()
问题排查与修复要点
1. 命令大小写与格式不匹配
代码中使用.title().split()处理用户输入,导致输入的"go west"变成['Go', 'West'],但房间字典中的移动键是'go west'(全小写带空格),物品名也是全小写。这直接导致移动和收集物品的判断条件永远不满足,每次都触发"无效命令"提示。
修复: 将用户输入转为全小写,统一格式:
move = input('What do you do?:\n').lower().split()
2. 移动命令判断逻辑错误
原代码用move[1](即方向词)去匹配房间字典中的键,但字典中的移动键是'go west'这种完整命令,不是单独的'west'。
修复: 拼接完整的移动命令字符串,再去字典中查找:
if len(move) >= 2 and move[0] == 'go': full_move = ' '.join(move[:2]) if full_move in rooms[current_room]: current_room = move_to_rooms(current_room, full_move, rooms)
3. 物品收集判断逻辑错误
原代码用' '.join(move[1:])(首字母大写的物品名)和房间中的全小写物品名比较,永远不相等。
修复: 用全小写的物品名进行比较:
elif len(move) >= 2 and move[0] == 'get': target_item = ' '.join(move[1:]) room_item = rooms[current_room].get('item') if room_item and target_item == room_item.lower(): print(f'You pick up the {room_item}') print('------------------------------') get_item(current_room, move, rooms, inventory) continue
4. 激活库存更新函数
原代码中get_item函数被注释,导致收集物品后库存和房间物品都不会更新,取消注释即可。
5. 添加游戏结束判断
进入Bunker时,根据物品数量判断胜负:
# 在每次移动后检查是否进入Bunker if current_room == 'Bunker': if len(inventory) >= 6: print('You have collected all items and defeated Hitler! Victory!') else: print('You entered the bunker without all items! Hitler discovers you and you are killed. Game Over.') break
修复后的完整代码
def start_menu(): # Display instructions to game print('Operation Kill Hitler') print('You must collect 6 items before entering Hitlers bunker, ' 'or you will be discovered and killed before taking out the Fuhrer!') print('Instructions') print('move commands: go south, go north, go east, go west') print("Add to Inventory: get 'item name'") # moving between rooms def move_to_rooms(current_room, move, rooms): return rooms[current_room].get(move, current_room) # add item to inventory and remove from room def get_item(current_room, rooms, inventory): inventory.append(rooms[current_room]['item']) del rooms[current_room]['item'] # dictionary links a room to other rooms and items in rooms def main(): rooms = { 'Guest Room': {'name': 'Guest Room', 'go west': 'Library'}, 'Library': {'name': 'Library', 'go east': 'Guest Room', 'go south': 'Main Hall', 'item': 'silencer'}, 'Main Hall': {'name': 'Main Hall', 'go west': 'Officers Quarters', 'go north': 'Library', 'go east': 'Dining Hall', 'go south': 'Wine Cellar', 'item': 'bullets'}, 'Officers Quarters': {'name': 'Officers Quarters', 'go east': 'Main Hall', 'item': 'nazi uniform'}, 'Dining Hall': {'name': 'Dining Hall', 'go north': 'Kitchen', 'go west': 'Main Hall', 'item': 'pistol'}, 'Kitchen': {'name': 'Kitchen', 'go south': 'Dining Hall', 'item': 'smoke grenade'}, 'Wine Cellar': {'name': 'Wine Cellar', 'go north': 'Main Hall', 'go east': 'Bunker', 'item': 'glass of wine'}, 'Bunker': {'name': 'Bunker'} } start_menu() # list of current inventory inventory = [] # starting room current_room = 'Guest Room' # Game Loop while True: # display current location and inventory print(f'You are in the {current_room}.') print(f'Inventory: {inventory}') # show if there is an item in the room if current_room != 'Bunker' and 'item' in rooms[current_room]: print(f'You see the {rooms[current_room]["item"]}') print('------------------------------') # handle user input move = input('What do you do?:\n').lower().split() # handle move commands if len(move) >= 2 and move[0] == 'go': full_move = ' '.join(move[:2]) if full_move in rooms[current_room]: current_room = move_to_rooms(current_room, full_move, rooms) # Check if entered Bunker after moving if current_room == 'Bunker': if len(inventory) >= 6: print('You have collected all items and defeated Hitler! Victory!') else: print('You entered the bunker without all items! Hitler discovers you and you are killed. Game Over.') break continue else: print('Invalid move, please try again') continue # handle get item commands elif len(move) >= 2 and move[0] == 'get': target_item = ' '.join(move[1:]) room_item = rooms[current_room].get('item') if room_item and target_item == room_item.lower(): print(f'You pick up the {room_item}') print('------------------------------') get_item(current_room, rooms, inventory) continue else: print('That item is not here or invalid command!') continue # handle invalid commands else: print('Invalid move, please try again') continue main()
内容的提问来源于stack exchange,提问作者Russell Sweeten
相关产品推荐
相关产品推荐

