Python文本冒险游戏无法切换房间问题求助
文本冒险游戏房间移动故障排查与修复
问题描述
我正在Python脚本入门课程中开发一款文本冒险游戏,同时编写伪代码辅助开发。测试时发现无法在房间间移动——代码无报错,但输入任意方向指令均停留在初始房间「邮件室(Mail Room)」。
游戏场景需求
就职于一家文本游戏开发小公司,需向团队提案一款自定义主题的文本冒险游戏,核心要求如下:
- 包含不同房间、道具和反派角色
- 玩家需在房间间移动收集所有道具,在遭遇反派前集齐即可获胜
- 玩家指令分为两类:移动房间(方向为北、南、东、西)、拾取当前房间道具
- 提案需包含游戏地图、房间移动及道具拾取的伪代码或流程图
原始代码
import time # A dictionary for the Hacker text game # The dictionary links a room to other rooms. rooms = { 'Mail Room': {'South': 'Main Clinic', 'East': 'Storage Room', 'item': 'Pager'}, # First Room 'Main Clinic': {'North': 'Mail Room', 'item': 'Printer', 'South': 'Billing Office', 'item': 'Laptop', 'East': 'Doctor Office', 'item': 'Phone', 'West': 'Accounting', 'item': 'Tablet'}, 'Doctor Office': {'North': 'Break Room', 'item': 'Coffee Machine', 'West': 'Main Clinic'}, 'Billing Office': {'East': 'IT Office'}, 'IT Office': {'West': 'Billing Office'} #Last Room } \#defining the game instructions def introduction(): print('') print('\*\*\*\*\*\*\*\*\*Hacker Text Adventure Game \*\*\*\*\*\*\*\*\*\\n') print('***') print('\\nThere are 8 rooms to move between and 6 items to pick up\\n') print('in order to complete your adventure.\\n') print('Directions: You can go North, South, East, or West\\n') print('to navigate between rooms. If the room has an item\\n') print('it will be picked up and added to your inventory.\\n') print('You can also type exit to leave the game. Good Luck!\\n') print('***\\n\\n') # Defining player status, prints def player_status(): print('=========================================') print('\\nYou are currently in the {}'.format(currentRoom)) print('\\nYour current inventory: {}'.format(inventory)) \#print('You see a {}'.format(currentItem)) print('\\n=========================================') print('\\n') # Inventory list to hold inventory as it's added in each room inventory = \\[\\] user_item = '' # defining Inventory carried to add to inventory list def game(item): \#user_item = input('') if item in inventory: # use in operator to check membership print("you already have this") print(" ".join(inventory)) elif item not in inventory: print('You see a', item) print('Would you like to pick it up? \n') user_item = input('') if user_item == 'Y': print('item has been added\n') inventory.append(item) else: print('You leave the item behind.') #else: #print("You see a", item) #print("and you pick it up, it has been added to inventory") #inventory.append(item) #print(" ".join(inventory)) # Current Room, starts off in Mail Room currentRoom = 'Mail Room' # Players Move player_move = '' # Calling the Introduction function introduction() # While Loop for moving between rooms while True: player_status() player_move = input('Enter a move: \n') if player_move == 'Exit': print('Thanks for playing.') # break statement for exiting break if player_move in rooms[currentRoom]: currentRoom = rooms[currentRoom][player_move] addItem = input(' \n') if 'item' in rooms[currentRoom]: game(rooms[currentRoom]['item']) #if/else statements with break statement for final room/finishing the game if currentRoom == 'IT Office': if len(inventory) == 6: print('\n*********************** Congratulations! ***********************') print('\n************************************************************************') print('\nYou have made it to the IT Office with all of the items to ') print('stop the hacker and recover the information.\n') print('You have completed your mission. You are a Hero to your company!\n') print('*************************************************************************\n') print('^^^^^^^^^^^^^^^^^^^^^^^^^^ Thanks for playing! ^^^^^^^^^^^^^^^^^^^^^^^^^^\n\n') break else: print('\n################## You Failed! #####################') print('\n####################################################') print('\nYou are missing items needed to stop the hacker') print('& the sensitive information is at risk. All of your') print('companies data is gone. Time to polish your resume.') print('\n####################################################\n') print('@@@@@@@@@@@@@@@@@@@ Please play again @@@@@@@@@@@@@@@@') break # else statement for input validation/invalid move else: print('\nYou walk in to a wall, try again.\n')
核心问题与修复方案
1. 缩进错误(致命问题)
Python是缩进敏感语言,原代码中rooms字典、introduction()等函数、inventory变量存在大量不必要的缩进,导致这些对象无法被全局逻辑正确访问,移动指令无法修改currentRoom的值。
2. 房间字典重复键问题
Main Clinic的字典中重复定义了多个item键,Python字典会自动覆盖重复键,最终只保留最后一个item值(Tablet),导致其他道具无法被识别。
3. 指令大小写不匹配
代码中判断player_move == 'Exit',但玩家输入小写exit会无效;方向判断严格区分大小写,输入小写south会被判定为无效移动。
4. 冗余输入步骤
移动后多余的addItem = input(' \n')会打断游戏流程,属于无效代码。
修复后的完整代码
import time # 黑客主题文本冒险游戏房间字典 rooms = { '邮件室': {'南': '主诊室', '东': '储藏室', '道具': '传呼机'}, # 初始房间 '主诊室': {'北': '邮件室', '南': '计费室', '东': '医生办公室', '西': '会计室', '道具': ['打印机', '笔记本电脑', '电话', '平板电脑']}, '医生办公室': {'北': '休息室', '西': '主诊室', '道具': '咖啡机'}, '计费室': {'东': 'IT办公室'}, 'IT办公室': {'西': '计费室'}, # 最终房间(反派所在) '储藏室': {'西': '邮件室'}, '会计室': {'东': '主诊室'}, '休息室': {'南': '医生办公室'} } # 游戏介绍 def introduction(): print('') print('**********黑客主题文本冒险游戏**********\n') print('***') print('\n共有8个房间可探索,需收集6件道具完成冒险\n') print('操作说明:输入 北/南/东/西 进行房间移动\n') print('进入有道具的房间后,可选择拾取道具加入背包\n') print('输入 退出 可结束游戏。祝你好运!\n') print('***\n\n') # 显示玩家当前状态 def player_status(): print('=========================================') print(f'\n你当前所在房间:{current_room}') print(f'\n当前背包:{inventory}') print('\n=========================================') print('\n') # 道具拾取逻辑 def pick_up_item(item): if isinstance(item, list): for i in item: if i not in inventory: print(f'你看到了一个{i}') choice = input('是否拾取?(Y/N) \n').strip().upper() if choice == 'Y': inventory.append(i) print(f'{i}已加入背包\n') else: print(f'你留下了{i}\n') else: print(f'你已经拥有{i}\n') else: if item in inventory: print(f'你已经拥有{item}\n') else: print(f'你看到了一个{item}') choice = input('是否拾取?(Y/N) \n').strip().upper() if choice == 'Y': inventory.append(item) print(f'{item}已加入背包\n') else: print(f'你留下了{item}\n') # 初始化游戏状态 inventory = [] current_room = '邮件室' # 启动游戏 introduction() # 游戏主循环 while True: player_status() player_move = input('请输入指令:\n').strip().upper() # 退出游戏 if player_move == '退出': print('感谢游玩!') break # 处理移动指令 if player_move in rooms[current_room]: current_room = rooms[current_room][player_move] # 检查当前房间是否有道具 if '道具' in rooms[current_room]: pick_up_item(rooms[current_room]['道具']) # 到达最终房间判断胜负 if current_room == 'IT办公室': if len(inventory) == 6: print('\n*********************** 恭喜通关! ***********************') print('\n********************************************************') print('\n你带着所有道具来到了IT办公室,成功阻止黑客并找回了机密信息!') print('你完成了任务,是公司的英雄!\n') print('*********************************************************\n') print('^^^^^^^^^^^^^^^^^^^^^^^^^^ 感谢游玩! ^^^^^^^^^^^^^^^^^^^^^^^^^^\n\n') break else: print('\n################## 挑战失败! #####################') print('\n####################################################') print('\n你缺少阻止黑客的必要道具,公司机密已泄露,准备更新简历吧...') print('\n####################################################\n') print('@@@@@@@@@@@@@@@@@@@ 欢迎再次挑战! @@@@@@@@@@@@@@@@') break else: print('\n你撞到了墙,请输入有效的方向指令!\n')
额外优化点
- 统一中文指令与房间名称,适配中文用户习惯
- 修复房间字典重复键问题,将多道具房间的道具改为列表形式
- 增加指令大小写兼容,玩家输入任意大小写均可识别
- 移除冗余输入步骤,优化游戏流畅度
- 完善多道具房间的拾取逻辑
内容的提问来源于stack exchange,提问作者Julie Mae Spohrer
相关产品推荐
相关产品推荐

