IT 140 项目 2 基于文本的游戏 - 库存和损失问题

问题描述 投票:0回答:1

我们被要求制作一个基于文本的游戏,在游戏中你必须穿过房间收集所有物品,然后才能面对恶棍。如果您在进入与恶棍一起的房间之前没有拥有所有物品,您就会失败。我的代码有问题。我可以移动一个房间到另一个房间,看看每个房间里有什么物品,我也可以看到它所在的房间里的恶棍。我无法拾取房间里的物品,也不知道我哪里出了问题,我也是不知道为什么当我和恶棍一起进入房间时,它并没有让我输。我是编码新手,因此我们将不胜感激。另外,这是我在这里发表的第一篇文章,我可以看到我的字典看起来格式很奇怪,但它确实适合 Pycharm。

rooms = {
    'Rec Room': {'North': 'Sleeping Quarters', 'South': 'Decontamination', 'East': 'Mess Hall',     'West': 'Med Bay',
                 'item': 'Nothing'},
    'Decontamination': {'North': 'Rec Room', 'South': 'Terrarium', 'East': 'Water Treatment', 'West': 'Lab',
                        'item': 'Empty Laser Weapon'},
    'Terrarium': {'North': 'Decontamination', 'item': 'The Alien!'},
    'Lab': {'North': 'Med Bay', 'East': 'Decontamination', 'item': 'Recording'},
    'Med Bay': {'North': 'Cargo Hold', 'South': 'Lab', 'East': 'Rec Room', 'item': 'Medical Supplies'},
    'Cargo Hold': {'South': 'Med Bay', 'East': 'Sleeping Quarters', 'item': 'MKIV Suit'},
    'Sleeping Quarters': {'South': 'Rec Room', 'East': 'Bathroom', 'West': 'Cargo Hold', 'item': 'Artifact'},
    'Bathroom': {'South': 'Mess Hall', 'West': 'Sleeping Quarters', 'item': 'MKV Helmet'},
    'Mess Hall': {'North': 'Bathroom', 'South': 'Water Treatment', 'West': 'Rec Room', 'item': 'Ammo'},
    'Water Treatment': {'North': 'Mess Hall', 'West': 'Decontamination', 'item': 'Shield Charge'}
}


# Add Directions Here
def show_instructions():
    print('---------------')
    print('Welcome to Lost Lab')  # Add Name of Game
    print('Collect all 8 Items to Win the Game, or Be Prepared to Face the Alien!')
    print('Move Commands: North, South, East, West')
    print('Add to Inventory: Get "Item Name" ')
    print('---------------')


# Start Player In Rec Room
starting_room = 'Rec Room'
current_room = starting_room

show_instructions()
inventory = []

# Start Loop Giving Status
while True:
    print('You are in the {}'.format(current_room))
    print('Inventory:', inventory)
    if 'item' in rooms[current_room]:
        print('You See ' + rooms[current_room]['item'])
    if len(inventory) != 8 and 'item' == 'The Alien!':
        print('You were not prepared, The Alien has defeated you!')
        break
    if len(inventory) == 8 and 'item' == 'The Alien!':
        print('You Did It! You have conquered The Alien!')
        break
    # Movement
    move = input('Enter Your Move: ').split()[-1].title()
    print('---------------')
    # Add .capitalize to make sure command works no matter what case is used

    # To Move To Next Room and Begin Item Pickup
    if move in rooms[current_room]:
        current_room = rooms[current_room][move]
    elif move.title() == ('Get' + rooms[current_room]['item']):
        if rooms[current_room]['item'] in inventory:
            print('You already have this item')
        else:
            inventory.append(rooms[current_room]['item'])

    # Invalid Direction
    else:
        print('Invalid Move, You can not go that way!'.format(move))
python pycharm
1个回答
0
投票
move = input('Enter Your Move: ').split()[-1].title()

当您以这种方式接受输入时,您只能获得输入的“最后一个单词”。这就是 [-1] 的作用。

因此,如果您输入“获取神器”,“获取”将被剥离,移动最终只是“神器”。

所以稍后当你将输入与

'Get' + rooms[current_room]['item']

进行比较时,当然不相等,因为

Get
不存在。
    

© www.soinside.com 2019 - 2024. All rights reserved.