创建某些对象的第二个实例后,pygame属性错误

发布于 2025-01-18 11:35:46 字数 1912 浏览 2 评论 0原文

我可以创建此类的一个实例,并且功能正常。但是,每当尝试创建第二个实例时,即使删除了第一个对象,也会返回错误。这也很奇怪,因为有一个类与该类相同的类,可以在没有错误的情况下根据需要创建的尽可能多的实例。

两次都以完全相同的方式使对象。

class GOAL(pygame.sprite.Sprite):
    def __init__(self,spawnx,spawny):
        super().__init__()
        self.image = goal
        self.surf = pygame.Surface(self.image.get_rect().size)
        self.rect = self.image.get_rect(topleft = (spawnx,spawny))

类启动代码。

goal = GOAL((lineCount*100),(characterCount*100))
all_sprites.add(goal)

类创建代码。

Traceback (most recent call last):
  File "C:\Users\User\Documents\python\pygame\simian shenanigans\simian shenanigans.py", line 975, in <module>
    mainmenu()
  File "C:\Users\User\Documents\python\pygame\simian shenanigans\simian shenanigans.py", line 689, in mainmenu
    entity.clicked()
  File "C:\Users\User\Documents\python\pygame\simian shenanigans\simian shenanigans.py", line 540, in clicked
    self.sendfunction()
  File "C:\Users\User\Documents\python\pygame\simian shenanigans\simian shenanigans.py", line 732, in playmenu
    entity.clicked()
  File "C:\Users\User\Documents\python\pygame\simian shenanigans\simian shenanigans.py", line 540, in clicked
    self.sendfunction()
  File "C:\Users\User\Documents\python\pygame\simian shenanigans\simian shenanigans.py", line 768, in level1
    levelgeneration(levelToGenerate,all_sprites,enemies,walls,playerGroup)
  File "C:\Users\User\Documents\python\pygame\simian shenanigans\simian shenanigans.py", line 903, in levelgeneration
    goal = GOAL((lineCount*100),(characterCount*100))
  File "C:\Users\User\Documents\python\pygame\simian shenanigans\simian shenanigans.py", line 638, in __init__
    self.surf = pygame.Surface(self.image.get_rect().size)
AttributeError: 'GOAL' object has no attribute 'get_rect'

错误代码。

请询问是否需要显示任何其他代码。整个过程是1000行,而不是全部相关,因此我试图包括必要的代码。

I can create one instance of this class, and it functions fine. However whenever a second instance is attempted to be created, even if the first object has been deleted, it returns an error. This is also odd because there is a class identical to this one, which can have as many instances created as needed with no errors.

Object is being made the exact same way both times.

class GOAL(pygame.sprite.Sprite):
    def __init__(self,spawnx,spawny):
        super().__init__()
        self.image = goal
        self.surf = pygame.Surface(self.image.get_rect().size)
        self.rect = self.image.get_rect(topleft = (spawnx,spawny))

Class initiation code.

goal = GOAL((lineCount*100),(characterCount*100))
all_sprites.add(goal)

Class creation code.

Traceback (most recent call last):
  File "C:\Users\User\Documents\python\pygame\simian shenanigans\simian shenanigans.py", line 975, in <module>
    mainmenu()
  File "C:\Users\User\Documents\python\pygame\simian shenanigans\simian shenanigans.py", line 689, in mainmenu
    entity.clicked()
  File "C:\Users\User\Documents\python\pygame\simian shenanigans\simian shenanigans.py", line 540, in clicked
    self.sendfunction()
  File "C:\Users\User\Documents\python\pygame\simian shenanigans\simian shenanigans.py", line 732, in playmenu
    entity.clicked()
  File "C:\Users\User\Documents\python\pygame\simian shenanigans\simian shenanigans.py", line 540, in clicked
    self.sendfunction()
  File "C:\Users\User\Documents\python\pygame\simian shenanigans\simian shenanigans.py", line 768, in level1
    levelgeneration(levelToGenerate,all_sprites,enemies,walls,playerGroup)
  File "C:\Users\User\Documents\python\pygame\simian shenanigans\simian shenanigans.py", line 903, in levelgeneration
    goal = GOAL((lineCount*100),(characterCount*100))
  File "C:\Users\User\Documents\python\pygame\simian shenanigans\simian shenanigans.py", line 638, in __init__
    self.surf = pygame.Surface(self.image.get_rect().size)
AttributeError: 'GOAL' object has no attribute 'get_rect'

Error code.

Please ask if any other code required to be shown. Whole thing is 1000 lines and not all relevant so I have tried to include necessary code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

涫野音 2025-01-25 11:35:46

看起来您正在将 self.image 设置为 goal,它是 GOAL 的实例,假设这与类启动代码。然而,即使 GOAL 是一个 pygame.sprite.sprite,该类也没有 get_rect 函数!

我想你想参考 pygame.Surface相反,根据此处的示例:

class Block(pygame.sprite.Sprite):

    def __init__(self, color, width, height):
       # Call the parent class (Sprite) constructor
       pygame.sprite.Sprite.__init__(self)

       # Create an image of the block, and fill it with a color.
       # This could also be an image loaded from the disk.
 This--->self.image = pygame.Surface([width, height])
       self.image.fill(color)

       self.rect = self.image.get_rect()

It looks like you're setting self.image to be goal, which is an instance of GOAL, assuming thats the same goal as in the class initiation code. However, even though GOAL is a pygame.sprite.sprite, that class doesn't have a get_rect function!

I think you want to reference the pygame.Surface instead, according to the example here:

class Block(pygame.sprite.Sprite):

    def __init__(self, color, width, height):
       # Call the parent class (Sprite) constructor
       pygame.sprite.Sprite.__init__(self)

       # Create an image of the block, and fill it with a color.
       # This could also be an image loaded from the disk.
 This--->self.image = pygame.Surface([width, height])
       self.image.fill(color)

       self.rect = self.image.get_rect()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文