Pygame 逐一渲染文本导致游戏延迟我该如何解决?

问题描述

每当我尝试显示以 1 比 1 呈现此文本时,它似乎会减慢我的游戏速度并使一切运行缓慢,并且出于某种原因它也开始文本我希望它在完成输入文本后结束 { {3}}


    def show_text(string):
        WHITE = (255,255,255)
        text = ''
        font = pygame.font.Font("Alice.ttf",30)
        for i in range(len(string)):
            text += string[i]
            text_surface = font.render(text,True,WHITE)
            text_rect = text_surface.get_rect()
            text_rect.center = (700/2,800/2)
            window.blit(text_surface,text_rect)
            pygame.display.update()
            pygame.time.wait(100)

        display_text_animation('Hello World!')

这里是完整的代码,除了 Alice.tff

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)
WHITE = (255,255)


class player:
    def __init__(self,x,y,height,width,color):
        self.x = x
        self.y = y
        self.height = height
        self.width = width
        self.color = color
        self.rect = pygame.Rect(x,width)
    def draw(self):
        self.rect.topleft = (self.x,self.y)
        pygame.draw.rect(disPLAYSURF,self.color,self.rect)

white = (120,120,120)
player1 = player(150,150,50,white)
def display_text_animation(string):
    text = ''
    for i in range(len(string)):
        text += string[i]
        font = pygame.font.Font("Alice.ttf",30)
        text_surface = font.render(text,white)
        text_rect = text_surface.get_rect()
        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()
        keys = pygame.key.get_pressed()
        if keys[pygame.K_d]:
            player1.x += 5
        if keys[pygame.K_a]:
            player1.x -= 5
        disPLAYSURF.fill((0,0))        
        player1.draw()
        display_text_animation('Hello World!')
main()

解决方法

不要在每一帧中都创建 pygame.font.Font 对象。创建字体对象非常耗时。每次创建 Font 对象时,都必须读取和解释字体文件(“Alice.ttf”)。在初始化期间在应用程序循环之前创建 Font 对象。

此外,不要使用应用程序循环为循环中的文本设置动画。使用 pygame.time.get_ticks() 来测量时间。计算作为时间函数显示的字母数:

font = pygame.font.Font("Alice.ttf",30)
def display_text_animation(string,start_time):
    current_time = pygame.time.get_ticks()  
    letters = (current_time - start_time) // 100 
    text = string[:letters]
    WHITE = (255,255,255)
    text_surface = font.render(text,True,WHITE)
    text_rect = text_surface.get_rect()
    text_rect.center = (700/2,800/2)
    DISPLAYSURF.blit(text_surface,text_rect)
def main():
    text = 'Hello World!'
    start_time = pygame.time.get_ticks()    

    clock = pygame.time.Clock()
    while True:
        clock.tick(60)
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
        keys = pygame.key.get_pressed()
        if keys[pygame.K_d]:
            player1.x += 5
        if keys[pygame.K_a]:
            player1.x -= 5

        DISPLAYSURF.fill((0,0))        
        player1.draw()
        display_text_animation(text,start_time)
        pygame.display.update()

当您想要为新文本设置动画时,您只需设置 textstart_time。例如:

text = "New text"
start_time = pygame.time.get_ticks()