Home > Blockchain >  Sprite shadow changing to full black
Sprite shadow changing to full black

Time:11-27

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

CodePudding user response:

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.

  • Related