繁体   English   中英

如何在基于文本的冒险游戏中添加项目并避免重复到我的库存?

[英]How can I add items and avoid duplicates to my inventory in a text based adventure game?

所以我正在尝试为我的一门课设计一款基于文本的冒险游戏。 我有从一个房间到另一个房间的移动,但是在将物品添加到库存中以进行逃生时一直在努力。 起初,当我尝试输入“get 'item'”时,系统提示我“无效移动”,这是因为我调用函数的顺序存在问题。 现在我已经修复了所有内容,这些物品会自动添加到库存中,而无需用户输入获取物品 function,并且每次您进入房间时,尽管该物品已经在库存中,但它正在被添加。 这是我的代码:

def show_instructions():
    print('Escape From Work Text Based Game')
    print('Movement commands: South, North, East, West')
    print("To pick up an item, type 'get [item]'")
    print()
    print('Your boss is asking you to come in on your days off. To escape without being seen, you will need to get your'
          ' vest, backpack, lunchbox, paycheck, name badge, and car keys.')
    print('Do not enter the room that has your boss, or you can kiss your days off goodbye!')


def player_stat(current_room, inventory):
    print('----------------------')
    print('You are in the {}'.format(current_room))
    print('Item in this room: {}'.format(rooms[current_room]['Item']))
    print('Inventory: ', inventory)
    print('----------------------')


def move(direction, current_room, rooms):
    if direction in rooms[current_room]:
        current_room = rooms[current_room][direction]
    else:
        print('You run into the wall, customers are staring. Try again.')
    return current_room


rooms = {
    'Garden Center': {'South': 'Hardware Dept.', 'West': 'Customer Service', 'North': 'HR Office',
                      'East': 'Storage Room', 'Item': 'None'},
    'Customer Service': {'East': 'Garden Center', 'Item': 'lunchbox'},
    'Hardware Dept.': {'East': 'Break Room', 'North': 'Garden Center', 'Item': 'backpack'},
    'HR Office': {'East': 'Lounge', 'South': 'Garden Center', 'Item': 'paycheck'},
    'Lounge': {'West': 'HR Office', 'Item': 'name badge'},
    'Break Room': {'West': 'Hardware Dept.'},
    'Storage Room': {'West': 'Garden Center', 'North': 'Admin Office', 'Item': 'vest'},
    'Admin Office': {'South': 'Storage Room', 'Item': 'keys'}
}

directions = ['North', 'East', 'South', 'West']
current_room = 'Garden Center'
inventory = []
show_instructions()

while current_room != 'Exit':
    player_stat(current_room, inventory)
    player_move = input('What is your command?: ').lower().title()
    if player_move == 'exit':
        current_room = 'Exit'
        print('Sorry to see you go, play again soon!')
    elif player_move in directions:
        current_room = move(player_move, current_room, rooms)

    item = rooms[current_room].get('Item')

    if item is not None and player_move == 'get ' + item:
        if item in inventory:
            print('You already have this item in your inventory!')
    else:
        inventory.append(item)

    if current_room == 'Break Room':
        print('Oh no! You got caught. Have fun working this weekend.')
        exit()
    else:
        print('Invalid move')

这就是我从几个房间搬走后 output 的样子。 我不明白为什么它在每次移动后都打印无效移动,即使它也是有效的。

You are in the HR Office
Item in this room: paycheck
Inventory:  ['paycheck', 'paycheck']
----------------------
Where would you like to go?: south
Invalid move
----------------------
You are in the Garden Center
Item in this room: None
Inventory:  ['paycheck', 'paycheck', 'None']
----------------------
Where would you like to go?: south
Invalid move
----------------------
You are in the Hardware Dept.
Item in this room: backpack
Inventory:  ['paycheck', 'paycheck', 'None', 'backpack']
----------------------

当你在收集完所有 6 个项目后获胜时,我还必须添加一种退出游戏的方法,并且我正朝着 'if inventory == [ all 6 items in list ]: print(you win) 然后退出的方向前进' 任何人提供的任何帮助都将不胜感激。

你的缩进是错误的。 你有这个:

if item is not None and player_move == 'get ' + item:
    if item in inventory:
        print('You already have this item in your inventory!')
else:
    inventory.append(item)

但我认为你想要这个:

if item is not None and player_move == 'get ' + item:
    if item in inventory:
        print('You already have this item in your inventory!')
    else:
        inventory.append(item)

尝试

#Loop continues if user has not entered 'exit' or inventory is not up to 6 items
while current_room != 'Exit' and len(inventory)!= 6:
    player_stat(current_room, inventory)
    player_move = input('What is your command?: ').lower().title()
    if player_move == 'Exit':
        current_room = 'Exit'
        print('Sorry to see you go, play again soon!')
        exit()
    elif player_move in directions:
        current_room = move(player_move, current_room, rooms)

    item = rooms[current_room].get('Item')
    #As the input has .title() this condition will accept input of item in that format
    if item is not None and player_move == 'Get ' + item.title():
        if item in inventory:
            print('You already have this item in your inventory!')
        #Was adding repeat items because of prior incorrect indentation
        #Correct indentation with the else applying after checking inventory
        else:
            inventory.append(item)

    if current_room == 'Break Room':
        print('Oh no! You got caught. Have fun working this weekend.')
        exit()
    #Replaced the 'else' statement with this so the console doesn't return 'Invalid move' every time
    if "get " + item not in player_move and player_move not in directions:
        print('Invalid move')
#When loop breaks, user will have won
print("You won!")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM