如何让text-rects在延迟pygame后一个接一个出现

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

希望得到一些关于序列中 blitting text-rects 的帮助,它们之间有大约一秒的小延迟。我已经设法接近了,但是我最近的尝试重复了一遍又一遍地 blitting 我的三行文本的过程。我想不出如何让这三行按顺序 blit 到屏幕,然后停止。

我已经包含了这部分代码的示例,我们将不胜感激任何帮助!

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()



您需要在主循环之外维护资源的状态。

在下面的示例中,我们使用 font-images 的列表来保存 what-to-paint。但是要绘制哪张张图片的控制是有定时器功能的。

当然,有很多不同的方法可以做到这一点。

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()