Aphire Aphire - 3 months ago 3
Python Question

Unable to fade out text - PyGame

I am attempting to fade out some text on screen, using the set_alpha function with a new surface.

I am able to fade in the text, however when using this code:

label = myFont.render(textToFadeOut,1,(255,0,0))
newSurf = pygame.Surface(myFont.size(textToFadeOut))
for x in range (225):
newSurf.set_alpha(225 - x)
screen.blit(newSurf, (100,100))

To fade out the text does not change.

The above code is essentially the exact same code I used to fade in (which works), the only difference is that I am using (225 - x) instead of just x to ensure the value becomes incrementally lower.

I have checked that x is not being used anywhere else, and also printed out (225 - x) each loop and the value does get lower.


You are not clearing the frame before blitting the new surface. Try filling with the background color, then blitting your text again.

for x in range(225):
    screen.fill((0,0,0))  #or whatever your background color is
    screen.blit(newSurf, (100,100))

Think about it, it works with the fade in because the text is getting darker and darker, but it doesn't work with the fade out because you are blitting on top of your old text.