Python游戏Gym场景物品无法拾取问题求助(附代码)
问题:Gym场景无法拾取物品,提示“找不到该物品”
修改代码后,房间物品显示、移动功能均正常,但进入Gym场景尝试拾取物品时,会输出“找不到该物品”的提示,且每次尝试修复都会导致其他功能损坏。
原代码:
def clear(): os.system('cls' if os.name == 'nt' else 'clear') # dictionary rooms = { 'Locker Room': {'South': 'Dressing Room'}, # starting room 'Dressing Room': {'East': 'Ring', 'North': 'Locker Room', 'Item': 'Rics Robe'}, 'Ring': {'North': 'Gym', 'South': 'Steel Cage', 'West': 'Dressing Room', 'East': 'Office', 'Item': 'Chair'}, 'Gym': {'South': 'Ring', 'East': 'Garage', 'Item': 'RKO Promo'}, 'Garage': {'West': 'Gym', 'Item': 'Blueprint'}, 'Steel Cage': {'North': 'Ring', 'East': 'Green Room', 'Item': 'Mr.Socko'}, 'Green Room': {'West': 'Steel Cage', 'Item': 'StoneCold Beer'}, 'Office': {'West': 'Ring', 'Boss': 'Vance McMan'} # villain } # list of vowels vowels = ['a', 'e', 'i', 'o', 'u'] # directions direction = ['north', 'south', 'east', 'west'] # track inventory inventory = [] # track the current room current_room = 'Locker Room' Item = rooms[current_room].get('Item') # results of the players previous move msg = '' clear() prompt() # gameplay loop until exit while True: clear() # display info to player print(f"You are in the {current_room}\nInventory : {inventory}\n{' - ' * 27}") # display the msg print(msg) # indicate items if 'Item' in rooms[current_room].keys(): nearby_item = rooms[current_room]["Item"] if nearby_item not in inventory: if nearby_item[-1] == 's': print(f'You see {nearby_item}') elif nearby_item[0] in vowels: print(f'You see an {nearby_item}') else: print(f'You see a {nearby_item}') # boss encounter if 'Boss' in rooms[current_room].keys(): # lose if len(inventory) < 6: print(f"Game over! You lost a fight with {rooms[current_room]['Boss']}") # win else: print(f"You beat {rooms[current_room]['Boss']}") # accept the player move as input move = input('Which direction will you go?\n') # split the move into words next_move = move.split(' ') # the first word is an action action = next_move[0].title() if len(next_move) > 1: Item = next_move[1:] direction = next_move[1].title() Item = ' '.join(Item).title() # move in between rooms if action == 'Go': try: current_room = rooms[current_room][direction] msg = f'You go {direction}.' except: msg = f'You cannot go that way.' # picking up items elif action == "Get": if Item == rooms[current_room]['Item']: if Item not in inventory: inventory.append(rooms[current_room]['Item']) msg = f"{Item} retrieved!" else: msg = f"You already have the {Item} in your inventory." else: msg = f"Cannot find the {Item}" # exit the game if move == 'Exit': current_room = 'exit' print('Game over. Thank you for playing!') break # any other commands invalid else: msg = f"Invalid command"
问题原因
- 大小写不匹配:处理物品名称时使用
.title()将其转为每个单词首字母大写(如输入Get RKO Promo会被转为Rko Promo),但房间中存储的物品名是RKO Promo(RKO全大写),导致两者比对不相等,触发“找不到物品”提示。 - 全局变量被覆盖:全局
direction列表被错误赋值为物品名称的一部分,虽未直接影响拾取,但存在功能隐患。 - 逻辑分支顺序错误:
Exit的独立if语句与后续else配对,会覆盖Get/Go分支设置的提示信息。
修复后的代码
import os def clear(): os.system('cls' if os.name == 'nt' else 'clear') # 房间配置 rooms = { 'Locker Room': {'South': 'Dressing Room'}, # 初始房间 'Dressing Room': {'East': 'Ring', 'North': 'Locker Room', 'Item': 'Rics Robe'}, 'Ring': {'North': 'Gym', 'South': 'Steel Cage', 'West': 'Dressing Room', 'East': 'Office', 'Item': 'Chair'}, 'Gym': {'South': 'Ring', 'East': 'Garage', 'Item': 'RKO Promo'}, 'Garage': {'West': 'Gym', 'Item': 'Blueprint'}, 'Steel Cage': {'North': 'Ring', 'East': 'Green Room', 'Item': 'Mr.Socko'}, 'Green Room': {'West': 'Steel Cage', 'Item': 'StoneCold Beer'}, 'Office': {'West': 'Ring', 'Boss': 'Vance McMan'} # 最终Boss } # 元音列表 vowels = ['a', 'e', 'i', 'o', 'u'] # 有效方向(保留全局列表,不被覆盖) valid_directions = ['north', 'south', 'east', 'west'] # 玩家背包 inventory = [] # 当前房间 current_room = 'Locker Room' # 上一步操作提示 msg = '' clear() # prompt() # 原代码未定义该函数,暂注释 # 游戏主循环 while True: clear() # 显示当前状态 print(f"You are in the {current_room}\nInventory : {inventory}\n{' - ' * 27}") print(msg) # 显示当前房间物品 if 'Item' in rooms[current_room]: nearby_item = rooms[current_room]["Item"] if nearby_item not in inventory: if nearby_item[-1] == 's': print(f'You see {nearby_item}') elif nearby_item[0].lower() in vowels: print(f'You see an {nearby_item}') else: print(f'You see a {nearby_item}') # Boss遭遇战 if 'Boss' in rooms[current_room]: if len(inventory) < 6: print(f"Game over! You lost a fight with {rooms[current_room]['Boss']}") else: print(f"You beat {rooms[current_room]['Boss']}") # 获取玩家输入 move = input('Which direction will you go?\n').strip() msg = "" next_move = move.split() if not next_move: msg = "Invalid command" continue action = next_move[0].title() # 处理移动命令 if action == 'Go': if len(next_move) < 2: msg = "Please specify a direction (North/South/East/West)" continue go_direction = next_move[1].title() if go_direction in rooms[current_room]: current_room = rooms[current_room][go_direction] msg = f'You go {go_direction}.' else: msg = f'You cannot go that way.' # 处理拾取命令 elif action == "Get": if len(next_move) < 2: msg = "Please specify an item to get" continue requested_item = ' '.join(next_move[1:]) room_item = rooms[current_room].get('Item', '') # 忽略大小写比对,避免大小写问题 if requested_item.lower() == room_item.lower(): if room_item not in inventory: inventory.append(room_item) msg = f"{room_item} retrieved!" else: msg = f"You already have the {room_item} in your inventory." else: msg = f"Cannot find the {requested_item}" # 处理退出命令 elif move.lower() == 'exit': print('Game over. Thank you for playing!') break # 无效命令 else: msg = "Invalid command"
修复要点
- 补上缺失的
import os,注释未定义的prompt()函数。 - 将全局
direction列表改名为valid_directions,避免被错误覆盖。 - 拾取物品时采用忽略大小写的比对逻辑,解决物品名大小写不匹配问题。
- 调整逻辑分支结构,将
Exit命令纳入elif分支,避免覆盖其他操作的提示信息。 - 增加输入为空、参数缺失的判断,优化错误提示逻辑。
- 每次循环重置
msg,避免上一次操作的提示残留。
内容的提问来源于stack exchange,提问作者Rashy415635
相关产品推荐
相关产品推荐

