“Chicken”对象没有属性“rect”

2024-10-03 21:32:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在用python编写一个更大的程序。这是一个简单的游戏,但我有一个错误。有人能帮我吗?在

错误

          Traceback (most recent call last):
  File "C:/Users/kkuja/Desktop/game.py", line 36, in <module>
    MainWindow.MainLoop()
  File "C:/Users/kkuja/Desktop/game.py", line 17, in MainLoop
    self.chicken_sprites.draw(self.screen)
  File "C:\Users\kkuja\AppData\Local\Programs\Python\Python35\lib\site-packages\pygame\sprite.py", line 475, in draw
    self.spritedict[spr] = surface_blit(spr.image, spr.rect)
AttributeError: 'Chicken' object has no attribute 'rect'

编码

^{pr2}$

提前谢谢!在


Tags: inpyrectself程序game错误line
1条回答
网友
1楼 · 发布于 2024-10-03 21:32:14

在您代码中的函数self.chicken_sprites.draw(self.screen)中,chicken.rect正试图被访问,但您没有定义它。在

如果您参考official documentation,您可以找到代码的和平

class Block(pygame.sprite.Sprite):

    # Constructor. Pass in the color of the block,
    # and its x and y position
    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.
       self.image = pygame.Surface([width, height])
       self.image.fill(color)

       # Fetch the rectangle object that has the dimensions of the image
       # Update the position of this object by setting the values of rect.x and rect.y
       self.rect = self.image.get_rect()

您没有在Chicken中设置self.rect,它应该是这样的。在

^{pr2}$

相关问题 更多 >