Search code examples
pythonfontspygame

how to use other fonts in pygame?


I've downloaded a font called redline.ttf and I want to use it in pygame. I just want to print text on my screen. I've found a basic font called 'freesansbold.ttf' in folder pygame -> lib. I've put my downloaded font in the same folder but when I use

fontObj = pygame.font.Font('redline.ttf', 16)
textSurfaceObj = fontObj.render('some text', True, (240,240,240), (115,117,117))
textRectObj = textSurfaceObj.get_rect()
textRectObj.center  = (350, 30)
DISPLAYSURF.blit(textSurfaceObj, textRectObj)
pygame.display.update()

i get this error:

OSError: unable to read font file 'redline.ttf'

Is it possible to use the redline font?


Solution

  • I think that your problem stems from the fact that your font path argument isn't correct, and that is why python can't find and use that file.

    From your problem description it's not clear what your program's working directory is. When pygame.font.Font() takes your path and tries to find the font relative to your working directory, it is likely it's looking for the font in your .py file directory. Try using an absolute path instead and see if that solves your problem.

    There is a much less likely possibility that your .ttf file is corrupted.

    (Documentation is here)