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

基于文本的Python期末项目游戏移动功能故障求助

《Fire Demon Hunt》角色无法移动问题排查

问题描述

我正在为期末项目开发一款名为《Fire Demon Hunt》的基于文本的Python游戏,但目前游戏内角色无法进行移动操作,不知道问题出在哪,以下是我的游戏代码:

def ShowInstructions():
    # print the main menu and commands
    print("Fire Demon Hunt")
    print(
        "Collect all 6 Dragon Items items across the Castle in order to take on the Fire Demon, or Die by his hand.")
    print("Move commands: go South, go North, go East, go West")
    print("Add to Inventory: get 'item name'")


def Main():
        rooms = {#A dictionary for the simplified text game that links a room to other rooms.
            'Courtyard': {'Name': 'Courtyard', 'North': 'Armory', 'South': 'Kitchen', 'East': 'Hall', 'West': 'Foyer'},
            'Kitchen': {'Name': 'Kitchen', 'item': 'Dragon Wings', 'North': 'Courtyard', 'East': 'Storage'},
            'Storage': {'Name': 'Storage', 'item': 'Dragon Blade', 'West': 'Kitchen'},
            'Hall': {'Name': 'Hall', 'item': 'Dragon Shield', 'West': 'Courtyard', 'North': 'Bedroom'},
            'Bedroom': {'Name': 'Bedroom', 'item': 'Angels Kiss', 'South': 'Hall'},
            'Foyer': {'Name': 'Foyer', 'item': 'Book of Light', 'East': 'Courtyard'},
            'Armory': {'Name': 'Armory', 'item': 'Dragon Helm', 'South': 'Courtyard', 'West': 'Secret Room'},
            'Secret Room': {'Name': 'Secret Room', 'East': 'Armory'}
        }

    directions = ['north', 'south', 'east', 'west']
    current_room = 'Courtyard'
    Inventory = []
    ShowInstructions()

    while True:
        if current_room == 'Secret Room':
            if len(Inventory) == 6:
                print('Congratulations! You have reached the Secret Room and defeated the Fire Demon!')
                break
            else:
                print('You have reached the Secret Room but did not find all of the Items the Fire Demon laughs as he tears you to shreds.')
                print('You Lose!')
                break
        # displays the players current location
        print()
        print('You are in the {}.'.format(current_room))
        print('Inventory', Inventory)
        room_dict = rooms[current_room]
        if "item" in room_dict:
            item = room_dict["item"]
            if item not in Inventory:
               print("You look around and see a", item)


        # gets the users input
        command = input('What do you wish to do? ').split() # this controls the movement
        #movement
        if command[0] == 'go':
            if command[1] in directions:
                room_dict = rooms[current_room]
                if command[1] in room_dict:
                    current_room =room_dict[command[1]]
                else:
                    # bad movement
                    print('You run into the wall.')
            else:
                print("Invalid entry")

        # quit game
        elif command[0] in ['exit', 'quit']:
            print('Thanks for playing!')
            break
        # get item
        elif command[0] == 'get':
            if command[1] == item:
                Inventory.append(item)
                print(item, "collected")
            else:
                print('Invalid command')
        # bad command
        else:
            print('Invalid input')


Main()

问题原因

  1. 大小写不匹配:directions列表存储的是小写方向(north/south等),但rooms字典中的方向键是大写开头(North/South等)。用户输入go North时,command[1]为North,与directions中的小写不匹配,直接触发Invalid entry,根本没执行移动逻辑。
  2. 缩进错误:Main函数内directions、current_room等变量的缩进比rooms字典多一级,会导致语法错误,游戏无法正常运行。
  3. 未初始化变量:当房间没有item时,执行get命令会因item变量未定义报错。

修复后的代码

def ShowInstructions():
    # 打印主菜单和命令说明
    print("Fire Demon Hunt")
    print("收集城堡里的全部6件龙系物品,才能挑战火魔,否则会被他击杀。")
    print("移动命令:go South, go North, go East, go West")
    print("收集物品:get '物品名称'")


def Main():
    rooms = {# 房间关联字典,定义各房间的连接与物品
        'Courtyard': {'Name': 'Courtyard', 'North': 'Armory', 'South': 'Kitchen', 'East': 'Hall', 'West': 'Foyer'},
        'Kitchen': {'Name': 'Kitchen', 'item': 'Dragon Wings', 'North': 'Courtyard', 'East': 'Storage'},
        'Storage': {'Name': 'Storage', 'item': 'Dragon Blade', 'West': 'Kitchen'},
        'Hall': {'Name': 'Hall', 'item': 'Dragon Shield', 'West': 'Courtyard', 'North': 'Bedroom'},
        'Bedroom': {'Name': 'Bedroom', 'item': 'Angels Kiss', 'South': 'Hall'},
        'Foyer': {'Name': 'Foyer', 'item': 'Book of Light', 'East': 'Courtyard'},
        'Armory': {'Name': 'Armory', 'item': 'Dragon Helm', 'South': 'Courtyard', 'West': 'Secret Room'},
        'Secret Room': {'Name': 'Secret Room', 'East': 'Armory'}
    }

    directions = ['north', 'south', 'east', 'west']
    current_room = 'Courtyard'
    Inventory = []
    ShowInstructions()

    while True:
        if current_room == 'Secret Room':
            if len(Inventory) == 6:
                print('恭喜!你到达了密室并击败了火魔!')
                break
            else:
                print('你到达了密室,但没有集齐所有物品,火魔大笑着将你撕碎。')
                print('你输了!')
                break
        # 显示当前位置与背包
        print()
        print('你现在在{}。'.format(current_room))
        print('背包', Inventory)
        room_dict = rooms[current_room]
        item = None
        if "item" in room_dict:
            item = room_dict["item"]
            if item not in Inventory:
                print("你环顾四周,发现了一个", item)

        # 处理用户输入
        command = input('你想做什么? ').split()
        # 移动逻辑
        if command[0] == 'go':
            if len(command) < 2:
                print('请输入完整移动命令,比如:go North')
                continue
            input_dir = command[1].lower()
            if input_dir in directions:
                dir_key = input_dir.capitalize()
                if dir_key in room_dict:
                    current_room = room_dict[dir_key]
                else:
                    print('你撞到了墙上。')
            else:
                print("无效输入")

        # 退出游戏
        elif command[0] in ['exit', 'quit']:
            print('感谢游玩!')
            break
        # 收集物品
        elif command[0] == 'get':
            if len(command) < 2:
                print('请输入完整收集命令,比如:get Dragon Wings')
                continue
            if item and command[1] == item:
                Inventory.append(item)
                print(item, "已收集")
            else:
                print('无效命令')
        # 无效输入
        else:
            print('无效输入')


Main()

关键修复说明

  • 统一方向判断:将用户输入的方向转为小写验证,再转回大写开头匹配字典键,彻底解决大小写不匹配问题。
  • 修正缩进:调整Main函数内变量的缩进,确保语法正确。
  • 初始化item变量:默认设为None,避免无物品房间执行get命令时报错。
  • 增加输入长度校验:防止用户只输入go或get时出现索引错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.22 19:36:17