Coder
Coder

Reputation: 67

Pygame Loading fonts from external font file

I am trying to load a .woff formatted font file into my pygame. I have seen other posts talking about this issue, but their answers do not help answer my question. I tried using this:

fontObj = py.font.Font('brandon_blk-webfont.woff', 16)
        textSurfaceObj = fontObj.render(self.fact, True, black, None)
        textRectObj = textSurfaceObj.get_rect()
        textRectObj.center  = (x * 1.5, y  * 1.5)
        gameDisplay.blit(textSurfaceObj, textRectObj)

Here is my error:

Traceback (most recent call last):
  File "C:\Users\Sruthi\Desktop\Python\Pygame\Memorization Game\main.py", line 111, in <module>
    gameLoop()
  File "C:\Users\Sruthi\Desktop\Python\Pygame\Memorization Game\main.py", line 108, in gameLoop
    create_button(mouse, 50, 50)
  File "C:\Users\Sruthi\Desktop\Python\Pygame\Memorization Game\main.py", line 84, in create_button
    fontObj = py.font.Font('Memorization Game/brandon_blk-webfont.woff', 16)
OSError: unable to read font file 'Memorization Game/brandon_blk-webfont.woff'

I have placed this font file in my current python file directory as you can see in this picture:

Shows that the font files are in the same directory

Also I know the font file isn't corrupted as I have seen a problem can be.

Upvotes: 3

Views: 7762

Answers (1)

NuclearPeon
NuclearPeon

Reputation: 6059

The first step to ensuring this isn't a corrupted font or windows compatibility issue would be to convert woff to ttf and attempt got get pygame to load that file. I went to this site: https://everythingfonts.com/woff-to-ttf and I tested it with my own woff font and it works just the same with ttf. I'm on Linux and my font/code worked for me.

I'll paste my font-loading code for you so you can test with that just in case.

Please let me know if things are still not working. If so, post your Windows + python versions so I can reproduce.

import pygame, sys, os
from pygame.locals import *

pygame.init()
screen = pygame.display.set_mode((400, 300))

# font-related code:

fpsClock = pygame.time.Clock()
# https://www.behance.net/gallery/31268855/Determination-Better-Undertale-Font
font = pygame.font.Font(os.path.join("res", "fonts", 'DeterminationMonoWeb.ttf'), 16)

screen.blit(font.render(text, 0, (255, 240, 230)), (10, 10))
pygame.display.flip()
# Main game loop
while True:
    if pygame.event.wait().type in (QUIT, KEYDOWN, MOUSEBUTTONDOWN):
        break

    pygame.display.update()
    fpsClock.tick(60)

Upvotes: 2

Related Questions