我在尝试创建类的属性时遇到错误(AttributeError:“ int”对象没有属性“ self”)

时间:2019-12-04 17:17:19

标签: python pygame

class food:
    def __init__(self, x, y, width, height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height

    def draw_food(self):
        pygame.draw.rect(screen, black, (self.x, self.y, self.height. self.width))
        pygame.display.update()

    def collition_detection(self):
        pass



    pizza = food(random.randint(100,1000),random.randint(100,500) ,30, 30)
    pizza.draw_food()

当我运行上面的代码时,我得到一个属性错误,说((AttributeError:'int'对象没有属性'self'),并且我在程序中执行了与其他类完全相同的操作,这门课有问题。

1 个答案:

答案 0 :(得分:0)

有一个拼写错误:self.height. self.width 而不是 self.height, self.width。专注于 .,

替换

pygame.draw.rect(screen, black, (self.x, self.y, self.height. self.width))

pygame.draw.rect(screen, black, (self.x, self.y, self.height, self.width))