1

I can get my text to fade out at the end of my "scene", but I cannot figure out how to make the text or screen fade IN. I've looked at many different videos and threads, but they either address images, which is different from what I'm trying to do, say how to fade OUT (I already know how to fade out, I don't know fade IN), or they are outdated. I've also tried reversing the alpha value and the counter, but from what I've tested it doesn't change it and mostly gives me errors.

I would also be interested to see if anyone has a simpler way to do this, eg. is there something I can call from pygame that can fade for me... eg: pygame.fadeout(screen, 2)

import pygame
from pygame.locals import *
pygame.init()

import pygame as pg


def title():
    clock = pg.time.Clock()
    screen = pygame.display.set_mode((1395, 855), pygame.FULLSCREEN)
    font = pygame.font.Font("/Users/ACCOUNTNAME/Library/Fonts/ARIALN.TTF", 70)
    
    color = pg.Color('#FFFFFF')
    orig_surf = font.render("Filler: ", True, color)
    color2 = pg.Color('#FFFFFF')
    orig2_surf = font.render("Very Cool", True, color2)
    
    txt_surf = orig_surf.copy()
    txt2_surf = orig2_surf.copy()
    alpha_surf = pg.Surface(txt_surf.get_size(), pg.SRCALPHA)
    alpha2_surf = pg.Surface(txt2_surf.get_size(), pg.SRCALPHA)
    
    alpha = 255
    counter = 0

    while True:
        for e in pygame.event.get():
            if e.type == QUIT or e.type == KEYDOWN and e.key == pygame.K_ESCAPE:
                pygame.mixer.music.fadeout(1000)
                return

        if alpha > 0 and counter <= 80:
            alpha = max(alpha-0, 0)
            txt_surf = orig_surf.copy()
            alpha_surf.fill((255, 255, 255, alpha))
            txt_surf.blit(alpha_surf, (0, 0), special_flags=pg.BLEND_RGBA_MULT)
            
            txt2_surf = orig2_surf.copy()
            alpha2_surf.fill((255, 255, 255, alpha))
            txt2_surf.blit(alpha2_surf, (0, 0), special_flags=pg.BLEND_RGBA_MULT)
            
            counter += 1
            
        elif alpha > 0 and counter > 80:
            alpha = max(alpha-1.2, 0)
            txt_surf = orig_surf.copy()
            alpha_surf.fill((255, 255, 255, alpha))
            txt_surf.blit(alpha_surf, (0, 0), special_flags=pg.BLEND_RGBA_MULT)

            txt2_surf = orig2_surf.copy()
            alpha2_surf.fill((255, 255, 255, alpha))
            txt2_surf.blit(alpha2_surf, (0, 0), special_flags=pg.BLEND_RGBA_MULT)



        #screen.fill((9, 47, 68, alpha))
        screen.fill((0, 66, 99, alpha))
        
        screen.blit(txt_surf, (302, 348))
        screen.blit(txt2_surf, (509, 418))

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


if __name__ == '__main__':
    pg.init()
    title()
    pg.quit()
  • This code has a lot of convolutions, but obviously, everything you have o do is to have alpha increase from 0 to 255, instead of decrease as the code above start doing once counter is past 80 – jsbueno Dec 06 '20 at 03:04

1 Answers1

0

All you need to do is to set the alpha value for the full text Surface with set_alpha:

alpha = 0
counter = 0

while True:
    # [...]

    if counter <= 80:
        alpha = min(alpha + 255/80, 255)            
    elif alpha > 0 and counter > 80:
        alpha = max(alpha - 255/80, 0)
    counter += 1
        
    txt_surf = orig_surf.copy()
    txt_surf.set_alpha(alpha)    
    txt2_surf = orig2_surf.copy()
    txt2_surf.set_alpha(alpha)

If you want to fade out the entire display, create a Surface the size of the window and a color of your choice:

fadeScreen = pygame.Surface(screen.get_size())
fadeScreen.fill((0, 0, 0))

Blend the Surface at the top of the screen before the display is updated:

while True:
    # [...]

    fadeScreen.set_alpha(255-alpha)
    screen.blit(fadeScreen, (0, 0))
    pg.display.flip()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174