在python中使用类时,获取'TypeError:'int'对象是不可调用的'

时间:2017-02-06 19:36:57

标签: python class oop pygame

我在python中使用了一个类(我在这个程序中也使用了pygame),但它不允许我调用这个特殊的方法。这个班叫做'敌人'。 这是问题方法:

    def attack(self, screen):
    self.charx = self.screen.get_width()/2 - self.pic.get_width()/2
    self.chary = self.screen.get_height()/2 - self.pic.get_height()/2
    textbox(self.screen, self.charx - self.c.rect.x + self.rect.x, self.chary - self.c.rect.y + self.rect.y, 15, 5, True, (255, 255, 255), True, (0, 0, 0), 0, '', True, (255, 0, 0), 15*self.healthpercent)

    if self.rect.colliderect(self.w.rect):
        if self.agility*random.randint(1, 10) > self.c.agility*random.randint(1, 10):
            self.c.health -= self.level*5  
        if self.agility*random.randint(1, 10) <= self.c.agility*random.randint(1, 10):
            self.health -= self.w.weaponpower*self.c.strength
            self.healthpercent = self.health/self.level*100

    if self.health == 0:
        self.dead = True

这就是我所说的:

    e = enemy(enemypics, 50, 50, 4, 3, screen, c, bgdict, w)
    if e.dead == False:
        e.attack(screen) #stops on this line 
        e.update(screen)
    else:
        e.die(screen, timesincelasttick)

显然一些变量名称等在别处定义,但我不认为这些位是必要的。 这是完整的堆栈跟踪:

    Traceback (most recent call last):
    File "E:\Code\Game\Main6.py", line 119, in <module>
        e.attack(screen)
    TypeError: 'int' object is not callable
    >>>

2 个答案:

答案 0 :(得分:3)

此错误的最可能原因是程序中的其他位置有一个名为attack的变量,并且您为其指定了int值。

所以当你这样做时

e.attack(screen)

您正尝试在int变量attack

上调用函数

确保您的程序中没有名为attack的变量

答案 1 :(得分:0)

我已经解决了!我不小心重用了“攻击”,改变它并使其成为一个int对象。我已经改名为'#attack;&#39;变量,它现在有效。

相关问题