我正在嘗試使用 python 制作 RPG 游戲,但我遇到了問題,因為我必須允許用戶只接收一次某個專案。如果用戶第一次與 NPC 互動,他們應該會收到一個物品,如果用戶再次與同一個 NPC 互動,他們應該會收到一個報價。如果他們多次互動,有什么辦法可以防止 NPC 給用戶同樣的東西嗎?
def function():
print("What do you want to do?")
userInput = input("1. Talk to the blacksmith \n2. Leave\n")
if userInput == "1":
# if the user hasnt already received the sword:
print("the BLACKSMITH gifts you a sword")
else:
print("Hi, how can I help?")
function()
function()
uj5u.com熱心網友回復:
當然,有多種方式。
你可以有一個可變函式,那么你的代碼看起來像這樣:
def function(given_items=[]):
print("What do you want to do?")
userInput = input("1. Talk to the blacksmith \n2. Leave\n")
if userInput == "1":
# if the user hasnt already received the sword:
if not "sword" in given_items:
print("the BLACKSMITH gifts you a sword")
given_items.append("sword")
else:
print("Hi, how can I help?")
function()
function()
關于可變引數的警告:https : //florimond.dev/en/posts/2018/08/python-mutable-defaults-are-the-source-of-all-evil/
或者你可以有一個全域標志或陣列,例如:
GIVEN_ITEMS = []
# SWORD_GIVEN = FALSE # You could also use a boolean instead of a list, the advantage with a list is that it allows you to keep track of all such items instead of having a boolean flag for every single item.
def function():
print("What do you want to do?")
userInput = input("1. Talk to the blacksmith \n2. Leave\n")
if userInput == "1":
# if the user hasnt already received the sword:
if not "sword" in GIVEN_ITEMS:
print("the BLACKSMITH gifts you a sword")
GIVEN_ITEMS.append("sword")
else:
print("Hi, how can I help?")
function()
function()
uj5u.com熱心網友回復:
如果劍是假的,那么玩家將收到一個禮物并記錄一個條目,劍是真的(存在)。在 if 函式“和”中,確保兩個條件都為真,即劍是假的。
Sword = False
def function():
print("What do you want to do?")
userInput = input("1. Talk to the blacksmith \n2. Leave\n")
if userInput == "1" and Sword == False:
# if the user hasnt already received the sword:
print("the BLACKSMITH gifts you a sword")
Sword = True:
else:
print("Hi, how can I help?")
function()
function()
uj5u.com熱心網友回復:
您還可以將更改整個實作function()作為通過閉包執行它的一部分。這里的優點可能是方法本身會跟蹤正確的實作,而不是使用“全域”變數。
## -------------------------------
## A closure that encapsulates some internal methods and when called
## returns one of them that can be assigned to a more global variable
## that can then be called.
## -------------------------------
def build_blacksmith_conversation():
## -------------------------------
## The implementation we want if this is the first time
## -------------------------------
def first_time():
nonlocal active ## this variable is defined in the outer context
print("What do you want to do?")
userInput = input("1. Talk to the blacksmith \n2. Leave\n")
if userInput == "1":
print("the BLACKSMITH gifts you a sword\n\n")
## -------------------------------
## since you have the sword, reset the active implementation
## -------------------------------
active = not_first_time
## -------------------------------
## -------------------------------
## -------------------------------
## The implementation we want if this is not first time
## -------------------------------
def not_first_time():
print("What do you want to do?")
userInput = input("1. Talk to the blacksmith \n2. Leave\n")
if userInput == "1":
print("Hi, how can I help?\n\n")
## -------------------------------
## -------------------------------
## Set the active implementation
## -------------------------------
active = first_time
## -------------------------------
## -------------------------------
## return a method that when called calls whatever the "active" method is
## -------------------------------
return lambda : active()
## -------------------------------
## -------------------------------
## -------------------------------
## Create a new method blacksmith_conversation() that will initially use
## one implementation but after the sword is given will switch to a new one
## -------------------------------
blacksmith_conversation = build_blacksmith_conversation()
## -------------------------------
## -------------------------------
## Give the sword
## -------------------------------
blacksmith_conversation()
## -------------------------------
## -------------------------------
## offer help
## -------------------------------
blacksmith_conversation()
## -------------------------------
這將為您提供可能如下所示的輸出:
What do you want to do?
1. Talk to the blacksmith
2. Leave
1
the BLACKSMITH gifts you a sword
What do you want to do?
1. Talk to the blacksmith
2. Leave
1
Hi, how can I help?
uj5u.com熱心網友回復:
有很多方法可以實作這一點:
你可以在你的函式之外有一個變數,比如:
Temp = True
并在您的函式中進行類似于此的變數更改:
global Temp
Temp = False
所以實際的代碼是:
Temp = True
def function():
print("What do you want to do?")
userInput = input("1. Talk to the blacksmith \n2. Leave\n")
if userInput == "1":
# if the user hasnt already received the sword:
print("the BLACKSMITH gifts you a sword")
else:
print("Hi, how can I help?")
global Temp
Temp = False
while True:
if Temp != False:
function()
else:
print(Temp)
break
現在,while 將尋找變數更改并相應地執行回圈迭代
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/391579.html
