问题
I am currently on a pygame project and wanted to display some text. I have done this before in another program and it works just fine but when I write the exact same thing in this project, it gives this error :
Traceback (most recent call last): File "C:\Users\Fazelifar\Desktop\Dot Game.py", line 197, in <module> myfont = pygame.font.SysFont(None, 15) File "C:\Users\Fazelifar\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pygame\sysfont.py", line 362, in SysFont return constructor(fontname, size, set_bold, set_italic) File "C:\Users\Fazelifar\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pygame\sysfont.py", line 285, in font_constructor font = pygame.font.Font(fontpath, size) pygame.error: font not initialized
and here is my code :
myfont = pygame.font.SysFont(None, 15)
def txt_display (txt , color , x , y):
txt = lala.render(txt , True , black)
main.blit(txt , (x , y))
plz help I am stuck
回答1:
pygame has to be initialized, before a instance of Font respectively SysFont can be created. More accurate, the pygame.font modul has to be initialized.
pygame.font.init()
But note, pygame.init()
would initialize the pygame.font
module, too. pygame.init() initialize all imported pygame modules.
The name of the instance of pygame.font.SysFont is myfont
, rather than lala
. So it has to be:
txt = lala.render(txt , True , black)
txt = myfont.render(txt , True , black)
来源:https://stackoverflow.com/questions/58695609/how-to-display-some-text-in-pygame