属性错误:无法为pyGame Sprite继承设置属性

5ktev3wc  于 2022-09-18  发布在  Java
关注(0)|答案(1)|浏览(165)

我有一个HPBar类的工作代码-->继承自ProgressBar类-->继承自pygame.sprite.Sprite。我决定创建一个Widget类来实现以下继承流程:HPBar-->ProgressBar-->Widget-->pygame.sprite.Sprite。这样做的目的是为了灵活性,特别是在添加更多的小部件时,如按钮、文本框等。然而,在我的修订中,我遇到了Attribute error: can't set attribute。具体内容如下。

在我的代码中的某个地方,我有这个HPBar示例化:

hp_bar = HPBar(
    x=x, y=y,
    entity=self.player,
    groups=[self.camera, self.extras],
)

**工作代码:此代码在修订前有效。

class HPBar(ProgressBar):
    def __init__(self, entity, *args,**kwargs):
        max_value = entity.stats["max_hp"]
        value = entity.stats["hp"]
        super().__init__(
            max_value=max_value, value=value,
            width=32, height=5,
            *args,**kwargs
        )

class ProgressBar(pygame.sprite.Sprite):
    def __init__(
            self,
            x: float,
            y: float,
            width: int,
            height: int,
            groups: List[pygame.sprite.AbstractGroup] = [],
            max_value: int,
            value: int,
            *args,**kwargs
    ):
        super().__init__(groups)

    @property
    def image(self):
        _image = # pygame surface
        return _image

**修改后的代码:有错误属性的代码。

class ProgressBar(Widget):
    def __init__(
            self,
            max_value: int,
            value: int,
            *args,**kwargs
    ):
        super().__init__(*args,**kwargs)

    @property
    def image(self):
        _image = # pygame surface
        return _image

class Widget(pygame.sprite.Sprite):
    def __init__(
            self,
            x: float, y: float,
            width: int, height: int,
            groups: List[pygame.sprite.AbstractGroup] = [],
    ):
        super().__init__(groups)
        self.image = pygame.Surface((width, height))

回溯错误:

File "C:UsersHpDocumentsWorkingPersonalplatformer1game_modelswindowsplatformer_window.py", line 127, in load_level
    hp_bar = HPBar(
  File "C:UsersHpDocumentsWorkingPersonalplatformer1game_modelsspriteshp_bar.py", line 16, in __init__
    super().__init__(
  File "C:UsersHpDocumentsWorkingPersonalplatformer1contribmodelswidgetsprogress_barsprogress_bar.py", line 24, in __init__
    super().__init__(*args,**kwargs)
  File "C:UsersHpDocumentsWorkingPersonalplatformer1contribmodelswidgetswidget.py", line 22, in __init__
    self.image = pygame.Surface((width, height))
AttributeError: can't set attribute

调试次数较少:

我尝试打印出Widget类中的widthheight参数,以确保我接收和发送的数据类型正确:

在Widget类中:

super().__init__(groups)
        print(width, height)
        print(type(width), type(height))
        self.image = pygame.Surface((width, height))

打印结果:

32 5
<class 'int'> <class 'int'>

此外,我的这个实现类似于我的Widget类实现,并且工作得很好:

class Player(pygame.sprite.Sprite):
    def __init__(self, pos):
        super().__init__()
        self.image = pygame.Surface((16, 32))
nkhmeac6

nkhmeac61#

是的,当然。方法/属性和属性不能同名。image可以是属性,也可以是属性。但不能有两个同名的对象。

以下情况是不可能的:

class Foo:
    def __init__(self):
        self.bar = 1

    def bar(self):
        return 2

print(Foo().bar())
print(Foo().bar())
TypeError: 'int' object is not callable

也不可能:

class Foo:
    def __init__(self):
        self.bar = 1

    @property
    def bar(self):
        return 2

print(Foo().bar)
self.bar = 1
AttributeError: can't set attribute 'bar'

但是,您可以定义setter:

class Foo:
    def __init__(self):
        self._bar = 1
        self.bar = 2

    @property
    def bar(self):
        return self._bar

    @bar.setter
    def bar(self, value):
        self._bar = value

print(Foo().bar)
2

相关问题