You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

修复Python Killer Wolf文本冒险游戏胜利条件与Inventory重复bug

Bug 修复说明
  • 背包重复添加问题修复
    原逻辑判断条件错误,写死检查字符串'item'是否存在于背包中,永远无法命中重复校验,导致可无限重复添加同一物品。修正为:
    1. 单独识别Get开头的物品获取指令,避免和移动指令逻辑混淆
    2. 校验当前输入的物品是否为所在房间的可获取物品
    3. 若物品已存在于背包,直接输出You already have this item,不再重复添加
  • 胜利条件逻辑修复
    原逻辑错误,只要背包集齐6个物品就提前弹出恭喜提示,不符合“集齐物品后进入Petting area才触发胜利”的需求。修正为:
    1. 移除原逻辑中无场景判断的集齐物品弹窗
    2. 在进入Petting area的场景判断中新增分支:若背包物品数达到6,输出指定胜利语句You defeated the Killer Wolf so I hope you enjoyed playing.并结束游戏;若不足6则保留原有的被狼击杀的死亡逻辑
    3. 优化物品提示:已获取过的物品不再重复提示“找到新物品”,改为提示已在背包中

完整可运行代码

def game_instructions():
    # print a main menu and the commands
    print("Killer Wolf Text Game")
    print("---" * 20)
    print("Collect 6 items to win the game, or be killed by the Wolf.")
    print("---" * 20)
    print('Move Commands: North , South , East , or West.')
    print("---" * 20)
    print("Add to Inventory: get 'item name'")
    print("---" * 20)


game_instructions()

rooms = {
    "Main fair ground": {'South': 'Food stand', 'North': 'Arcade', 'East': 'Corn field', 'West': 'Candy shop'},
    "Food stand": {'North': 'Main fair ground', 'East': 'Security', 'item': 'meat'},
    "Security": {'West': 'Food stand', 'item': 'armor'},
    "Arcade": {'South': 'Main fair ground', 'East': 'Gift shop', 'item': 'sword'},
    "Gift shop": {'West': 'Arcade', 'item': 'gloves'},
    "Candy shop": {'item': 'candy', 'East': 'Main fair ground'},
    "Corn field": {'West': 'Main fair ground', 'North': 'Petting area', 'item': 'repellent'},
    "Petting area": {'South': 'Corn field', 'item': 'Killer Wolf'}
}

current_room = 'Main fair ground'  # starts player in the Main fair ground
inventory = []  # Adds an inventory


def get_new_room(current_room, direction):
    new_room = current_room
    for room_name in rooms:
        if room_name == current_room:
            if direction in rooms[room_name]:
                new_room = rooms[room_name][direction]
    return new_room


# 游戏主循环
while current_room:
    print(f'You are in the {current_room}')
    print(f'Inventory: {inventory}')
    current_room_data = rooms[current_room]

    # Petting area 专属胜负判断逻辑
    if current_room == 'Petting area':
        if len(inventory) >= 6:
            print('You defeated the Killer Wolf so I hope you enjoyed playing.')
        else:
            print('NOM NOM, You did not collected all the necessary items to survive! The game has ended, I hope you enjoyed!')
        break

    # 普通房间的物品提示逻辑
    item = current_room_data.get('item', 'This room has no item!')
    if item != 'This room has no item!':
        if item in inventory:
            print(f'The item in this room ({item}) is already in your inventory')
        else:
            print(f'You found the: {item}')
    else:
        print('This room has no item!')

    # 获取玩家输入并标准化处理
    direction = input('Enter direction you would like to move. >>')
    direction = direction.strip().capitalize()

    # 处理移动指令
    if direction in ['North', 'South', 'East', 'West']:
        new_room = get_new_room(current_room, direction)
        if new_room == current_room:
            print('That is a wall not an exit. Try Again!')
        else:
            current_room = new_room

    # 处理物品获取指令
    elif direction.startswith('Get '):
        # 校验当前房间是否存在可获取的非狼物品
        if 'item' in current_room_data and current_room_data['item'] != 'Killer Wolf':
            target_item = current_room_data['item']
            # 校验输入的物品是否和当前房间的物品匹配
            if direction == f'Get {target_item.capitalize()}':
                if target_item in inventory:
                    print('You already have this item')
                else:
                    inventory.append(target_item)
                    print(f'Added {target_item} to your inventory')
            else:
                print('That item is not available in this room!')
        else:
            print('There is no item to get in this room!')

    else:
        print('Not a valid direction!')

内容的提问来源于stack exchange,提问作者Ms_python.newbie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.25 18:15:09