文字不会出现在pygame的屏幕上

时间:2020-05-04 00:42:44

标签: python text pygame pygame-surface

这是我用来尝试在屏幕上添加文本的代码

font = pygame.font.Font('freesansbold.ttf', 20)

TextX = 700
TextY = 100

def showText(x,y):
    text = font.render("random text", True, (255,0,0))
    screen.blit(text, (x,y))

# Game Loop
running = True
while running:
    showText(TextX,TextY)

I am trying to add the writing in the space on the right, in a column preferable 谁能告诉我为什么该代码不允许我将文本改写到屏幕上,以及我应该如何更改代码以使其将更多文本改写到屏幕上。

1 个答案:

答案 0 :(得分:1)

您需要使用pygame.display.update()更新显示。要在列中编写文本,可以循环调用函数。这是循环的更新版本,该循环在列中重复打印文本:

while running:
    window_height = pygame.display.get_surface().get_size()[1]
    for TextY in range(100, window_height, 100):
        showText(TextX,TextY)
    pygame.display.update()