python - 为什么它说我没有直肠

标签 python attributes pygame sprite

我已经输入了代码self.rect = pygame.Rect(x, y, 100, 100)。有一个 AttributeError: type object 'MobRow1' has no attribute 'rect'。为什么当我放入 rect 后,不断弹出此错误。

我已经尝试过 self.rect = pygame.Rect(self.x, self.y, 100, 100) 这仍然不起作用。

        class MobRow1(pygame.sprite.Sprite):
            def __init__(self, x, y):
                pygame.sprite.Sprite.__init__(self)
                self.image = pygame.image.load(badguy_file).convert_alpha()
                self.image = pygame.transform.scale(self.image, (50, 50))
                self.x = x
                self.y = y
                self.rect = pygame.Rect(x, y, 100, 100)
                self.mask = pygame.mask.from_surface(self.image)

            def update(self):
                self.rect.y += self.speedy
                self.rect.x += self.speedx

最佳答案

您调用 MobRow1.update(screen_rect),这将导致 self 变为 screen_rect

因此,当调用 self.rect.y += self.speedy 时,它将失败并出现 AttributeError 因为 self,即screen_rect 是一个 Rect 实例,没有 rect 属性。

<小时/>

您应该做的是在 MobRow1 的实例上调用 update ,而不是在 MobRow1 类上本身。此外,如果您这样做,则必须向其 update 函数添加另一个参数,或者从函数调用中删除 screen_rect 参数。

关于python - 为什么它说我没有直肠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51837006/

相关文章:

python - Pyspark 将列表列转换为嵌套结构列

c# - 无法提取属性数据

eclipse - Eclipse 自动完成快捷方式不显示使用复合组件的新标签的属性

jquery - 删除和添加导航上的数据属性

python - “>' not supported between instances of ' 日期时间.时间”和 'datetime.datetime'

python - 如何使用pyramid.wsgi中ini文件的相对路径作为pyramid.paster.get_app的参数?

python - 如何在 pygame 中导入 tmx map ?

python - 如何将文字放置到指定位置?

python - 在检测到 python 时运行特定代码

python - 使用 Python 发送 XMPP 消息