Gaspump1112 Gaspump1112 - 6 months ago 14
Python Question

Making text appear one character at a time [PyGame]

I am trying to make the dialog appear one character at a time (Like in Pokemon games and other similar).

I have searched over the internet but have not managed to find anything helpful.

I am aware of another question asked like this, but it didn't solve what I am trying to do. I know this can be done because I have seen games made with python where this has been done.

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

NOTE: I haven't used pygame before so this may not work.