0

0

如何在文本冒险游戏中将物品从房间放入背包

霞舞

霞舞

发布时间:2025-09-28 16:34:01

|

903人浏览过

|

来源于php中文网

原创

如何在文本冒险游戏中将物品从房间放入背包

本文档旨在解决在文本冒险游戏中,玩家无法将房间内的物品放入背包的问题。通过分析游戏代码,找出错误原因,并提供正确的代码示例,帮助开发者实现物品拾取功能,完善游戏逻辑。

理解游戏逻辑

在文本冒险游戏中,玩家通常通过输入指令与游戏世界互动。其中一个常见的功能就是拾取物品。实现这一功能需要以下几个关键步骤:

  1. 检测玩家输入的指令:判断玩家是否输入了拾取物品的指令。
  2. 确定要拾取的物品:获取玩家想要拾取的物品名称。
  3. 验证物品是否存在于当前房间:检查当前房间的物品列表中是否存在玩家想要拾取的物品。
  4. 将物品添加到玩家的背包:如果物品存在,则将其从房间的物品列表中移除,并添加到玩家的背包中。
  5. 更新游戏状态:显示更新后的房间和背包信息。

分析问题代码

在提供的代码中,问题主要出现在物品拾取的逻辑判断上。具体来说,以下代码存在错误:

if item in rooms(current_room):
    inventory_items.append(item)
else:
    print(f"There's no {item} here.")

这段代码存在两个问题:

  1. 使用圆括号访问字典:rooms(current_room) 错误地使用了圆括号来访问字典,这会导致 TypeError: 'dict' object is not callable 错误。正确的访问方式是使用方括号:rooms[current_room]。
  2. 未访问物品键:即使使用了方括号,if item in rooms[current_room] 仍然无法正确判断物品是否存在。因为 rooms[current_room] 返回的是一个包含房间所有信息的字典,而不是房间内的物品列表。要判断物品是否存在,需要访问该字典中的 item 键:rooms[current_room]['item']。

解决方案

要解决这个问题,需要修改代码如下:

笔启AI论文
笔启AI论文

专业高质量、低查重,免费论文大纲,在线AI生成原创论文,AI辅助生成论文的神器!

下载
if command == 'get':
    item = input('What do you want to take? ')
    if item == rooms[current_room]['item']:
        inventory_items.append(item)
        rooms[current_room]['item'] = 'None' # Remove item from room
        print(f"You picked up the {item}.")
    else:
        print(f"There's no {item} here.")

修改说明:

  1. 使用 rooms[current_room]['item'] 正确访问了当前房间的物品。
  2. 使用 item == rooms[current_room]['item'] 比较玩家输入的物品名称和房间中的物品名称。
  3. 在成功拾取物品后,将房间内的物品设置为 'None',表示该房间已没有物品。
  4. 添加了拾取成功后的提示信息。

完整代码示例

以下是包含修复后的物品拾取功能的完整代码示例:

def user_instructions():
    print('--------------')
    print('You are a monkey and wake up to discover your tribe is under attack by the Sakado tribe ')
    print('Your goal is to collect all 6 items and bring them to the Great Mother Tree to save the tribe!')
    print('Their life is in your hands!')
    print('\nMove through the rooms using the commands: "north", "east", "south", or "west"')
    print('Each room contains an item to pick up, use command: "(item name)"')
    print('\nDo not failure your tribe!')


# define command available for each room
rooms = {
    'Great Hall': {'east': 'Shower Hall', 'south': 'Armory Room', 'west': 'Bedroom', 'north': 'Chow Hall', 'item': 'Armor of the Hacoa Tribe'},
    'Bedroom': {'east': 'Great Hall', 'item': 'Tribe Map'},
    'Chow Hall': {'east': 'Bathroom', 'south': 'Great Hall', 'item': 'Golden Banana'},
    'Shower Hall': {'west': 'Great Hall', 'north': 'Branding Room', 'item': 'Sword of a 1000 souls'},
    'Bathroom': {'west': 'Chow Hall', 'item': 'None'},
    'Branding Room': {'south': 'Shower Hall', 'item': 'Sacred Key'},
    'Armory Room': {'north': 'Great Hall', 'east': 'Great Mother Tree', 'item': 'Spear of the Unprotected'},
    'Great Mother Tree': {'west': 'Armory', 'item': 'None'}
}


