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

Python文字冒险游戏:移动报错、背包更新、胜负逻辑问题求助

文本冒险游戏Bug修复方案

现存问题对应修复点

  • 指令与移动逻辑修复
    • 调整输入处理顺序,exit指令直接触发退出,不再走后续指令拆分逻辑,解决运行报错问题
    • 增加指令合法性校验,对输入不完整(比如只输入go不带方向、只输入get不带物品名)的情况给出提示,避免数组越界报错
    • 修正无效路径判定逻辑,当前房间没有对应方向出口时正常输出重试提示
  • 物品系统逻辑修复
    • 统一房间配置字典的字段名,把Hidden Storage房间写错的items键改为item,解决物品读不到的问题
    • 重写状态展示函数:进入房间时自动检测当前房间是否存在可拾取物品,存在则打印物品提示;每次状态更新都实时打印当前背包全部内容
    • 拾取物品成功后立刻删除当前房间的物品标记,避免重复拾取,同时修正拾取成功的提示文案,展示具体获取的物品名称
  • 胜负判定逻辑补充
    • 每次完成房间移动后立刻做房间校验:如果进入反派The Villain房间,判断背包物品数量,集齐6件则触发胜利结局,未集齐则触发失败结局,直接结束游戏

修复后完整可运行代码

def show_instructions():
    # 打印游戏主菜单和操作指令
    print('Tea Shop Quest Game')
    print("Collect 6 items to win the game or be consumed by Emma's Rage!!")
    print("Move commands: go South, go North, go East, go West")
    print("Add to Inventory: get 'item name'")


def currentStatus(currentRoom, currentInventory, rooms):
    # 打印当前游戏状态
    print(' ---------------------------')
    print(f'You are in {currentRoom}')
    # 判断当前房间是否有可拾取物品
    if 'item' in rooms[currentRoom]:
        print(f'You see a "{rooms[currentRoom]["item"]}"')
    else:
        print("There's nothing to pick up here.")
    print('Your current inventory is:')
    print(currentInventory if currentInventory else 'Empty')


def main():
    # 房间配置字典:关联相邻房间、房间内物品
    Rooms = {
        'Upper Room1': {'west': 'Upstairs', 'item': 'Lift Off'},
        'Upstairs': {'south': 'The Counter', 'east': 'Upper Room1', 'item': 'Aloe/Tea'},
        'Side Room': {'east': 'The Counter', 'item': 'Cup/Lid'},
        'The Counter': {'south': 'Backroom', 'west': 'Side Room', 'east': 'Storage Room', 'north': 'Upstairs'},
        'Storage Room': {'west': 'The Counter', 'north': 'Hidden Storage', 'item': 'Flavors'},
        'Hidden Storage': {'south': 'Storage Room', 'item': 'Ice'},
        'Backroom': {'north': 'The Counter', 'east': 'The Villain', 'item': 'Straw'},
        'The Villain': {'west': 'Bedroom'}
    }
    # 初始化玩家状态
    Inventory = []
    currentRoom = 'The Counter'
    required_item_count = 6
    show_instructions()

    # 游戏主循环
    while True:
        # 打印当前状态
        currentStatus(currentRoom, Inventory, Rooms)

        # 获取玩家输入
        print("Where do you want to go? ")
        user_input = input('>').strip()

        # 退出指令处理
        if user_input.lower() == 'exit':
            break

        # 拆分指令,处理输入不完整的情况
        navigate = user_input.split(' ', 1)
        if len(navigate) < 2:
            print("Invalid command! Please input full command like 'go North' or 'get item name'.")
            continue
        command = navigate[0].lower()
        param = navigate[1].lower()

        # 移动逻辑处理
        if command == 'go':
            direction = param
            if direction in Rooms[currentRoom]:
                currentRoom = Rooms[currentRoom][direction]
                # 进入新房间后做胜负判定
                if currentRoom == 'The Villain':
                    if len(Inventory) == required_item_count:
                        print("Congratulations! You collected all items and defeated Emma! You win!")
                    else:
                        print(f"Oh no! You ran into Emma without collecting all {required_item_count} items! You are consumed by her rage... Game Over.")
                    break
            else:
                print('You can\'t go that way! Try another direction.')
        # 拾取物品逻辑处理
        elif command == 'get':
            item_name = param
            if 'item' in Rooms[currentRoom] and item_name == Rooms[currentRoom]['item'].lower():
                if item_name not in Inventory:
                    Inventory.append(Rooms[currentRoom]['item'])
                    print(f"{Rooms[currentRoom]['item']} Added to Inventory!")
                    # 拾取后删除房间内的物品,避免重复拾取
                    del Rooms[currentRoom]['item']
                else:
                    print("You already have this item!")
            else:
                print(f"You can't collect {item_name} here.")
        else:
            print("Invalid command! Please use 'go [direction]' to move or 'get [item name]' to collect items.")

    print("Thanks for playing Tea Shop Quest!!")


if __name__ == "__main__":
    main()

运行效果说明

运行后完全匹配预期输出格式,初始进入The Counter房间时如果配置对应物品会正常展示,背包内容实时更新,输入exit可正常退出,走错方向会提示重试,进入反派房间时会根据背包收集情况触发对应结局。如果需要匹配示例里The Counter房间展示物品的效果,直接在The Counter对应的配置字典里添加'item': 'insert Item here'字段即可。

内容的提问来源于stack exchange,提问作者HYBRDF1R3 _

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.29 10:21:27