为卡片游戏创建连续效果

2024-05-23 19:04:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图找出一些流行游戏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

非常感谢你们的帮助()


Tags: selffieldinitdefclasshealths2effect
3条回答

你不需要一段时间的循环,这会持续降低所有的法力消耗到0。你可能需要一个完全不同的变量来控制增益/减益。你知道吗

例如:

spell_manacost_modifier = 0

class Spell:
    def __init__(self,manacost):
        self.manacost = manacost

def calculate_cost(spell):
    final_cost = spell.manacost + spell_manacost_modifier
    return final_cost if final_cost >=0 else 0

s1 = Spell(1)
s2 = Spell(2)
s3 = Spell(3)

hand = [s1,s2,s3]

# this specific if statement should be run when the turn starts, after of course setting spell_manacost_modifier to 0
if creature in field:
    spell_manacost_modifier -= 1

for spell in hand:
    print(calculate_cost(spell))

希望这有帮助!你知道吗

Python看起来确实与英语/伪代码非常相似,但在本例中,含义不同。当您标记问题时,构造while <condition>: <looping code>是一个循环,这意味着当程序到达这一点时,只要条件为真,它就会继续运行包装的代码。你知道吗

在本例中,如果不是因为operator precedence的问题,您的代码将是一个无限循环。你所写的被解释为:

while creature in (field == True):
    for spell in hand:
        if spell.manacost >=1:
            spell.manacost -= 1

它永远不会运行,而不是

while (creature in field) == True:
    for spell in hand:
        if spell.manacost >=1:
            spell.manacost -= 1

或同等地

while creature in field:
    for spell in hand:
        if spell.manacost >=1:
            spell.manacost -= 1

它们都是无限循环,将永远运行。你知道吗


你需要的是一个函数,它根据原始的魔法消耗和当前的修正值来计算当前的魔法消耗,就像阿托建议的那样。你知道吗

一种方法是作为Spell类中的新方法,如下所示:

class Spell:
    def current_manacost(self, field, creature):
        if creature in field:
            return self.manacost - 1
        else:
            return self.manacost

    def __init__(self,manacost):
        self.manacost = manacost

print([x.current_manacost(field, creature) for x in hand])

Try it online!

计算为布尔值的表达式不需要与True或False进行比较。你知道吗

>>> a = [1,2,3]
>>> 1 in a == True
False

a == True1 in a之前被求值。参考6.16 Operator Precedence。你知道吗

>>> 1 in a
True
>>> a == True
False

>>> (1 in a) == True
True
>>>

更改:

while creature in field == True

while creature in field:

相关问题 更多 >