Согласно документации каждый спрайт должен иметь атрибут .rect
.
См. Документацию pygame.sprite
:
Базовый класс Sprite может рисоватьСпрайты, которые он содержит на поверхности.Метод Group.draw()
требует, чтобы каждый Sprite имел атрибут Surface.image
и Surface.rect
.
Атрибут .rect
должен быть установлен приложением:
СноваЯ обращаюсь к документации:
При создании подкласса Sprite обязательно вызовите инициализатор базы перед добавлением Sprite в группы.Например:
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()
Конечно, атрибуты могут быть напрямую установлены и для объекта спрайта:
например,
mySprite = pygame.sprite.Sprite()
mySprite.image = pygame.Surface([width, height])
mySprite.rect = mySprite.image.get_rect()
После этого позицияможно получить из атрибута .rect
в любое время.См. pygame.Rect
:
например
topLeftX, tolLeftY = mySprite.rect.topleft
centerX, centerY = mySprite.rect.center