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.