Search code examples
pythontexttimepygameblit

How to make text-rects appear one after another, after a delay in pygame


hoping to get some help regarding blitting text-rects in a sequence, a small delay of about a second between them. I've managed to get pretty close however my most recent attempt repeats the process of blitting my three lines of text over and over. I can't for the life of my figure out how to have these three lines blit to the screen in order, and then stop.

I have included an example of this part of my code, any and all help would be greatly appreciated!

def Title_Screen():
    while True:
        TitleScreen_MOUSE_POS = pygame.mouse.get_pos()

        # Background
        SCREEN.blit(BG, (0, 0))
        pygame.display.flip()

        # Title
        pygame.time.delay(1000)
        SCREEN.blit(TitleScreen1_TEXT, TitleScreen1_RECT)
        pygame.display.flip()

        pygame.time.delay(1000)
        SCREEN.blit(TitleScreen2_TEXT, TitleScreen2_RECT)
        pygame.display.flip()

        pygame.time.delay(1000)
        SCREEN.blit(TitleScreen3_TEXT, TitleScreen3_RECT)

        # Press Any Key To Continue
        SCREEN.blit(PressKeyText, PressKeyRect)
        pygame.display.flip()

        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                Main_menu()
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()          
        pygame.display.update()




Solution

  • You need to maintain the state of the resources outside of the main loop.

    In the example below, we use a list of font-images to hold what-to-paint. But the control of which image to paint is advanced with a timer function.

    Of course, there's lots of different ways to do this.

    import pygame
    
    WIDTH = 800
    HEIGHT= 300
    
    BLACK = (  0,   0,   0)
    WHITE = (255, 255, 255)
    
    pygame.init()
    screen = pygame.display.set_mode( ( WIDTH, HEIGHT ) )
    pygame.display.set_caption( "Title Screen" )
    
                    
    font  = pygame.font.Font( None, 50 )
    text1 = font.render( "The Owl and the Pussy-cat went to sea", 1, WHITE)
    text2 = font.render( "  In a beautiful pea-green boat,", 1, WHITE)
    text3 = font.render( "They took some honey, and plenty of money,", 1, WHITE)
    text4 = font.render( "  Wrapped up in a five-pound note.", 1, WHITE)
    title_lines = [ text1, text2, text3, text4 ]
    
    title_delay     = 2000  # Milliseconds between title advances
    title_count     = 4     # How many titles are in the animation
    title_index     = 0     # what is the currently displayed title
    title_next_time = title_delay  # clock starts at 0, time for first title
    
    
    while True:
        clock = pygame.time.get_ticks()   # time now
    
        # Handle events
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit()
    
        # paint the screen
        screen.fill( BLACK )  # paint it black
    
        # Write the current title, unless we've seen them all
        if ( title_index < title_count ):
            screen.blit( title_lines[ title_index ], ( 10, 10 ) )
    
            # Is it time to update to the next title?
            if ( clock > title_next_time ):
                title_next_time = clock + title_delay  # some seconds in the future
                title_index += 1 # advance to next title-image
    
        pygame.display.flip()