How can I put the rect of a sprite in the center of the image

248 Views Asked by At

enter image description here

here is what I have until now, I have draw the rect to keep a track of it, here is the code:

class Personagem(pygame.sprite.Sprite):
      def __init__(self,x,y):
            super().__init__()
            self.imagem = pygame.image.load('sprites/frente-parado.bmp')
            self.rect = self.imagem.get_rect()
            self.rect.height = 20
            self.rect.width = 20
            self.rect.centerx = x
            self.rect.centery = y
            self.velocidade_x = 0
            self.velocidade_y = 0

I have been looking at similar questions here, a have tried create another rect that starts at the bottom left of the original but then I cant collide it because the sprite ended up with two rects

I appreciate any answer, sorry for bad english

3

There are 3 best solutions below

2
Isaac Reynaldo On

To center the rect of a sprite in the image, you can use the center attribute of the rect instead of centerx and centery. Here's an updated version of your code that centers the rect:

class Personagem(pygame.sprite.Sprite):
    def __init__(self, x, y):
        super().__init__()
        self.imagem = pygame.image.load('sprites/frente-parado.bmp')
        self.rect = self.imagem.get_rect()
        self.rect.width = 20
        self.rect.height = 20
        self.rect.center = (x, y)
        self.velocidade_x = 0
        self.velocidade_y = 0

In this version, the center attribute of the rect is set to the tuple (x, y), which will center the rect in the image. Note that I've also switched the order of the width and height attributes, as it's recommended to set the width before the height when working with pygame.

0
guilherme franceschi On

well, I find a way to do it, its a bit ugly but it works

x = per.rect.topleft[0] - 10 y = per.rect.topleft[1] - 10 tela.blit(per.imagem,(x,y))

enter image description here

0
Scarlet On

When you initialize Personagem you give to it the two variables x and y. Looking at your first picture, you were giving the topleft corner of the image. To center the rect on the center of the image you would need to give a pygame.Rect() of the image.

class Personagem(pygame.sprite.Sprite):
    def __init__(self, r):
        super().__init__()
        self.imagem = pygame.image.load('sprites/frente-parado.bmp')
        self.rect = pygame.Rect(0,0,20,20)
        self.rect.center = r.center
        self.velocidade_x = 0
        self.velocidade_y = 0

pygame.Rect(0,0,20,20) creates a rectangle with 20 px in width and 20 px in height, while self.rect.center = r.center centers the rect with the rect of the image.

If you prefer, you can also choose to give to Personagem two variables centerx and centery instead and the class would result in

class Personagem(pygame.sprite.Sprite):
    def __init__(self, centerx,centery):
        super().__init__()
        self.imagem = pygame.image.load('sprites/frente-parado.bmp')
        self.rect = pygame.Rect(0,0,20,20)
        self.rect.centerx = centerx
        self.rect.centery = centery
        self.velocidade_x = 0
        self.velocidade_y = 0