一段时间后做某事:Pygame

Doing something after a period of time: Pygame

想隔一段时间做点什么。在堆栈溢出时,我发现了一个有助于解决该问题的问题,(Link) 但是当我 运行 程序时代码可以运行,但是它会在一毫秒后消失。而我希望它在我希望它等待的时间后停留在那里。在这种情况下进行测试 运行 我将一些文本 blitting 到屏幕上。这是代码:

import pygame
# Importing the modules module.

pygame.init()
# Initializes Pygame

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
screen = pygame.display.set_mode((800, 600))
# Sets the screen to pygame looks and not normal python looks.

pygame.display.set_caption("Test Run")
# Changes the title

# Heading
headingfont = pygame.font.Font('Bouncy-PERSONAL_USE_ONLY.otf', 45)
headingX = 230
headingY = 10

class Other():
    def show_heading():
        Heading = headingfont.render("Health Run!", True, (255, 255, 255))
        screen.blit(Heading, (headingX, headingY))


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

running = True
while running:
    screen.fill((0,0,0))
 
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        if event.type == pygame.USEREVENT:
                Other.show_heading()

    #Update Display
    pygame.display.update()

如果要永久绘制文字,需要在应用程序循环中绘制。当定时器事件发生时,设置一个布尔变量“draw_text”。在应用程序循环中根据draw_text绘制文本:

draw_text = False

running = True
while running:
    screen.fill((0,0,0))
 
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        if event.type == pygame.USEREVENT:
            draw_text = True

    if draw_text:
        Other.show_heading()

    #Update Display
    pygame.display.update()

有关计时器和计时器事件的更多信息,请参阅 , or 等。