首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在一个房间里展示特定的物品?

如何在一个房间里展示特定的物品?
EN

Stack Overflow用户
提问于 2022-07-30 17:44:31
回答 1查看 74关注 0票数 2

我正在学习脚本类,我们正在使用python。我的最后一个项目是一个基于文本的游戏,我很难弄清楚如何才能让一个项目出现,这取决于用户在哪个房间里,以及在一个项目列表中。我已经设置了一个函数,允许用户将项目添加到库存中,但是我只能在每个房间中显示一个项目,或者在每个房间中显示所有的项目。

代码语言:javascript
复制
rooms = {
    'Your bedroom': {
        'north': 'Alley Way'
    },
    'Alley Way': {
        'north': 'Main Hall',
        'east': 'Bar',
        'west': 'Ammo Store',
        'south': 'Your bedroom',
        'item': 'Cloaking Device'
    }, 
    'Bar': {
        'west': 'Alley Way'
    },
    'Ammo Store': {
        'east': 'Alley Way',
        'item': 'Gun'
    },        
    'Main Hall': {
        'north': 'Oval Office',
        'west': 'West Wing',
        'east': 'East Wing',
        'south': 'Alley Way'
    },         
    'East Wing': {
        'west': 'Main Hall'
    },         
    'West Wing': {
        'east': 'Main Hall'
    },         
    'Oval Office': {
        'south': 'Main Hall'
    }     
}

items = ['Cloaking Device', 'Whiskey', 'Gun', 'Noise Maker', 'Key', 'Rocket Launcher']

def player_stat():
    print("-" * 20)
    print('You are in the {}'.format(location))
    print('You have {} in your inventory.'.format(inventory))
    print("-" * 20)

def get_item():
    command = input('Would you like to pick up the {}?').format(items).strip().lower()
    if command == 'yes':
        inventory.append(items)
        print('{} has been added to your inventory'.format(items))

location = 'Your bedroom'
item = 'item'
inventory = []
direction = ''
while direction != 'exit':
    if location == 'Your bedroom':
        print('\nYou are currently in', location)

    possible_moves = rooms[location].keys()
    print('You can move:', *possible_moves)

    direction = input('What will yor next move be? ').strip().lower()
    print('You entered:', direction)

    if direction in rooms[location]:
        location = rooms[location][direction]
        if location == 'Alley Way':
            player_stat()
            print('You have left your room and entered the Alley Way.', '\n'
                  'You notice some alien guards roughing up a homeless man.', '\n'
                  'While wrangling the homeless man, you notice one of the guards drop their cloaking device.')
            if item:
                print('You see a {}'.format(items[0]))
            get_item()

input('Would you like to pick up the {}?').format(items)是我试图只选择应该在每个房间的项目,但我似乎不知道如何。

EN

回答 1

Stack Overflow用户

发布于 2022-07-30 18:03:47

如果我正确理解的话,每个房间都有一个项目。就像现在一样,你的物品和它们所在的房间之间没有任何联系,那么你怎么知道该展示哪一个呢?

您可以将每个项目添加到相关位置的房间-dict中,这样您就可以按è.g访问它。item = rooms[location]["item"]与您当前所在的位置(具体的语法可能是不同的,这取决于您的房间-dict是如何构造的)。然后,您可以将该项作为输入传递给get_item-function,然后再进行其他操作。

另外,如果您使用python-3.6或更高版本,您可能会对F-字符串感兴趣。它们使字符串格式更具可读性,特别是当您有多个变量时:

代码语言:javascript
复制
print(f'You have {inventory} in your inventory.')
vs.
print('You have {} in your inventory.'.format(inventory))
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73177844

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档