我试图找出一些流行游戏Hearthstone的卡片的代码,并用Python复制它。我说炉石,但这基本上可以适用于每一个纸牌游戏,如魔术的聚会等。。。 假设我在场上有一个生物:
class Creature:
def __init__(self,manacost,atk,health):
self.manacost = manacost
self.atk = atk
self.health = health
creature = Creature(2,3,2)
field = []
field.append(creature)
现在,这个生物有一个连续的效果,比如“你手上的法术花费减少1”。这意味着,当卡牌在战场上时,我的法术费用将减少1,当卡牌离开战场时,它们将恢复到原来的费用。 然后我写道:
class Spell:
def __init__(self,manacost):
self.manacost = manacost
s1 = Spell(1)
s2 = Spell(2)
s3 = Spell(3)
hand = [s1,s2,s3]
while creature in field == True:
for spell in hand:
if spell.manacost >=1:
spell.manacost -= 1
一开始我希望我所有的咒语都manacost
等于0
。
结果什么也没发生。
打字
[x.manacost for x in hand]
我得到了[1,2,3]
,这与我在开头为咒语设置的manacost
完全相同。我该怎么办?你知道吗
最后这里是我的代码修复(我不得不张贴在这里,而不是在答案,因为我没有足够的字符在回复槽。你知道吗
class Creature:
def __init__(self,manacost,atk,health):
self.manacost = manacost
self.atk = atk
self.health = health
def continuous_effect(self):
global spellcostdifference
spellcostdifference += 1
def continuous_effect_cancel(self):
global spellcostdifference
spellcostdifference -= 1
class Spell:
def __init__(self,manacost):
self.manacost = manacost
def current_manacost(self):
global spellcostdifference
return self.manacost - spellcostdifference if spellcostdifference <= self.manacost else 0
spellcostdifference = 0
creature = Creature(2,3,2)
s1 = Spell(1)
s2 = Spell(2)
s3 = Spell(3)
hand = [s1,s2,s3]
creature.continuous_effect() ## this means the creature is on the field
creature.continuous_effect_cancel() ## this means the creature left the field or got its effect negated
非常感谢你们的帮助()
你不需要一段时间的循环,这会持续降低所有的法力消耗到0。你可能需要一个完全不同的变量来控制增益/减益。你知道吗
例如:
希望这有帮助!你知道吗
Python看起来确实与英语/伪代码非常相似,但在本例中,含义不同。当您标记问题时,构造
while <condition>: <looping code>
是一个循环,这意味着当程序到达这一点时,只要条件为真,它就会继续运行包装的代码。你知道吗在本例中,如果不是因为operator precedence的问题,您的代码将是一个无限循环。你所写的被解释为:
它永远不会运行,而不是
或同等地
它们都是无限循环,将永远运行。你知道吗
你需要的是一个函数,它根据原始的魔法消耗和当前的修正值来计算当前的魔法消耗,就像阿托建议的那样。你知道吗
一种方法是作为
Spell
类中的新方法,如下所示:Try it online!
计算为布尔值的表达式不需要与True或False进行比较。你知道吗
a == True
在1 in a
之前被求值。参考6.16 Operator Precedence。你知道吗更改:
至
相关问题 更多 >
编程相关推荐