Python类口袋妖怪之战

时间:2016-11-29 19:19:42

标签: python class object

嘿stackoverflow用户,

我正在尝试为口袋妖怪之战写一个Python类。我关注了帖子here,但我正在尝试整合不同的口袋妖怪打字。例如,火,草和水,让它们像在游戏中一样发挥作用。例如,火对草很有效。我不确定是否应该为类型添加另一个函数或在构造函数中定义类型,并编写一系列确定有效性或无效性的嵌套语句。

class Pokemon(object):
        def __init__(self, name, HP, Damage, type):
        self.name = name     #Sets the name of the Pokemon
        self.HP = HP         #The Hit Points or health of this pokemon
        self.Damage = Damage #The amount of Damage this pokemon does every     attack
        self.type = type #Determines the type of the pokmeon to factor in effectiveness

    def Battle(self, Opponent):
        if self.type == 'grass':
            self.Damage = self.Damage * 1.35

        if(self.HP > 0): #While your pokemon is alive it will coninute the Battle
            print("%s did %d Damage to %s"%(self.name, self.Damage, Opponent.name)) #Text-based combat descriptors
            print("%s has %d HP left"%(Opponent.name,Opponent.HP)) #Text-based descriptor for the opponent's health

        Opponent.HP -= self.Damage #The damage you inflict upon the opponent is subtracted here
        return Opponent.Battle(self)  #Now the Opponent pokemon attacks
    else:
        print("%s wins! (%d HP left)" %(Opponent.name, Opponent.HP)) #declares the winner of the Battle
        return Opponent, self  #return a tuple (Winner, Loser)

Squirtle = Pokemon('Squirtle', 100, 5, 'water')
Bulbasaur = Pokemon('Bulbasaur', 100, 10, 'grass')
Winner, Loser = Bulbasaur.Battle(Squirtle)

3 个答案:

答案 0 :(得分:2)

您的问题似乎可以通过嵌套方式[1]填充Python的dict数据类型[2]来优雅地解决:

attacking = {'fire': {'fire': 0.5, 'grass': 2.0, 'water': 0.5}, 'grass': {'fire': 0.5, 'grass': 0.5, 'water': 2.0}, 'water': {'fire': 2.0, 'grass': 0.5, 'water': 0.5}}

在第一种类型攻击时为弱点创建嵌套字典。然后,您可以访问火灾类型的乘数,攻击草类型,如下所示:

attacking['fire']['grass'] # yields 2.0

作为旁注:你用这条指令覆盖了口袋妖怪的伤害:self.Damage = self.Damage * 1.35。只要您没有例如Squirtle战斗首先是Bulbasaur,然后是地面型Pokemon,Squirtle具有效力1.当你开始与Charmander的战斗时,Squirtle的伤害将是5*2.0*1.0 = 10.0,这不是等于应该处理的损害(5.0)。如果您创建了一个新变量(turnDamage = self.Damage * attack[self.type][Opponent.type],那么这不会有问题。

又一个编辑:这个实际上对我有用:

class Pokemon(object):
    attackingDict = {'fire': {'fire': 0.5, 'grass': 2.0, 'water': 0.5}, 'grass': {'fire': 0.5, 'grass': 0.5, 'water': 2.0}, 'water': {'fire': 2.0, 'grass': 0.5, 'water': 0.5}}
    def __init__(self, name, HP, Damage, type):
        self.name = name     #Sets the name of the Pokemon
        self.HP = HP         #The Hit Points or health of this pokemon
        self.Damage = Damage #The amount of Damage this pokemon does every     attack
        self.type = type #Determines the type of the pokmeon to factor in effectiveness

    def Battle(self, Opponent):
        attackDamage = self.Damage * self.attackingDict[self.type][Opponent.type]

        if(self.HP > 0): #While your pokemon is alive it will coninute the Battle
            print("%s did %d Damage to %s"%(self.name, attackDamage, Opponent.name)) #Text-based combat descriptors
            print("%s has %d HP left"%(Opponent.name,Opponent.HP)) #Text-based descriptor for the opponent's health

            Opponent.HP -= attackDamage #The damage you inflict upon the opponent is subtracted here
            return Opponent.Battle(self)  #Now the Opponent pokemon attacks
        else:
            print("%s wins! (%d HP left)" %(Opponent.name, Opponent.HP)) #declares the winner of the Battle
            return Opponent, self  #return a tuple (Winner, Loser)


Squirtle = Pokemon('Squirtle', 100, 5, 'water')
Bulbasaur = Pokemon('Bulbasaur', 100, 10, 'grass')
Winner, Loser = Bulbasaur.Battle(Squirtle)

乘数是类的内在因素(即每个小宠物遵守相同的规则),因此字段attackingDict属于类。

答案 1 :(得分:0)

听起来你想要比较tuples = {(k, vv) for k, v in my_dict.items() for vv in v} 函数中每个神奇宝贝的type。您需要编写自己的比较类型的函数。天真的方式是使用Battleif

elif

此函数将返回伤害乘数,您可以像def typecomp(type1, type2): if type1 == 'fire': if type2 == 'grass' return 2.0 elif ... elif type1 == ...

一样使用它

答案 2 :(得分:0)

我只想提一下您的神奇宝贝定义系统不是很理想:

Squirtle = Pokemon('Squirtle', 100, 5, 'water')
Bulbasaur = Pokemon('Bulbasaur', 100, 10, 'grass')

- 我这样做的方法是制作一个名为pokemon.txt的.txt文件,其中包含所有数据,我在Ruby中完成了工作,但您可以使其在Python中工作