Pygame 文本不显示

问题描述

def score():
    global x_score,o_score
    
    scoreFont = pygame.font.SysFont('Comic Sans',20,bold=True)
    score = scoreFont.render(f'X   {x_score} : {o_score}   O',True,(0,0))
    screen.blit(score,((WIDTH / 2) - (score.get_width() / 2),HEIGHT + score.get_height()))
  • WIDTH 和 HEIGHT = 屏幕宽度和高度

  • 定义了 x 和 o 分数

  • pygame 和字体从一开始就初始化

  • 函数之后调用pygame.display.flip()

我的代码显示了其他所有内容,但是这个。谁能帮助我并告诉我我在尝试显示分数时做错了什么?

解决方法

分数在窗口外的底部。更改 y 坐标的计算。垂直位置是 HEIGHT - SCORE.get_height() 而不是 HEIGHT + SCORE.get_height()

screen.blit(SCORE,((WIDTH / 2) - (SCORE.get_width() / 2),HEIGHT + SCORE.get_height()))

screen.blit(SCORE,HEIGHT - SCORE.get_height()))