Python文本类僵尸游戏代码报错求助:附代码及错误信息
文本类Zombie游戏代码问题修复方案
问题概述
制作的文本类Zombie游戏代码存在语法冗余、逻辑错误及PEP8规范问题,导致无法正常运行,结合给出的错误提示,以下是具体修复方案。
错误分析与修复步骤
1. 重复定义get_item函数(Redeclared 'get_item' defined above without usage)
- 问题:代码中两次定义
get_item函数,第一个定义未被使用,直接被第二个覆盖,造成冗余;同时函数参数state与全局变量重名,触发作用域阴影警告。 - 修复:删除未使用的第一个
get_item定义,将函数参数名改为current_state,避免与全局变量冲突。
2. 不可达代码(This code is unreachable:71)
- 问题:主游戏逻辑在
while True循环中,只有通过exit(0)才能退出,因此循环后的stop = "go"及后续代码永远不会执行,属于冗余代码。 - 修复:删除这段冗余的循环代码。
3. PEP8规范问题(E303 too many blank lines)
- 问题:第二个
get_item函数定义前存在过多空行,不符合PEP8代码规范。 - 修复:删除多余空行,保持函数间空2行的标准格式。
4. 房间名称大小写不一致(逻辑错误)
- 问题:
Gun Room中East指向的Toy room(小写r)与房间字典中的Toy Room(大写R)不一致,导致无法正常移动到该房间。 - 修复:将
Gun Room中的'East': 'Toy room'改为'East': 'Toy Room'。
5. 其他警告处理
- Name 'state' can be undefined:全局变量
state已初始化为'Master Bedroom',循环内的赋值也确保其始终有效,修复其他问题后该警告会自动消失。 - Unresolved reference 'inventory':该警告由不可达代码引发,删除冗余代码后即可解决。
修复后的完整代码
rooms = { 'Master Bedroom': {'South': 'Kitchen', 'North': 'Toy Room', 'East': 'Garage', 'West': 'Closet'}, 'Kitchen': {'North': 'Master Bedroom', 'East': 'Bathroom', 'West': 'Backyard', 'Item': 'Cell Phone'}, 'Gun Room': {'South': 'Closet', 'East': 'Toy Room', 'Item': 'Pistol'}, # 修正房间名称大小写 'Backyard': {'East': 'Kitchen', 'Item': 'ZOMBIE'}, 'Garage': {'West': 'Master Bedroom', 'South': 'Bathroom', 'Item': 'Body Armor'}, 'Toy Room': {'South': 'Master Bedroom', 'West': 'Gun Room', 'Item': 'Baseball Bat'}, 'Closet': {'East': 'Master Bedroom', 'North': 'Gun Room', 'Item': 'Flashlight'}, 'Bathroom': {'North': 'Garage', 'West': 'Kitchen', 'Item': 'Bandages'} } def get_item(current_state): # 修改参数名避免与全局state重名 if current_state != 'Master Bedroom': return rooms[current_state]['Item'] else: return 'Nothing' def show_instructions(): print('ZOMBIE run Adventure Game') print('Collect 6 items to win the game, or be eaten by a ZOMBIE.') print('Move commands: go South, go North, go East, go West') print("Add to Inventory: get 'Item_name'") print("type quit to end the game") show_instructions() inventory = [] state = 'Master Bedroom' while True: print('You are in', state) print('Inventory:', inventory) item = get_item(state) # 优化物品显示逻辑,输出更自然 if item == 'Nothing': print('You see nothing of interest.') elif item == 'ZOMBIE': print('You see a ZOMBIE!') else: # 根据物品首字母选择a/an冠词 article = 'an' if item[0].lower() in ['a', 'e', 'i', 'o', 'u'] else 'a' print(f'You see {article} {item}.') print('--------------------') if item == 'ZOMBIE': print('NOM NOM...GAME OVER!') exit(0) direction = input('Enter your move: ').strip() if direction.startswith('go '): move_dir = direction[3:].strip() if move_dir in ['East', 'West', 'North', 'South']: new_state = rooms[state].get(move_dir) if new_state is None: print('The room has a wall in that direction! Choose another direction.') else: state = new_state else: print('Invalid direction! Use go North/South/East/West.') elif direction.startswith('get '): item_name = direction[4:].strip() if item != 'Nothing' and item_name == item and item not in inventory: inventory.append(item) print(f'You picked up the {item}!') else: print('That item could not be taken at this location.') elif direction.lower() == 'quit': print('Thanks for playing!') exit(0) else: print('Invalid command! Use move commands, get commands, or quit.') if len(inventory) == 6: print('Congratulations! You have collected all items and defeated the ZOMBIE!') exit(0)
额外优化说明
- 增加输入
strip()处理,去除用户输入的多余空格,提升体验。 - 添加
quit命令支持,让玩家可以正常退出游戏。 - 优化物品显示的冠词逻辑,让输出更符合自然语言习惯。
- 简化移动命令的判断逻辑,代码更简洁易读。
内容的提问来源于stack exchange,提问作者Crystal Vancleave
相关产品推荐
相关产品推荐

