我正在尝试将 .woff 格式的字体文件加载到我的 pygame 中。我看过其他帖子谈论这个问题,但他们的回答无助于回答我的问题。我试过使用这个:
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)
这是我的错误:
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'
我已将此字体文件放在我当前的 python 文件目录中,如您在这张图片中所见:
Shows that the font files are in the same directory
而且我知道字体文件没有损坏,因为我已经看到了一个问题。
最佳答案
确保这不是损坏的字体或 Windows 兼容性问题的第一步是将 woff 转换为 ttf 并尝试让 pygame 加载该文件。我去了这个网站:https://everythingfonts.com/woff-to-ttf我用我自己的 woff 字体测试了它,它与 ttf 的效果一样。我在 Linux 上,我的字体/代码对我有用。
我会为您粘贴我的字体加载代码,以便您可以使用它进行测试以防万一。
如果仍然无法正常工作,请告诉我。如果是这样,请发布您的 Windows + python 版本,以便我可以重现。
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)
关于python - Pygame从外部字体文件加载字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38669178/