Skip to main content

Python Pygame Transparancy Issue With overlapped PNG Images

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:

enter image description here 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)

enter image description here

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

Popular posts from this blog

Where and how is this Laravel kernel constructor called? [closed]

Where and how is this Laravel kernel constructor called? public fucntion __construct(Application $app, $Router $roouter) { } I have read the documentation and some online tutorial but I can find any clear explanation. I am learning Laravel and I am wondering where does this kernel constructor receives its arguments from. "POSTMOTERM" CLARIFICATION: Here is more clarity.I have checked the boostrap/app.php and it is only used for boostrapping the interfaces into the container class. What is not clear to me is where and how the Kernel class is instatiated and the arguments passed to the object calling the constructor.Something similar to; obj = new kernel(arg1,arg2) or, is the framework using some magic functions somewhere? Special gratitude to those who burn their eyeballs and brain cells on this trivia before it goes into a full blown menopause alias "MARKED AS DUPLICATE". To some of the itchy-finger keyboard warriors, a.k.a The mods,because I believe in th...

Why is my reports service not connecting?

I am trying to pull some data from a Postgres database using Node.js and node-postures but I can't figure out why my service isn't connecting. my routes/index.js file: const express = require('express'); const router = express.Router(); const ordersCountController = require('../controllers/ordersCountController'); const ordersController = require('../controllers/ordersController'); const weeklyReportsController = require('../controllers/weeklyReportsController'); router.get('/orders_count', ordersCountController); router.get('/orders', ordersController); router.get('/weekly_reports', weeklyReportsController); module.exports = router; My controllers/weeklyReportsController.js file: const weeklyReportsService = require('../services/weeklyReportsService'); const weeklyReportsController = async (req, res) => { try { const data = await weeklyReportsService; res.json({data}) console...

How to show number of registered users in Laravel based on usertype?

i'm trying to display data from the database in the admin dashboard i used this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count(); echo $users; ?> and i have successfully get the correct data from the database but what if i want to display a specific data for example in this user table there is "usertype" that specify if the user is normal user or admin i want to user the same code above but to display a specific usertype i tried this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count()->WHERE usertype =admin; echo $users; ?> but it didn't work, what am i doing wrong? source https://stackoverflow.com/questions/68199726/how-to-show-number-of-registered-users-in-laravel-based-on-usertype