0

I'm making a 2d platformer and i made my first 3 levels and on the 3rd level there is a platform with 2 enemies moving left and right and It is almost impossible to get through because the players hit box is too big. I was wondering how can I make a hitbox where I can change the size of it.

This is how I'm making the hit box:

self.image = self.images_right[self.index]
    self.rect = self.image.get_rect()
    self.rect.x = x
    self.rect.y = y
    self.width = self.image.get_width()
    self.height = self.image.get_height()

and this is the collide detection:

#check for collisions with enemy
        if pygame.sprite.spritecollide(self, virus_group, False):
            game_over = -1
        
        #check for collisions with water
        if pygame.sprite.spritecollide(self, water_group, False):
            game_over = -1

        #check for collisions with exit
        if pygame.sprite.spritecollide(self, exit_group, False):
            game_over = 1

This is how I am seeing the hitbox:

pygame.draw.rect(win, (255, 255, 255), self.rect, 2)

I am new to to coding so I don't know if this is all the code you need to see so if you think it might be something else I can share more code.

  • 1
    Use [get_bounding_rect()](https://www.pygame.org/docs/ref/surface.html#pygame.Surface.get_bounding_rect) instead of [get_recrt()](https://www.pygame.org/docs/ref/surface.html#pygame.Surface.get_rect). See [How to get the correct dimensions for a pygame rectangle created from an image](https://stackoverflow.com/questions/65361582/how-to-get-the-correct-dimensions-for-a-pygame-rectangle-created-from-an-image) – Rabbid76 Jun 03 '22 at 11:57
  • 1
    `pygame.sprite.spritecollide` use the `.rect` attribute of the sprite. So you need to make the rectangle smaller. – Rabbid76 Jun 03 '22 at 12:00

0 Answers0