-1
import pygame
from pygame.locals import *

pygame.init()
surf = pygame.display.set_mode((400,400))

pygame.draw.rect(
    surface = surf,
    color = (0,255,255),
    rect = (100,100,100,50)
    )

clock = pygame.time.Clock()

t = pygame.font.Font.render("Change Color",1,(255,255,255))


display.blit(
    source = t,
    dest = (100,100,100,50),
    area = None,
    special_flags = 0
    )

pygame.display.update()

The error message I am getting is this:

    t = pygame.font.Font.render("Change Color",1,(255,255,255))
TypeError: descriptor 'render' for 'pygame.font.Font' objects doesn't apply to a 'str' object

What am I doing wrong?

1 Answers1

0

I figured it out...

First of all, render does not take positional arguments, so we can't use those.

Link to documentation: my text won't blit to my display in pygame

Second, you have to specify the font to use.

I tried using this next:

import pygame
from pygame.locals import *

pygame.init()

font_game = pygame.font.SysFont("Arial",20)

t = pygame.font.Font.render(
    "Testing: ",
    1,
    (255,255,255)
    )

But this was erroring out

TypeError: descriptor 'render' for 'pygame.font.Font' objects doesn't apply to a 'str' object

If you look at the documentation located here:

https://www.pygame.org/docs/ref/font.html#pygame.font.Font.render

These are the arguments:

render(text, antialias, color, background=None)

Background is an optional argument and I specified 3 arguments, so what did I do wrong?

When you install pygame, one of the files that gets downloaded is the font.pyi file.

If you look at that file, you have this:

class Font(object):

    def __init__(self, name: Union[AnyPath, IO[Any], None], size: int) -> None: ...
    def render(
        self,
        text: str,
        antialias: bool,
        color: _ColorValue,
        background: Optional[_ColorValue] = None,
    ) -> Surface: ...

(There is more in that file, but that is the important part).

Not only do you have to specify the text, antialias, and color, but if you look at the first line in the class, def __init__, that's where it's telling you that you need to specify the font and size.

Once I changed my code to this, it worked fine:

font_game = pygame.font.SysFont("Arial",20)

t = pygame.font.Font.render(
    font_game,
    "Testing: ",
    1,
    (255,255,255)
    )