I want to put a bird image with transparent background on the top of a sky image. I used convert_alpha() on the bird image. The result looks like this:
The code is:
class Game():
screen: pygame.Surface
clock: pygame.time.Clock
all_sprites: pygame.sprite.Group
def __init__(self) -> None:
self.screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption('Flappy Birds')
self.all_sprites = pygame.sprite.Group()
self.clock = pygame.time.Clock()
Background(self.all_sprites)
Player(self.all_sprites)
def run(self) -> None:
prev_time = time.time()
while True:
dt = time.time() - prev_time
prev_time = time.time()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# GAME LOGIC
self.screen.fill('Blue')
self.all_sprites.update(dt)
self.all_sprites.draw(self.screen)
pygame.display.update()
self.clock.tick(50)
if __name__ == '__main__':
pygame.init()
game = Game()
game.run()
the code for the sprite background and bird is:
class Background(pygame.sprite.Sprite):
image: pygame.Surface
rect: pygame.Rect
pos_x: float
def __init__(self, group: pygame.sprite.Group):
super().__init__(group) # the Background instance belong to this group
bg = pygame.image.load('photos/bg.png').convert()
scale_factor = WINDOW_HEIGHT / bg.get_height()
bg = pygame.transform.rotozoom(bg, 0, scale_factor)
self.image = pygame.Surface((bg.get_width() * 2, WINDOW_HEIGHT))
self.image.blit(bg, (0, 0))
self.image.blit(bg, (bg.get_width(), 0))
self.rect = self.image.get_rect(topleft=(0, 0))
class Player(pygame.sprite.Sprite):
def __init__(self, group: pygame.sprite.Group):
super().__init__(group)
image1 = pygame.image.load('photos/player-1.png').convert_alpha()
image1 = pygame.transform.rotozoom(image1, 0, 0.1)
image2 = pygame.image.load('photos/player-2.png').convert_alpha()
image2 = pygame.transform.rotozoom(image2, 0, 0.1)
self.frames = [image1, image2]
self.frame_index = 0
self.image = self.frames[self.frame_index]
self.rect = self.image.get_rect(center=(WINDOW_WIDTH // 3.5, WINDOW_HEIGHT // 2))
self.rect_pos = list(self.rect.topleft)
I've convert the 32 bit PNG of the bird to 8 bit. I think there is no problem with the bird image, bc it works when I draw it separately. Also when I write a similar code on the other file, the code works See the code that works:
pygame.init()
screen = pygame.display.set_mode((400, 400))
clock = pygame.time.Clock()
bg_image = pygame.image.load('Flappy Birds/photos/bg.png').convert()
bg = pygame.Surface((bg_image.get_width() * 2, 300))
bg.blit(bg_image, (0, 0))
bg.blit(bg_image, (bg_image.get_width(), 0))
bg_rect = bg.get_rect(topleft=(0, 0))
i1 = pygame.image.load('Flappy Birds/photos/player-1.png').convert_alpha()
i1 = pygame.transform.rotozoom(i1, 0, 0.3)
i1_rect = i1.get_rect(topleft=(0,0))
prev_time = time.time()
while True:
Dt = time.time() - prev_time
prev_time = time.time()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.blit(bg, bg_rect)
screen.blit(i1, i1_rect)
pygame.display.update()
clock.tick(50)

Does anyone know what happened??
I tried to store Background and Bird instances not in the same sprite Group, and then blit their image on the screen separately: self.screen.blit(self.background.image, self.background.rect) and self.screen.blit(self.player.image, self.player.rect) inside the Game.run(). The bird image shows transparent when I didn't blit background. However, once I blit the background, the bird's background turns solid dark.
source https://stackoverflow.com/questions/76213041/python-pygame-transparancy-issue-with-overlapped-png-images
Comments
Post a Comment