我提前道歉,因为我什至不确定自己如何问这个问题。我基本上是一名自学成才的程序员,我在项目的一部分中苦苦挣扎,这部分要求我将信息从一种方法传递到另一种方法,其中一个参数可能根本没有值(value)。
该项目是我正在开发的一款游戏。一种两人游戏,是一个非常基本的 PVP/骰子滚动战斗系统。在这个游戏中,玩家可以拥有某些专长,为自己提供某些奖励,或对对手施加某些惩罚。该专长一旦使用,就无法在同一游戏中再次使用,并且只能在玩家的回合中选择专长。
例如。专长“致残打击”将对对手的下一次掷骰施加-1惩罚,或者专长“泰坦打击”将对玩家的伤害施加+50%的加值。因此,我知道我需要确保该玩家的“defineHit”方法接受两个“featUsed”函数。例如,如果玩家一决定使用致残打击,这显然对他的立即攻击没有任何作用,但会影响玩家二的攻击掷骰。
问题是,专长是在玩家的回合中选择的。所以自然地,一开始,当playerOne运行时,他的usedFeat方法将填充适当的数据,但playerTwo的方法中将没有任何内容。
我在 JSON 中设置了完整的功能列表。该列表相当广泛,但我只会在此处放置与示例相关的部分。正如您将在 JSON 中看到的,只需要壮举的名称和“action”。名称显然就是名称,“ Action ”是用于以某种方式操纵玩家掷骰子的数值。
我还提供了两种相关方法以及任何可能有帮助的注释。
功能 JSON:
"titan blow": {
"stat": "strength",
"desc": "Your next attack deals 50% more damage, can only be used once per fight.",
"requirements": [13, 10, 0, 0, "greater crushing blow"],
"status": "active",
"action": 0.5
},
"crippling blow": {
"stat": "strength",
"desc": "You strike your opponent with such a force that it hinders their own attempts to strike back, given them a -1 to their next attack.",
"requirements": [3, 6, 4, 0, "none", 0],
"status": "active",
"action": -1
},
"improved crippling blow": {
"stat": "strength",
"desc": "improves crippling blow to -3 to their next attack.",
"requirements": [7, 8, 5, 0, "crippling blow"],
"status": "active",
"action": -3
},
"greater crippling blow": {
"stat": "strength",
"desc": "improves crippling blow to -5 to their next attack.",
"requirements": [9, 9, 6, 0, "improved crippling blow"],
"status": "active",
"action": -5
询问玩家要使用的专长的方法(注意:featDict是从加载和转储上面的专长JSON的模块中提取的。另请注意,有一个名为“pTwoFeatUsed(self)”的方法,基本上就是这个确切的代码,仅适用于玩家二):
def pOneFeatUsed(self):
featDict = gameFeats.featDict()[0]
feat = ""
while feat != "none":
feat = input("Do you wish to use a feat? (type full name of feat here, or 'none'): ")
if feat == 'power attack' or feat == 'combat expertise' or feat == 'defensive fighting' or feat == 'masochist':
print(feat + " is a passive feat, and will be determined after this.")
elif feat in self.pOneInfo['feats taken']:
print(featDict[0][feat]['action'])
return [feat, featDict[0][feat]['action']]
elif feat not in self.pOneInfo['feats taken'] and feat != "none":
print("Either you do not have that feat, or you did not type it correctly")
就像 featUsed 方法一样,有一个 'defineHitPTwo' 方法,其代码基本上完全相同,仅用于playerTwo 的攻击
def determineHitPOne(self):
pOneToHit = self.pOneInfo['hit']
pTwoAC = self.pTwoInfo['ac']
for word in self.pOneInfo["feats taken"]:
if word == "power attack":
self.pOnepMod = self.pOnePowerAttack()
if word == "combat expertise":
self.pOnecMod = self.pOneCombatExpertise()
if word == "defensive fighting":
self.pOnedMod = self.pOneDefensiveFighting()
if word == "masochist":
self.pOnemMod = self.pOneMasochist()
pMod = self.pOnepMod
cMod = self.pOnecMod
dMod = self.pOnedMod
mMod = self.pOnemMod
pTwodMod = self.pTwodMod
pTwomMod = self.pTwomMod
hit = random.randint(1, 20)
total = int(hit + pOneToHit - pMod + cMod - dMod + mMod)
print("Roll: " + str(hit) + " Base: " + str(pOneToHit) + " PA: " + str(pMod) + " CE: " + str(cMod) + " DF: " + str(dMod) + " MC: " + str(mMod))
totalAC = pTwoAC + pTwodMod - pTwomMod
print("P2 AC: " + str(pTwoAC) + " DF: " + str(pTwodMod) + " MC: " + str(pTwomMod))
if total >= totalAC:
print(self.playerOne + " rolled a " + str(total) + " to hit an AC " + str(totalAC) + " and was successful.")
self.pTwodMod = 0
self.pTwomMod = 0
self.determineDamagePOne()
else:
print(self.playerTwo + " rolled a " + str(total) + " to hit an AC " + str(totalAC) + " and missed.")
self.pTwodMod = 0
self.pTwomMod = 0
self.scoreboard()
我尝试过很多事情。我知道设置默认值来传递参数,但这似乎只适用于除列表和字典之外的任何内容。我尝试过:
def determineHitPOne(self, pOneFeatUsed = ["none", 0], pTwoFeatUsed = ["none", 0]):
尝试为变量分配默认列表,但是1)据我了解,这是不好的使用,因为数据是可变的,并且不会提供所需的结果? 2)这甚至没有调用所需的方法,如果我放入方法'pOneFeatUsed = self.pOneFeatUsed(),它显然会调用方法本身并执行它,而不是分配它的值。
我尝试过一种称为哨兵的东西:
def determineHitPOne(self, pOneFeatUsed = None, pTwoFeatUsed = None):
if pOneFeatUsed is None:
pOneFeatUsed = ["none", 0]
if pTwoFeatUsed is None:
pTwoFeatUsed = ["none", 0]
但是,我再次不确定如何调用该方法,以便获得其中分配的值(如果有的话),或者如果玩家还没有分配默认值 ["none", 0]还没有轮到,没有要分配的值。
如果(正如我怀疑的那样)这个解释清晰如泥,请索取更多信息。如果您需要更多信息,我将很乐意提供。
提前致谢。
最佳答案
当您以这种方式定义方法时:
def determineHitPOne(self, pOneFeatUsed=None, pTwoFeatUsed=None):
您将 pOneFeatUsed
定义为新变量,实质上忽略了 pOneFeatUsed
方法的存在。此语法不会调用您想要的方法,它将创建一个具有该名称的新变量,然后 A) 如果您没有为该参数提供值,则为其分配 None ,或者 B) 分配任何值您提供的。
相反,您可以存储其他游戏中上次使用的专长的状态,然后检查该字段。
def pOneFeatUsed(self):
featDict = gameFeats.featDict()[0]
self.p1_last_feat = None
while self.p1_last_feat is not None:
self.p1_last_feat = input("Do you wish to use a feat? (type full name of feat here, or 'none'): ")
if self.p1_last_feat == 'none':
self.p1_last_feat = None
elif self.p1_last_feat in ('power attack', 'combat expertise', 'defensive fighting', 'masochist'):
print(self.p1_last_feat + " is a passive feat, and will be determined after this.")
elif self.p1_last_feat in self.pOneInfo['feats taken']:
print(featDict[0][self.p1_last_feat ]['action'])
return [self.p1_last_feat , featDict[0][self.p1_last_feat ]['action']]
else self.p1_last_feat not in self.pOneInfo['feats taken']:
print("Either you do not have that feat, or you did not type it correctly")
然后,您不必再次尝试调用该方法,只需检查 last_feat
的状态
def determineHitPOne(self):
if self.p1_last_feat is None:
pOneFeatUsed = ["none", 0]
if self.p2_last_feat is None:
pTwoFeatUsed = ["none", 0]
或者,如果您愿意,您甚至可以显式存储这些列表。要点是调用该方法应该设置一个属性,然后您可以稍后检查该属性的状态。
关于python - 如何设置一种方法,其中传递的参数之一可能没有值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56301470/