📜  python 制作商店菜单 - Python 代码示例

📅  最后修改于: 2022-03-11 14:46:12.835000             🧑  作者: Mango

代码示例1
# this is my own work from a fun project of a text based game I am making

def shopkeep(player_gold, player_inv, general_store_inv):
    for x in range(8):
        print()
    print("Shopkeeper: Welcome to my humble shop for now!")
    time.sleep(0.5)
    shopkeep_leave = False
    while shopkeep_leave is False:
        x2 = 0
        print("\nShopkeeper: Care to peruse my wares?\n")
        time.sleep(1)
        print("Shopkeeper: The upgraded sword and shield are 5 gold each, everything else is 2 gold.")
        print("Gold in coin purse: " + str(player_gold))
        for x in general_store_inv:
            time.sleep(0.2)
            print(str(x2 + 1) + ". " + str(general_store_inv[x2]))
            x2 += 1
        shop_purchase = input("""Please select:
            > """)
        if shop_purchase == "":
            shop_purchase = "1"
        int_choice = int(shop_purchase) - 1
        if x2 < 1:
            print("Shopkeeper: Sorry lad, just realised we're out of stock!")
            time.sleep(1)
            print("You leave the store")
            shopkeep_leave = True
            return player_gold, player_inv, general_store_inv
        if int_choice <= x2:
            print("You want to buy: " + str(general_store_inv[int_choice]) + " is this correct?\nY/N")
            confirm = input("   > ")
            if "Y" in confirm.upper():
                if "apple" in str(general_store_inv[int_choice]):
                    if player_gold >= 2:
                        time.sleep(0.5)
                        print("You pay 2g and obtain an apple!")
                        player_inv.append("apple")
                        general_store_inv.remove("apple")
                        player_gold -= 2
                    else:
                        print("Shopkeep: Looks like you can't afford that!")
                elif "sword2" in str(general_store_inv[int_choice]):
                    if player_gold >= 5:
                        time.sleep(0.5)
                        print("You pay 5g and obtain an upgraded sword!")
                        player_inv.append("sword2")
                        general_store_inv.remove("sword2")
                        player_gold -= 5
                    else:
                        print("Shopkeep: Looks like you can't afford that!")
                elif "sword" in str(general_store_inv[int_choice]):
                    if player_gold >= 2:
                        time.sleep(0.5)
                        print("You pay 2g and obtain a basic sword!")
                        player_inv.append("sword")
                        general_store_inv.remove("sword")
                        player_gold -= 2
                    else:
                        print("Shopkeep: Looks like you can't afford that!")
                elif "shield" in str(general_store_inv[int_choice]):
                    if player_gold >= 5:
                        time.sleep(0.5)
                        print("You pay 5g and obtain a shield!")
                        player_inv.append("shield")
                        general_store_inv.remove("shield")
                        player_gold -= 2
                    else:
                        print("Shopkeep: Looks like you can't afford that!")
                elif "brown coat" in str(player_inv[int_choice]):
                    if player_gold >= 2:
                        time.sleep(0.5)
                        print("You pay 2g and obtain a brown coat!")
                        player_inv.append("brown coat")
                        general_store_inv.remove("brown coat")
                        player_gold -= 2
                    else:
                        print("Shopkeep: Looks like you can't afford that!")
            elif confirm == "":
                print("Shopkeeper: I take it you didn't want that then")
                time.sleep(2)
            else:
                confirm = input("Shopkeeper: Do you want to exit?\n    > ")
                if "Y" in confirm.upper():
                    shopkeep_leave = True
                    return player_gold, player_inv, general_store_inv

    return player_gold, player_inv, general_store_inv