使用 pygame2.1 的 Blitting 文本无法正常工作

Blitting text with pygame2.1 not working correctly

我在尝试使用 pygame2.1.

位块传输文本时遇到一些问题

这是一些可重现的代码:

import pygame

pygame.init()

win = pygame.display.set_mode((500, 500))

font = pygame.font.SysFont("Arial", 50)
text = font.render("Test", True, (255, 255, 255))
text_rect = text.get_rect(center=(250, 250))

run = True
while run:
    for e in pygame.event.get():
        if e.type == pygame.QUIT:
            run = False

    win.fill(0)
    win.blit(text, text_rect)
    pygame.display.update()


直接在主 window 上 Blitting 似乎没有按预期运行。


但奇怪的是,在第二个表面上 blit 文本,然后在主表面上 blit 表面本身 window 确实有效!

import pygame

pygame.init()

win = pygame.display.set_mode((500, 500))
surf2 = pygame.Surface((400, 400))

font = pygame.font.SysFont("Arial", 50)
text = font.render("Test", True, (255, 255, 255))
text_rect = text.get_rect(center=(200, 200))

run = True
while run:
    for e in pygame.event.get():
        if e.type == pygame.QUIT:
            run = False

    win.fill(0)
    surf2.fill((128, 128, 128))
    surf2.blit(text, text_rect)
    win.blit(surf2, (50, 50))
    pygame.display.update()

我不明白为什么会这样。这是 pygame 中的错误,还是我的电脑有问题?

问题不在于文本,而在于 pygame.Surface.fill。替换

win.fill(0)

win.fill((0, 0, 0)

这是 MacOS 版本 Pygame 2.1.0 中的已知错误,将在 Pygame 2.1.1 中修复。
参见 Fix weird MacOS display surf weirdness #2859