使文本一次显示一个字符 [PyGame]

Gas*_*112 3 python text pygame dialog

我试图让对话框一次显示一个字符(就像在口袋妖怪游戏和其他类似游戏中一样)。

我在互联网上进行了搜索,但没有找到任何有用的东西。

我知道还有一个这样问的问题,但它没有解决我想要做的事情。我知道这是可以做到的,因为我见过用 python 制作的游戏已经做到了这一点。

Sco*_*God 7

import pygame, sys
from pygame.locals import *

WINDOW_WIDTH = 500
WINDOW_HEIGHT = 500

pygame.init()
DISPLAYSURF = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))

BLACK = (  0,   0,   0)
WHITE = (255, 255, 255)

def display_text_animation(string):
    text = ''
    for i in range(len(string)):
        DISPLAYSURF.fill(WHITE)
        text += string[i]
        text_surface = font.render(text, True, BLACK)
        text_rect = text_surface.get_rect()
        text_rect.center = (WINDOW_WIDTH/2, WINDOW_HEIGHT/2)
        DISPLAYSURF.blit(text_surface, text_rect)
        pygame.display.update()
        pygame.time.wait(100)

def main():
    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()

display_text_animation('Hello World!')
main()
Run Code Online (Sandbox Code Playgroud)

注意:我以前没有使用过 pygame,所以这可能不起作用。