Pygame Surface is considered as a tuple

Viewed 158

I'm trying to blit a text surface on my screen, but I get this error: TypeError: argument 1 must be pygame.Surface, not tuple. What could be wrong? I know for sure that my text is a surface.

import pygame
import pygame.freetype
pygame.init()
pygame.font.init()
screen = pygame.display.set_mode((100, 100))
font = pygame.freetype.Font('resource2.ttf', 20)
text_image = font.render("text", (255,255,255))
screen.blit(text_image,(0,0))
pygame.display.flip()
1 Answers

From the Manual of pygame.freetype.Font.render():

The return value is a tuple: the new surface and the bounding rectangle giving the size and origin of the rendered text.

So you need something like:

text_font = pygame.freetype.Font( None, 20 )
text_image, text_rect = text_font.render( "text", (255,255,255) )

[...]

screen.blit( text_image, ( 0, 0 ) )

As in ~

import pygame
import pygame.freetype

pygame.init()
pygame.font.init()
screen = pygame.display.set_mode( (200, 100) )
text_font = pygame.freetype.Font( None, 20 )
text_image, text_rect = text_font.render( "text", (255,255,255) )
print(str(text_image))

clock = pygame.time.Clock()
done = False
while not done:
    # Handle user-input
    for event in pygame.event.get():
        if ( event.type == pygame.QUIT ):
            done = True

    screen.fill( ( 0,0,0 ) ) # paint it black
    screen.blit( text_image, ( 0, 0 ) )
    pygame.display.flip()

    # Clamp FPS
    clock.tick_busy_loop(60)

pygame.quit()
Related