0

enter image description hereI am writing a Pygame Game with Sprite. It seems that some png images are showing fine without the background, and without me using convert_alpha()

but some of the images I want to use are showing with the background, even when I use convert_alph() nothing happens.

How do I make this to remove the background image from my png image?

     from turtle import width
     import pygame
     import os

     BACKGROUND_IMAGE = pygame.image.load(
        os.path.join("Assets", "2dforest.jpeg"))

     BACKGROUND_WIDTH, BACKGROUND_HEIGHT = 900, 500
     pygame.display.set_caption("Hunting Birds")

     WIN = pygame.display.set_mode((BACKGROUND_WIDTH, BACKGROUND_HEIGHT))


     class HunterGun(pygame.sprite.Sprite):

        def __init__(self, image, width, height, pos_x, pos_y):
           super().__init__()
           self.width = width
           self.height = height
           self.pos_x = pos_x
           self.pos_y = pos_y
           self.image = pygame.image.load(
            os.path.join("Assets", image)).convert_alpha()
           self.rect = self.image.get_rect()

           def update(self):
             self.image = pygame.transform.scale(
             self.image, (self.width, self.height))
             self.rect = self.image.get_rect(center=self.rect.center)

          def move_and_turn(self, mover):
             keys_pressed = pygame.key.get_pressed()
             if (keys_pressed[pygame.K_LEFT]):
                pos_x, pos_y = self.rect.center
                pos_x -= mover
                self.rect.center = (pos_x, pos_y)
             elif (keys_pressed[pygame.K_RIGHT]):
                pos_x, pos_y = self.rect.center
                pos_x += mover
                self.rect.center = (pos_x, pos_y)
             elif (keys_pressed[pygame.K_UP]):
                pos_x, pos_y = self.rect.center
                pos_y -= mover
                self.rect.center = (pos_x, pos_y)
             elif (keys_pressed[pygame.K_DOWN]):
                pos_x, pos_y = self.rect.center
                pos_y += mover
                self.rect.center = (pos_x, pos_y)


      gunhunter = HunterGun("hunt3.png", 100, 100, 250, 250)
      gun_sprite = pygame.sprite.Group()
      gun_sprite.add(gunhunter)

     pygame.init()
     clock = pygame.time.Clock()
     run = True

     while run:
        for event in pygame.event.get():
           if event.type == pygame.QUIT:
              run = False

       picture = pygame.transform.scale(BACKGROUND_IMAGE, (1000, 540))

      gun_sprite.update()
      WIN.blit(picture, (0, 0))
      gun_sprite.draw(WIN)
      gunhunter.move_and_turn(4)
      pygame.display.flip()
      clock.tick(60)

 pygame.quit()
  • Most likely, the background of your images is not congruent. Just because they are PNGs doesn't mean the background is transparent. The background can still be opaque. In this case you can try to set a color key with [`pygame.Surface.set_colorkey`](https://www.pygame.org/docs/ref/surface.html#pygame.Surface.set_colorkey). See [How can I make an Image with a transparent Backround in Pygame?](https://stackoverflow.com/questions/62623341/how-can-i-make-an-image-with-a-transparent-backround-in-pygame/62623422#62623422) and many many more answers here. – Rabbid76 Oct 27 '22 at 08:38
  • **You have downloaded a preview of the image with checkerboard background, but not the transparent PNG** – Rabbid76 Oct 27 '22 at 08:42

0 Answers0