0

I am rendering some text from the surface, but it seems the text surface is filled with a color.

in-game capture

the white blocks and green block is text surface rendered to the main surface by the code below

import pygame.freetype as ft

font_dir = "./fonts/D2Coding.ttf"
font = ft.Font(font_dir, 24) # Font('fonts\D2Coding.ttf')

content = "some texts"
font_color = (255,255,255)

font_surf_rect = font.render(content, True, font_color) # (<Surface(34x18x32 SW)>, <rect(1, 18, 34, 18)>)

main_screen.blit(*font_surf_rect)

is there any method to make the text_surface background transparent, while keeping my text fully opaque?

  • EDIT (resolved)

I have used this font object well with the font.render_to function, and I am now on pygame 2.1.2

I just wanted to render my text to a given position as the center, so I just changed my code to use font.render_to and its position from font.render()[0].get_rect(center=given_position)

font_surf, font_rect = font_surf_rect

if center: rect = font_surf.get_rect(center=pos)
else:      rect = font_surf.get_rect(topleft=pos)

font.render_to(main_screen, rect[:2], text, font_color, size=font_size)

and this worked just fine.

Snoopy
  • 38
  • 5
  • The background is transparent by default. This is not a problem with the code, but with the system or the font. Likely the font cannot be found. – Rabbid76 Feb 03 '23 at 06:47
  • Try updating to pygame 2.1.3.dev8, if you're on a lower version. `pip install pygame --upgrade --pre`. – Starbuck5 Feb 03 '23 at 07:16

0 Answers0