Search code examples
pygamepygame-surface

How to fix the error, "TypeError: argument 1 must be pygame.surface.Surface, not tuple"


I am new to pygame and am trying to make a game with a bit of text, however when I run, I get the error, TypeError: argument 1 must be pygame.surface.Surface, not tuple. Here is my code:

import sys
import pygame
pygame.init()
# Make The Screen
screen = pygame.display.set_mode((1200,800))
screen_rect = screen.get_rect()
screen.fill((135, 206, 235))
# Font
font = pygame.freetype.SysFont('Times New Roman', 30)
while True:
    # make it work
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        screen.fill((250, 250, 0))
    # Try to make text happen
    ammo_text = font.render(f'Hello World', (0, 0, 100))
    screen.blit(ammo_text, (40, 250))
    pygame.display.flip() 

The error occurs in line 18 where I try to blit the text.

I tried a few different methods and the same error occured. I am open to ideas. P.S. I don't need to know how to make text.


Solution

  • The problem you're encountering is due to differences between pygame.Font.render() and pygame.freetype.Font.render().

    The freetype font method returns a tuple, a Surface and rect. So you can change your code to ignore the returned rect :

    ammo_text, _ = font.render(f'Hello World', (0, 0, 100))