簡體   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