Python3.10,文本不显示在 Pygame Window

Python3.10, Text Doesn't Show On Pygame Window

我正在尝试制作一个应用程序,您可以在其中输入分钟值,然后在 window 上弹出一个计时器。计时器没有出现。我正在使用 python 3.10。我是 python 的新手,所以如果你能修复它,请附上源代码。

import pygame
import time
import sys

pygame.init()

q = True
WIDTH = 800
HEIGHT = 600

clock = pygame.time.Clock()

myFont = pygame.font.SysFont("monospace", 35)

def countdown(t):
    t *= 60
    while t:
        min, sec = divmod(t, 60)
        timer = '{:02d}:{:02d}'.format(min, sec)
#
        screen = pygame.display.set_mode((WIDTH, HEIGHT))
        pygame.display.set_caption('PyTimerTwo,  By: ChanceMeteor515')
        screen.fill((0, 0, 0))
        text = str(timer)
        label = myFont.render(text, 1, (255, 255, 255))
        screen.blit(label, (WIDTH - 200, HEIGHT - 40))
#
    print("\nTime's Up")
    time.sleep(5400)

t = input("Enter Time In Minutes:")
countdown(int(t))

while q:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_h:
                pygame.mouse.set_visible(0)
            elif event.key == pygame.K_ESCAPE:
                sys.exit()

    clock.tick(120)
    pygame.display.update()

在你的 countdown 函数中,你忘记用 pygame.display.update() 更新显示,你也忘记添加事件循环,所以你将无法关闭 window.如果你添加这些,计时器就会出现。

修改后的代码:

import pygame
import time
import sys

pygame.init()

q = True
WIDTH = 800
HEIGHT = 600

clock = pygame.time.Clock()

myFont = pygame.font.SysFont("monospace", 35)

def countdown(t):
    t *= 60
    while t:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

        min, sec = divmod(t, 60)
        timer = '{:02d}:{:02d}'.format(min, sec)
#
        screen = pygame.display.set_mode((WIDTH, HEIGHT))
        pygame.display.set_caption('PyTimerTwo,  By: ChanceMeteor515')
        screen.fill((0, 0, 0))
        text = str(timer)
        label = myFont.render(text, True, (255, 255, 255))
        screen.blit(label, (WIDTH - 200, HEIGHT - 40))

        pygame.display.update()
#
    print("\nTime's Up")
    time.sleep(5400)

t = input("Enter Time In Minutes:")
countdown(int(t))

while q:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_h:
                pygame.mouse.set_visible(False)
            elif event.key == pygame.K_ESCAPE:
                sys.exit()

    clock.tick(120)
    pygame.display.update()