基于Python文本的游戏项目

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

为了让代码运行良好,我在这项作业上付出了很大的努力,但我无法弄清楚我的代码出了什么问题。我尝试实现“get”命令,让玩家实际上能够拾取房间中存在的物品,但它不会将其添加到我的库存空列表中,最重要的是,输出因为该物品只是“物品”,而不是房间中存在的物品。由于时间限制,我被迫提交无法正常运行的项目,但我将感谢任何和所有反馈,让我学习我的错误。

main_menu = 'Move commands: North, East, South, West, Exit.'

def showinstructions():
    print('Death to the Lich')  # Name of game
    print('Collect all items in order to defeat the evil Lich in his spooky Mansion')  # Goal of game
    print(main_menu)  # Show move commands
    print("Add item to inventory: get 'item name'")  # Add item to inventory: get 'item name'


def player_status(inventory, location, rooms):
    print('Inventory:', inventory)
    print('YOu are in here:', location)
    if 'item' in rooms[location]:
        print('Ah, look what you have found!', rooms[location]['item'])

def main():
    inventory = []
    rooms = {
        'Living Room': {'South': 'Bedroom', 'North': 'Dungeon', 'East': 'Alchemy Room', 'West': 'Library'},
        'Bedroom': {'North': 'Living Room', 'East': 'Cellar', 'item': 'Shiny Armor'},
        'Cellar': {'West': 'Bedroom', 'item': 'Shiny Helmet'},
        'Dungeon': {'South': 'Living Room', 'East': 'Gallery', 'item': 'Silver Sword'},
        'Gallery': {'West': 'Dungeon', 'item': 'Magic Potion'},
        'Alchemy Room': {'West': 'Living Room', 'North': 'Dining Room', 'item': 'Silver Sword'},
        'Dining Room': {'South': 'Alchemy Room', 'item': 'Shiny Gloves'},
        'Library': {'East': 'Living Room', 'item': 'Lich'}
    }


    # The main function:
    location = 'Living Room'

    showinstructions()




    while True:
        print('\nYou are in the', location)

        move_options = rooms[location].keys()
        print('Move options:', *move_options)

        command = input('Which direction will you go?').strip()
        print('You entered:', command)

        if 'item' in 'location':
            print('You see this in the room:', rooms[location]['item'])
        if command == 'get':
            item = input()
            inventory.append(item)
        else:
            print('There is no item here')

        if location == 'Library':
            print('I am the Evil Lich! Prepare to die!')
            if len(inventory) >= 6:
                print('You are the hero of these Lands! YOU WON!')
            else:
                print('Sadly you werent not strong enough to defeat the Lich. YOU LOSE')
                break
        if command == "Exit":
            print('Thanks for playing!')
            break
        elif command in move_options:
            location = rooms[location][command]
        else:
            print('Wrong direction!')



main()
python python-3.x pycharm
1个回答
0
投票

您应该将当前房间中的项目附加到列表中,而不是将用户的输入作为项目附加到列表中,因为每个房间似乎只有一个项目,所以您的 get if 分支应该如下所示:

if command == 'get':
    inventory.append(rooms[location]['item'])
© www.soinside.com 2019 - 2024. All rights reserved.