Home > Mobile >  Blitting text with pygame2.1 not working correctly
Blitting text with pygame2.1 not working correctly

Time:12-22

I’m having some problems trying to blit text with pygame2.1.

Here’s some reproducible code:

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()


Blitting directly on the main window doesen’t seem to function as expected.enter image description here


But strangely enough, blitting the text on a second surface, and then blitting the surface itself on the main window does work!

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()

enter image description here

I don’t get why that’s the case. Is it a bug in pygame, or just a problem with my computer?

CodePudding user response:

The problem is not with the text, it is with pygame.Surface.fill. Replace

win.fill(0)

win.fill((0, 0, 0)

This is a known bug in the MacOS version of Pygame 2.1.0 and will be fixed in Pygame 2.1.1.
See Fix weird MacOS display surf weirdness #2859.

  • Related