def user_status():  # indicate room and inventory contents
    print('\n-------------------------')
    print('You are in the {}'.format(current_room))
    print('In this room you see {}'.format(rooms[current_room]['item']))
    print('Inventory:', inventory_items)
    print('-------------------------------')


def invalid_move():
    print('Command not accepted, please try again')


def invalid_item():
    print('Item is not found in this room')
    user_status()


def show_status(current_room, inventory, rooms):
    print('   -------------------------------------------')
    print('You are in the {}'.format(current_room))
    print('Inventory:', inventory_items)
    print('   -------------------------------------------')


user_instructions()

inventory_items = []  # list begins empty
current_room = 'Bedroom'  # start in bedroom
command = ''

while current_room != 'Great Mother Tree':  # Great Mother Tree is the end of the game, no commands can be entered
    user_status()
    command = input('Enter your next move.\n').lower()

    if command == 'get':
        item = input('What do you want to take? ')
        if item == rooms[current_room]['item']:
            inventory_items.append(item)
            rooms[current_room]['item'] = 'None' # Remove item from room
            print(f"You picked up the {item}.")
        else:
            print(f"There's no {item} here.")

    elif command in rooms[current_room]:
        current_room = rooms[current_room][command]
    else:
        print('Invalid command')

if len(inventory_items) != 6:
    print('You Lose')

else:
    print('you win')

注意事项

  • 物品名称匹配:确保玩家输入的物品名称与房间中定义的物品名称完全一致(区分大小写)。
  • 错误处理:可以添加更完善的错误处理机制,例如,当玩家尝试拾取一个不存在的物品时,给出更详细的错误提示。
  • 游戏流程:在实际游戏中,可能需要更复杂的逻辑来处理物品拾取,例如,某些物品可能需要特定的条件才能拾取。
  • 用户体验:优化用户体验,例如,自动提示当前房间的物品名称,或者允许玩家使用物品编号来拾取物品。

总结

通过修改代码中的错误,并添加必要的逻辑,可以实现一个简单的物品拾取功能。在实际开发中,还需要根据游戏的具体需求进行扩展和优化。希望本文档能够帮助开发者更好地理解和实现文本冒险游戏的物品拾取功能。

相关专题

更多
if什么意思
if什么意思

if的意思是“如果”的条件。它是一个用于引导条件语句的关键词,用于根据特定条件的真假情况来执行不同的代码块。本专题提供if什么意思的相关文章,供大家免费阅读。

732

2023.08.22

Java 项目构建与依赖管理(Maven / Gradle)
Java 项目构建与依赖管理(Maven / Gradle)

本专题系统讲解 Java 项目构建与依赖管理的完整体系,重点覆盖 Maven 与 Gradle 的核心概念、项目生命周期、依赖冲突解决、多模块项目管理、构建加速与版本发布规范。通过真实项目结构示例,帮助学习者掌握 从零搭建、维护到发布 Java 工程的标准化流程,提升在实际团队开发中的工程能力与协作效率。

10

2026.01.12

c++主流开发框架汇总
c++主流开发框架汇总

本专题整合了c++开发框架推荐,阅读专题下面的文章了解更多详细内容。

106

2026.01.09

c++框架学习教程汇总
c++框架学习教程汇总

本专题整合了c++框架学习教程汇总,阅读专题下面的文章了解更多详细内容。

64

2026.01.09

学python好用的网站推荐
学python好用的网站推荐

本专题整合了python学习教程汇总,阅读专题下面的文章了解更多详细内容。

139

2026.01.09

学python网站汇总
学python网站汇总

本专题整合了学python网站汇总,阅读专题下面的文章了解更多详细内容。

13

2026.01.09

python学习网站
python学习网站

本专题整合了python学习相关推荐汇总,阅读专题下面的文章了解更多详细内容。

19

2026.01.09

俄罗斯手机浏览器地址汇总
俄罗斯手机浏览器地址汇总

汇总俄罗斯Yandex手机浏览器官方网址入口,涵盖国际版与俄语版,适配移动端访问,一键直达搜索、地图、新闻等核心服务。

93

2026.01.09

漫蛙稳定版地址大全
漫蛙稳定版地址大全

漫蛙稳定版地址大全汇总最新可用入口,包含漫蛙manwa漫画防走失官网链接,确保用户随时畅读海量正版漫画资源,建议收藏备用,避免因域名变动无法访问。

480

2026.01.09

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Go 教程
Go 教程

共32课时 | 3.6万人学习

Go语言实战之 GraphQL
Go语言实战之 GraphQL

共10课时 | 0.8万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号