1

Aohoj,

in Pygame I'm trying to set the transparency of an image. I use this:

ship = pygame.image.load(os.path.join(DIR, "spaceship.png"))
ship.set_alpha(128)

Problem: the code works on Mx Linux but does not work on the Raspberry Pi (Raspbian Linux). How to solve this? Could there be a problem with the Pygame version?

Radek
  • 57
  • 6

1 Answers1

0

A PNG file generates a Surface with an format that supports per pixel alpha. The combination of set_alpha with such a format has been supported since Pygame 2.0. Before you had to choose either set_alpha() or alpha per pixel. See pygame.Surface

This value is different than the per pixel Surface alpha. For a surface with per pixel alpha, blanket alpha is ignored and None is returned.

Changed in pygame 2.0: per-surface alpha can be combined with per-pixel alpha.


If you want to smoothly fade in or out an image, write a function that "blends" an image with the screen. In the following function BlendSurface, the parameter alpha is the opacity value of the surface in range[0.0, 1.0]. 0.0 would generate a completely transparent (invisible) surface. 1.0 would generate a completely opaque surface:

def BlendSurface(surf, pos, alpha):
    surf.set_alpha(min(int(alpha*255), 255))
    screen.blit(surf, pos)

See also Pygame : smooth picture aparition.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • 1
    Yea. Issue maybe solved. I have different Pygame version. 2.0.1 vs. 1.9.4. I am looking for update solving. – Radek Jun 25 '21 at 17:10