为什么文本没有显示在 pygame 中?

Why isnt the text showing up in pygame?

我正在尝试让玩家计数器,如果玩家按下键盘上的向上箭头,我希望玩家上升一个并显示计数器上升和下降相同。但是计数器没有显示

import pygame
pygame.init()
pygame.display.set_mode()
pygame.font.init()
Red = (247, 12, 12)
White = (255,255,255)
Blue = (0, 157, 255)
Black=(0,0,0)
X = 1400
Y = 1000
players = 0
display_surface = pygame.display.set_mode((X, Y))
font = pygame.font.Font('freesansbold.ttf', 20)
text = font.render("Player one is:", True, Black, White)
textRect = text.get_rect()
textRect.center = (X // 4.4, Y // 3.6)
str(players)
while True:
    display_surface.fill(White)
    pygame.display.update()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit(); #sys.exit() if sys is imported
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_0:
                print("Hey, you pressed the key, '0'!")
            if event.key == pygame.K_1:
                print("Doing whatever")
            if event.key == pygame.K_UP:
                int(players)
                players = players+1
                str(players)
                text1 = font.render(players, True, Black, White)
                text1Rect = text1.get_rect()
                text1Rect.center = (X // 4.4, Y // 3)
                display_surface.blit(text, textRect)
                int(players)
pygame.display.update()

str(x)x和returns转换成一个字符串:

str(players)

str(players)

但是,您根本不需要它。您必须在清除显示之后和更新显示之前呈现文本。在应用程序循环之前初始化text1

while True:
    # [...]

    # clear display
    display_surface.fill(White)
   
    # render secene
    display_surface.blit(text, textRect)
    display_surface.blit(text1, text1Rect)
    
    # [...]
    
    # update display
    pygame.display.update()

在按下 UP 时更改并重新渲染 text1 表面:

if event.key == pygame.K_UP:
    players += 1
    text = font.render("Player one is: " + str(players), True, Black, White)
    textRect = text.get_rect(center = (X // 4.4, Y // 3))

完整示例:

import pygame
pygame.init()
pygame.font.init()
Red = (247, 12, 12)
White = (255,255,255)
Blue = (0, 157, 255)
Black=(0,0,0)
X = 1400
Y = 1000
players = 0
display_surface = pygame.display.set_mode((X, Y))
font = pygame.font.Font('freesansbold.ttf', 20)

text = font.render("Player one is:", True, Black, White)
textRect = text.get_rect(center = (X // 4.4, Y // 3.6))
text1 = font.render(str(players), True, Black, White)
text1Rect = text.get_rect(center = (X // 4.4, Y // 3))

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_0:
                print("Hey, you pressed the key, '0'!")
            if event.key == pygame.K_1:
                print("Doing whatever")
            if event.key == pygame.K_UP:
                players += 1
                text1 = font.render(str(players), True, Black, White)
                text1Rect = text.get_rect(center = (X // 4.4, Y // 3))

    display_surface.fill(White)
    display_surface.blit(text, textRect)
    display_surface.blit(text1, text1Rect)
    pygame.display.update()

pygame.quit()
exit()