更新类对象列表

时间:2019-10-20 13:39:57

标签: python oop singleton

我在类级别有一个对象列表,还有一个类方法fight,该方法使用该列表中的对象:

from Heroes import Hero
from Creatures import Creature
from random import randint

class Battle():
    """template to simulate the battle"""


    obj_list = []

    def __init__(self,obj1_name,obj2_name):

        #create  hero
        self.obj1 = Hero(obj1_name, health = randint(70,100), strength = randint(70,80), defence = randint(45,55), speed = randint(40,50), luck = randint(10,30))
        Battle.obj_list.append(self.obj1)



        #create  creature
        self.obj2 = Creature(obj2_name, health = randint(70,100), strength = randint(70,80), defence = randint(45,55), speed = randint(40,50), luck = randint(10,30))
        Battle.obj_list.append(self.obj2)

    @classmethod
    def fight(cls):
        """simulate fight"""

        # first attack is landed by the obj with highest speed
        if cls.obj_list[0].speed > cls.obj_list[1].speed:

           cls.obj_list[1].health =  cls.obj_list[0].attack() - cls.obj_list[1].defence

        # same speed first attack landed by the obj with highest luck
        elif cls.obj_list[0].speed == cls.obj_list[1].speed:

            if cls.obj_list[0].luck >= cls.obj_list[1].luck:
                cls.obj_list[1].health =  cls.obj_list[0].attack() - cls.obj_list[1].defence
            else:
                cls.obj_list[0].health =  cls.obj_list[1].attack() - cls.obj_list[0].defence

        #self.obj1.speed < self.obj2.speed
        else:
                cls.obj_list[0].health =  cls.obj_list[1].attack() - cls.obj_list[0].defence

        return cls.obj_list

if __name__ == "__main__":

    first_round =  Battle("Icarus","Beast")


    for i in Battle.obj_list:
            print(i.name,i.health)

    Battle.fight()


    for i in Battle.obj_list:
            print(i.name,i.health)

    Battle.fight()

    for i in Battle.obj_list:
            print(i.name,i.health)

输出:

Icarus 90
Beast 74
Icarus 30
Beast 74
Icarus 30
Beast 74

第一次调用fight方法可以正确更新health中对象的obj_list属性,但是无论我调用方法health属性的次数是多少它不再更新了。我缺少什么,是否应该使用另一种方法共享对象的状态?

1 个答案:

答案 0 :(得分:0)

使用class属性作为英雄和生物的存储是不必要的,并且使代码不可读。

按照您的方法,经过三场战斗,Battle.obj_list看起来像这样:

[英雄(第一战),生物(第一战),英雄(第二战),生物(第二战),英雄(第三战),生物(第三战)。

在您的代码中,您始终访问[0]和[1]。

接下来的事情是您在战斗中创造英雄和生物。 您可以像在“现实”中那样想象:英雄和生物存在于战斗之前,战斗开始时(创建战斗实例)进入战斗。

class Battle():
    """template to simulate the battle"""
    def __init__(self, hero, creature):

        #create  hero
        self.hero = hero

        #create  creature
        self.creature = creature

    def fight(self):
        """simulate fight"""

        # first attack is landed by the obj with highest speed
        if self.hero.speed > self.creature.speed:
           self.creature.health =  self.hero.attack() - self.creature.defence

        # same speed first attack landed by the obj with highest luck
        elif self.hero.speed == self.creature.speed:
            if self.hero.luck >= self.creature.luck:
                self.creature.health =  self.hero.attack() - self.creature.defence
            else:
                self.hero.health =  self.creature.attack() - self.hero.defence

        #self.obj1.speed < self.obj2.speed
        else:
            self.hero.health =  self.creature.attack() - self.hero.defence
        return [self.hero, self.creature]


if __name__ == "__main__":
    hero = Hero(
        obj1_name, health = randint(70,100),
        strength = randint(70,80),
        defence = randint(45,55),
        speed = randint(40,50),
        luck = randint(10,30)
        )

    creature = Creature(
        obj2_name,
        health = randint(70,100),
        strength = randint(70,80),
        defence = randint(45,55),
        speed = randint(40,50),
        luck = randint(10,30)
        )

    first_round =  Battle(hero, creature)

如果您发布生物和英雄课程,那就太好了