【问题标题】:Text will not appear on the screen in pygamepygame中的文本不会出现在屏幕上
【发布时间】:2020-05-04 00:42:44
【问题描述】:

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

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 谁能告诉我为什么代码不允许我将文本blit到屏幕上,以及我应该如何更改代码以允许它将更多文本blit到屏幕上。

【问题讨论】:

  • 看图片,我假设你有一个pygame.display.update()。如果找不到,字体会引发错误,所以可能不是这样。确保在填满屏幕后对其进行 blitting。代码应该可以工作

标签: python text pygame pygame-surface


【解决方案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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多