0

I want to create a rectangle around my player that moves with the player sprite, but so far I've been unsuccessful. when I tested for my rectangle with a small test code(where I got the rect of my player and used collidepoint to see if my mouse cursor is over the rect of the player), the rectangle seems to be at the corner of the screen. I have been trying to understand why this happens, but I couldn't come to any conclusion. can someone please explain to me why this happens? I'm pretty new to player rectangles so I would be grateful if someone explained this to me.

here's my code for drawing the player on the screen:

def animation_idle(self, direction_x, direction_y, listName):
    if self.dir_y == direction_y and self.dir_x == direction_x and self.idleanim:
        sprite = listName[int(self.current_sprite)]
        self.current_sprite += self.animationspeed
        if self.current_sprite >= len(listName):
            self.current_sprite = 0
        screen.blit(sprite, (self.posX, self.posY))
        rect = sprite.get_rect()                      
        mouse = pygame.mouse.get_pos()    # test
        if rect.collidepoint(mouse):
            print("true")
icekete
  • 25
  • 1
  • 4
  • 2
    `rect = sprite.get_rect()` returns a rectangle positioned at `(0, 0)`. A `Sprite` object has no position so there's no way for it to know where it should be. Instead, use `rect = sprite.get_rect(topleft=(self.posX, self.posY))`. – Ted Klein Bergman Sep 23 '21 at 17:16
  • Yes, it's working now. Thank you for the answer. – icekete Sep 23 '21 at 17:27

0 Answers0