我怎样才能阻止这个计时器造成的滞后?

How could I stop the lag made by this timer?

我一直在做一个 Pygame 项目,最近我在屏幕上显示的程序中添加了一个计时器。该程序主要生成 2 个随机数,并将它们显示在屏幕上。然后,用户可以将结果放在输入框上。如果正确,则“Puntuació”(与分数相同)加 5 分。在我添加计时器之前,一切都运行良好且流畅。现在这段代码仍然有效并且没有崩溃,但它运行得非常慢。我确定它一定与计时器部分有关,但不知道如何解决。 [已编辑]

import pygame
import random
from InputBox import InputBox
from pygame import mixer
import time

pygame.init()
pygame.time.set_timer(pygame.USEREVENT, 60000)
clock = pygame.time.Clock()
surface = pygame.display.set_mode((600, 400))
pygame.display.set_caption("Projecte MatZanfe")
font = pygame.font.SysFont('comicsans', 50)
base_font = pygame.font.Font(None, 32)
user_text = ''
color_active = pygame.Color('lightskyblue3')
running = True
points = 0
t = 60
def TimeFunction():
    t = 60
    while True:
        time.sleep(1)
        t = t - 1
        return t

def start_the_game():
    x = random.randint(0, 10)
    y = random.randint(0, 10)
    is_correct = False
    return x, y

def display_the_game(x, y):
    # Variables
    z = x + y
    surface.fill((255, 70, 90))
    text = font.render(str(x) + "+" + str(y), True, (255, 255, 255))

    text_surface = base_font.render(user_text, True, (255, 255, 255))
    surface.blit(text, (260, 120))
    input_box.draw(surface)
    punts = font.render("Puntuació: " +  str(points),True, (255,255,255))
    surface.blit(punts, (350,30))
    titolsuma = font.render("SUMA (1)", True, (0,0,0))
    surface.blit(titolsuma,(10,20))
    temps = font.render("Temps:" + str(t),True, (255,255,255))
    surface.blit(temps,(0,220))

x, y = start_the_game()
input_box = InputBox(190, 250, 200, 32)

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.USEREVENT:
            t -= 1
            temps = font.render("Temps:" + str(t), True, (255, 255, 255))
            surface.blit(temps, (0, 220))
            pygame.display.flip()
            if int(t) == 0:
                pygame.QUIT
        else:
            result = input_box.handle_event(event)
            if result != None:
                if int(result) == int(x) + int(y):
                    points = points + 5
                    mixer.music.load('StarPost.wav')
                    mixer.music.play(1)

                # create new random numbers
                x, y = start_the_game()

                # reset input box (just create a new box)
                input_box = InputBox(190, 250, 200, 32)


    display_the_game(x, y)
    pygame.display.update()

pygame.quit()

这里是导入的InputBox的代码(以防万一)

import pygame


pygame.init()
surface = pygame.display.set_mode((600, 400))
COLOR_INACTIVE = pygame.Color('lightskyblue3')
COLOR_ACTIVE = pygame.Color('black')
FONT = pygame.font.SysFont('comicsans', 32)
base_font = pygame.font.Font(None, 32)
color_active = pygame.Color('lightskyblue3')
user_text = ''


class InputBox:

    def __init__(self, x, y, w, h, text=''):
        self.rect = pygame.Rect(x, y, w, h)
        self.color = COLOR_INACTIVE
        self.text = text
        self.txt_surface = FONT.render(text, True, self.color)
        self.active = False

    def handle_event(self, event):
        if event.type == pygame.MOUSEBUTTONDOWN:
            # If the user clicked on the input_box rect.
            if self.rect.collidepoint(event.pos):
                # Toggle the active variable.
                self.active = not self.active
            else:
                self.active = False
            # Change the current color of the input box.
            self.color = COLOR_ACTIVE if self.active else COLOR_INACTIVE
        if event.type == pygame.KEYDOWN:
            if self.active:
                if event.key == pygame.K_RETURN:
                    user_input = self.text
                    self.text = ''
                    self.txt_surface = FONT.render(self.text, True, self.color)
                    return user_input
                elif event.key == pygame.K_BACKSPACE:
                    self.text = self.text[:-1]
                else:
                    self.text += event.unicode
                # Re-render the text.
                self.txt_surface = FONT.render(self.text, True, self.color)

    def update(self):
        # Resize the box if the text is too long.
        width = max(200, self.txt_surface.get_width()+10)
        self.rect.w = width

    def draw(self, screen):
        # Blit the text.
        screen.blit(self.txt_surface, (self.rect.x+5, self.rect.y+5))
        # Blit the rect.
        pygame.draw.rect(screen, self.color, self.rect, 2)



def main():
    clock = pygame.time.Clock()
    input_box2 = InputBox(190, 250, 200, 32)
    input_boxes = [input_box2]
    done = False

    while not done:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True
            for box in input_boxes:
                box.handle_event(event)

        for box in input_boxes:
            box.update()

        surface.fill((255, 70, 90))
        for box in input_boxes:
            box.draw(surface)

        pygame.display.flip()
        clock.tick(30)


if __name__ == '__main__':
    main()
    pygame.quit()

您想每秒减少计时器。因此,定时器时间必须是 1 秒(1000 毫秒)而不是 60 秒(参见pygame.time.set_timer):

pygame.time.set_timer(pygame.USEREVENT, 60000)

pygame.time.set_timer(pygame.USEREVENT, 1000)

另见

输入后需要重新设置t=60

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.USEREVENT:
            t -= 1
            temps = font.render("Temps:" + str(t), True, (255, 255, 255))
            surface.blit(temps, (0, 220))
            pygame.display.flip()
        else:
            result = input_box.handle_event(event)
            if result != None:
                if int(result) == int(x) + int(y):
                    points = points + 5
                    mixer.music.load('StarPost.wav')
                    mixer.music.play(1)

                # create new random numbers
                x, y = start_the_game()

                # reset input box (just create a new box)
                input_box = InputBox(190, 250, 200, 32)
                
                t = 60 # <--


    display_the_game(x, y)
    pygame.display.update()

pygame.quit()