Pygame不显示文字

时间:2016-06-04 07:57:20

标签: python pygame

我的main()函数中有一些代码,用于显示我的游戏的倒计时器,但它没有显示剩余时间的文本。我运行游戏并且倒数计时器代码工作正常,但没有文字。请帮忙。

    def main():
        rand = random.randint(1, 4)
        frame_count = 0
        frame_rate = 60
        start_time = 60
        running = True
        while running:
            total_seconds = start_time - (frame_count // frame_rate)
            minutes = total_seconds // 60
            seconds = total_seconds % 60
            output_string = "Time left: {0:02}:{1:02}".format(minutes, seconds)
            screen.blit(font.render(output_string, True, WHITE), (150, 110))
            frame_count += 1
            clock.tick(frame_rate)

1 个答案:

答案 0 :(得分:2)

在绘制文本之前,您需要为屏幕着色。像这样:

screen.fill(BLACK)

然后你画出文字:

screen.blit(font.render(output_string, True, WHITE), (150, 110))

然后显示你必须写的所有内容:

pygame.display.flip()

最后应该是这样的:

def main():
    rand = random.randint(1, 4)
    frame_count = 0
    frame_rate = 60
    start_time = 60
    running = True
    while running:
        total_seconds = start_time - (frame_count // frame_rate)
        minutes = total_seconds // 60
        seconds = total_seconds % 60
        output_string = "Time left: {0:02}:{1:02}".format(minutes, seconds)

        screen.fill(BLACK)
        screen.blit(font.render(output_string, True, WHITE), (150, 110))
        pygame.display.flip()

        frame_count += 1
        clock.tick(frame_rate)