Why is my text string rendered as a solid/filled rectangle in Pygame?

Viewed 188

So I made a "Yo Mama" joke generator, and it does what it should be doing (It works if I delete all the Pygame-specific code and print it to the terminal/command line). But when I add Pygame and make an application, it shows me the text as a white rectangle. Visual Studio doesn't give me any errors, but the text is seen as a white rectangle. Take a look at the error

import random
import pygame
import os 
from yo_mama_jokes import da_jokes 
pygame.font.init()

WIDTH, HEIGHT = 750,750
WIN = pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption("Your mama joke generator")


def main():
    myfont = pygame.font.SysFont("monospace", 20)
    run = True
    joke = random.choice(da_jokes)
    while run:
        label = myfont.render(joke, 1, (255,255,255))
        WIN.blit(label, (15, 350))
        pygame.display.update()
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
main()
3 Answers

Use the pygame.SRCALPHA flag to give your text a transparent background:

label = myfont.render(joke, 1, (255,255,255), pygame.SRCALPHA)

Edit:
This code is actually using a black background behind the text. The 4th argument of render takes a color for the background, and since it doesn't recognize the pygame.SRCALPHA flag, it sets the background to black. This works in your situation, but will not work if you have a multicolored background.

The problem is not your code but your system. Your code works fine:

import pygame 
pygame.font.init()

WIDTH, HEIGHT = 750,750
WIN = pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption("Your mama joke generator")

background = pygame.Surface(WIN.get_size())
ts, w, h, c1, c2 = 50, *background.get_size(), (128, 128, 128), (64, 64, 64)
tiles = [((x*ts, y*ts, ts, ts), c1 if (x+y) % 2 == 0 else c2) for x in range((w+ts-1)//ts) for y in range((h+ts-1)//ts)]
[pygame.draw.rect(background, color, rect) for rect, color in tiles]

def main():
    myfont = pygame.font.SysFont("monospace", 100)
    run = True
    joke = "Joke"
    while run:
        WIN.blit(background, (0, 0))
        label = myfont.render(joke, 1, (255,255,255))
        WIN.blit(label, (15, 350))
        pygame.display.update()
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
main()

Note, that there is a known bug in the MacOS version of Pygame 2.1.0. fill(0). does not work correctly and must be replaced with fill((0, 0, 0)). This bug will be fixed in Pygame 2.1.1.
See Fix weird MacOS display surf weirdness #2859.

You have to add , pygame.SRCALPHA) after (255,255,255).

SRCALPHA creates an empty pixel alpha surface. The surface will now draw transparency if the color contains the fourth alpha value

This way the text will not be seen as a white rectangle when I add Pygame and create an application. You will have transparent every pixel of the surface:

import random
import pygame
import os 
from yo_mama_jokes import da_jokes 
pygame.font.init()

WIDTH, HEIGHT = 750,750
WIN = pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption("Your mama joke generator")


def main():
    myfont = pygame.font.SysFont("monospace", 20)
    run = True
    joke = random.choice(da_jokes)
    while run:
        label = myfont.render(joke, 1, (255,255,255), pygame.SRCALPHA)
        WIN.blit(label, (15, 350))
        pygame.display.update()
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
main()
Related