繁体   English   中英

文字冒险-库存下降

[英]Text Adventure - Inventory Drop

我正在制作类似无赖的游戏,想知道是否存在允许玩家输入以下内容来丢弃物品的方法: Drop [Item Name]

Drop是命令,项目名称是您库存中的命令,例如,如果我有想要扔掉的石头,我会输入: Drop Rock

CO = "Rock"

Inventory = {"Slot 1" : "Empty","Slot 2" : "Empty","Slot 3" : "Empty","Slot 4" : "Empty","Slot 5" : "Empty"}

def DROP():

    Slot_Number = int(input("\nInventory Slot to drop: "))
    Slot_Number = str(Slot_Number)
    Slot_Number = ("Slot " + Slot_Number)
    CO = Inventory[Slot_Number]
    Inventory[Slot_Number] = "Empty"

由于您使用的是编号插槽(而不是“袋”,“口袋”),因此一种更简单的方法是使用清单作为库存。 您可以索引一个列表,并轻松按值在列表中查找项目。

我还建议您使用None或至少使用一个空字符串""来表示一个空插槽(因为它们都等于False

Inventory = ["","","","","","",""]

然后,您可以按以下方式调整功能:

def DROP():
    Slot_Number = int(input("\nInventory Slot to drop: "))
    Inventory[Slot_Number] = ""

请注意,由于Python索引是从零开始的,因此,如果要允许用户在第一个插槽中输入1 ,而不是0 ,则需要从提供的值中减去1。

def DROP():
    Slot_Number = int(input("\nInventory Slot to drop: "))
    Inventory[Slot_Number-1] = ""

要将清单打印在一个不错的清单中,可以使用类似以下的内容。 i or "Empty"协同结构可能对您来说是新的:

for n, i in enumerate(Inventory):
    print("%d - %s" % (n+1, i or "Empty"))

i or "Empty"我们利用一个空字符串的值falsey连同or shortcutting。 如果iTrue值中i将被显示,如果是False (例如空串)的值后的or将被代替打印。 没有这个的等效项是:

for n, i in enumerate(Inventory):
    if i:
        print("%d - %s" % (n+1, i))    
    else:
        print("%d - Empty" % (n+1))    

最后,给出一个drop_by_name函数的示例,在该示例中,您使用.index()在清单Inventory查找某物的位置并将其删除:

def drop_by_name():
    item_name = input('\nEnter the name of the item to drop: ')
    if item_name in Inventory:
        Slot_Number = Inventory.index(item_name)
        Inventory[Slot_Number] = ""

您可能需要看一下cmd ,它使命令处理更加容易

您可以为每个命令创建函数,然后调用这些函数

我将它比@mfitzp更进一步,并完成了全部一行代码,以便Drop Stick可以放下一根棍子。 这是我使用的代码:

def DROP(CHOICE):

    global NV

    NV = CHOICE[5:]
    str(NV)

CHOICE = input("\nWhat do you want to do (type Help for list of commands): ")

if CHOICE.startswith("Drop ") or CHOICE.startswith("drop ") or CHOICE.startswith("DROP "):
    DROP(CHOICE)
    if NV in I:
            SN = I.index(NV)
            I[SN] = ""
    CO = NV

暂无
暂无

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

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