IT-140课程项目II:文本游戏无法添加物品至Inventory求助
问题与解决方案
我在开发IT-140课程的文本游戏项目,用Python字典搭建游戏场景,目前能正常实现房间移动,但没办法把物品添加到背包(Inventory)里。现有代码如下:
rooms = { 'Parking lot': {'South': 'Main entrance', 'North': 'dragon'}, 'Main entrance': {'North': 'Parking lot', 'South': 'Check in area', 'item': 'red star'}, 'Check in area': {'North': 'Main entrance', 'South': 'Main hallway', 'item': 'blue star'}, 'Main hallway': {'North': 'Check in area', 'South': 'Security area', 'item': 'green star'}, 'Security area': {'North': 'Main hallway', 'South': 'Gates hallway', 'item': 'yellow star'}, 'Gates hallway': {'North': 'Security area', 'West': 'Waiting area A', 'East': 'Waiting area B', 'item': 'orange star'}, 'Waiting area A': {'East': 'Gates hallway', 'item': 'purple star'}, 'Waiting area B': {'West': 'Gates hallway ', 'item': 'pink star'} } def welcome(): print("Welcome to Magic Stars!!") print("There is a Dragon threatening to destroy your city, currently is in the airport.") print("you need to collect the 7 magic stars that are spread around the abandoned airport.") print("are you ready to start this adventure?") print('----------------------------------------') welcome() inventory = [] directions = ['north', 'south', 'east', 'west', 'exit', 'take'] current_room = 'Main entrance' while True: print("You are in {}".format(current_room)) print('Your current inventory: {}'.format(inventory)) print('In this room you see the {}'.format(rooms[current_room]['item'])) command = input("Enter command:").split()[-1].capitalize() print('-----------------------------') if command == 'Exit': current_room = 'Exit' print("Thanks for Playing!!") break elif command in rooms[current_room]: current_room = rooms[current_room][command] # invalid move else: print("Invalid move")
问题分析
- 命令解析错误:原代码用
split()[-1].capitalize()获取命令,输入take red star会得到Star,无法识别take指令。 - 缺少物品拾取逻辑:没有处理
take命令的代码分支,无法将物品加入背包。 - 无物品房间报错:
Parking lot等房间没有item键,执行rooms[current_room]['item']会触发KeyError。 - 房间路径错误:
Waiting area B的West指向'Gates hallway '(末尾多空格),导致无法正常移动到该房间。
修改后的代码
rooms = { 'Parking lot': {'South': 'Main entrance', 'North': 'dragon'}, 'Main entrance': {'North': 'Parking lot', 'South': 'Check in area', 'item': 'red star'}, 'Check in area': {'North': 'Main entrance', 'South': 'Main hallway', 'item': 'blue star'}, 'Main hallway': {'North': 'Check in area', 'South': 'Security area', 'item': 'green star'}, 'Security area': {'North': 'Main hallway', 'South': 'Gates hallway', 'item': 'yellow star'}, 'Gates hallway': {'North': 'Security area', 'West': 'Waiting area A', 'East': 'Waiting area B', 'item': 'orange star'}, 'Waiting area A': {'East': 'Gates hallway', 'item': 'purple star'}, 'Waiting area B': {'West': 'Gates hallway', 'item': 'pink star'} # 修复末尾空格问题 } def welcome(): print("Welcome to Magic Stars!!") print("There is a Dragon threatening to destroy your city, currently is in the airport.") print("you need to collect the 7 magic stars that are spread around the abandoned airport.") print("are you ready to start this adventure?") print('----------------------------------------') welcome() inventory = [] valid_commands = ['north', 'south', 'east', 'west', 'exit', 'take'] current_room = 'Main entrance' while True: print(f"You are in {current_room}") print(f'Your current inventory: {inventory}') # 处理无物品的房间 if 'item' in rooms[current_room]: print(f'In this room you see the {rooms[current_room]["item"]}') else: print('This room has no items to collect.') # 正确解析命令:取输入的第一个单词转为小写 user_input = input("Enter command (e.g., 'north', 'take red star'): ").strip().lower() if not user_input: print("Please enter a valid command.") print('-----------------------------') continue command_parts = user_input.split() command = command_parts[0] print('-----------------------------') if command == 'exit': print("Thanks for Playing!!") break elif command == 'take': # 检查当前房间是否有物品 if 'item' not in rooms[current_room]: print("There's nothing to take here!") continue item = rooms[current_room]['item'] # 检查物品是否已在背包 if item in inventory: print(f"You already have the {item}!") else: inventory.append(item) print(f"You took the {item}!") # 从房间移除物品,避免重复拾取 del rooms[current_room]['item'] elif command in ['north', 'south', 'east', 'west']: # 检查方向是否有效 if command.capitalize() in rooms[current_room]: current_room = rooms[current_room][command.capitalize()] else: print("Invalid move! That direction doesn't lead anywhere.") else: print("Invalid command! Try directions (north/south/east/west), 'take [item]', or 'exit'.")
修改说明
- 修复了
Waiting area B的路径空格问题,确保移动正常。 - 调整命令解析逻辑,取输入的第一个单词作为命令,支持
take [item]格式的指令。 - 添加
take命令处理分支:检查房间物品、背包状态,完成拾取后移除房间内的物品。 - 增加无物品房间的判断,避免
KeyError。 - 优化提示信息,让玩家更清楚可用命令。
内容的提问来源于stack exchange,提问作者J Merino
相关产品推荐
相关产品推荐

