一步一步显示文本字符 [PyGame]

3 投票
3 回答
5406 浏览
提问于 2025-04-18 06:54

我想让对话框中的文字一个一个字地出现(就像在宝可梦游戏和其他类似的游戏中那样)。

我在网上搜索过,但没有找到任何有用的信息。

我知道还有其他人问过类似的问题,但那些回答并没有解决我想要做的事情。我知道这是可以做到的,因为我见过用Python制作的游戏里实现了这个效果。

3 个回答

0

这个例子有点简单,它创建了一个函数,你可以随时使用。在这个例子中,我把这个函数叫做'slow',并设置了在调用它时需要输入一个字符串。无论你输入什么,都会一个一个字符地显示出来。显示的速度取决于'sleep'旁边的数值。希望这对你有帮助。

from time import * #imports all the time functions

def slow(text): #function which displays characters one at a time
    for letters in text: #the variable goes through each character at a time
        print(letters, end = "") #current character is printed
        sleep(0.02) #insert the time between each character shown
#the for loop will move onto the next character

slow("insert text here") #instead of print, use the name of the function

如果你直接运行这个代码,它应该会一个一个字符地输出“insert text here”。

0

下面的代码运行得很好。它可以防止事件队列过载(也就是说,当有很多行或者大量文本时,不会导致动画停滞)。如果这个代码嵌入在一个简单的应用程序中,而这个应用程序又没有其他方式来处理事件队列,这样的做法是非常必要的。

line_space = 16
basicfont = pygame.font.SysFont('MorePerfectDOSVGA', 16)

def text_ani(str, tuple):
    x, y = tuple
    y = y*line_space ##shift text down by one line
    char = ''        ##new string that will take text one char at a time. Not the best variable name I know.
    letter = 0
    count = 0
    for i in range(len(str)):
        pygame.event.clear() ## this is very important if your event queue is not handled properly elsewhere. Alternativly pygame.event.pump() would work.
        time.sleep(0.05) ##change this for faster or slower text animation
        char = char + str[letter]
        text = basicfont.render(char, False, (2, 241, 16), (0, 0, 0)) #First tuple is text color, second tuple is background color
        textrect = text.get_rect(topleft=(x, y)) ## x, y's provided in function call. y coordinate amended by line height where needed
        screen.blit(text, textrect)
        pygame.display.update(textrect) ## update only the text just added without removing previous lines.
        count += 1
        letter += 1
        print char ## for debugging in console, comment out or delete.


text_ani('this is line number 1 ', (0, 1)) # text string and x, y coordinate tuple.
text_ani('this is line number 2', (0, 2))
text_ani('this is line number 3', (0, 3))
text_ani('', (0, 3)) # this is a blank line
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()

注意:我之前没有用过pygame,所以这个可能不太管用。

撰写回答