Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Sprite shadow changing to full black

player.png
shadow comparison

The shadows are different when I blit the player image to a surface and then loading that surface to the display vs loading the entire image on the display

import pygame

pygame.init()
display = pygame.display.set_mode((1280, 736))
display.fill('#555358')
clock = pygame.time.Clock()


if __name__ == '__main__':
    image_1 = pygame.Surface((16, 16)).convert_alpha()
    image_1.blit(
        pygame.image.load('player.png').convert_alpha(),
        (0, 0),
        (16, 32, 16, 16))
    image = pygame.transform.scale(image_1, (16 * 3, 16 * 3))
    image.set_colorkey((0, 0, 0))
    display.blit(image, (0, 96))

    image_2 = pygame.image.load('player.png').convert_alpha()
    image_2 = pygame.transform.scale(image_2, (288 * 3, 240 * 3))
    display.blit(image_2, (0, 0))

    while True:
        # Process player inputs.
        for event in pygame.event.get():

            if event.type == pygame.QUIT:
                pygame.quit()
                raise SystemExit

        pygame.display.flip()
        clock.tick(60)

I thought setting the color key was messing with it, so I tried removing it to no avail

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You need to create a surface with an alpha channel (pygame.SRCALPHA) instead of converting it with convert_alpha and setting a color key with set_colorkey:

image_1 = pygame.Surface((16, 16), pygame.SRCALPHA)
image_1.blit(
        pygame.image.load('player.png').convert_alpha(),
        (0, 0),
        (16, 32, 16, 16))
image = pygame.transform.scale(image_1, (16 * 3, 16 * 3))
display.blit(image, (0, 96))

Note: pygame.Surface((16, 16)) creates a completely black surface. In contrast, pygame.Surface((16, 16), pygame.SRCALPHA) creates a completely transparent surface. convert_alpha() changes the format of the image, but it remains solid black. Also see How to make a surface with a transparent background in pygame.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading