“Text has zero width” error after reinitializing Pygame display?

前端 未结 2 598
萌比男神i
萌比男神i 2021-01-07 15:17

I have a project in Pygame 1.9.2 where I reinitialize the display multiple times, and draw text to the display surface. It works fine until I close the Pygame display and r

相关标签:
2条回答
  • 2021-01-07 15:29

    The problem is that pygame.quit() was called before the program was finished. This causes every Pygame module (e.g. pygame.font) to be uninitialized.

    Instead of relying on pygame.quit to close the Pygame display, use pygame.display.quit at the end of every while running loop. Then put pygame.quit at the very end of the script to unload the rest of the modules after they're done being used.

    (Calling pygame.init() again before rendering text won't fix this issue either, because it causes the Pygame display to stop responding. (This might be a bug with Pygame 1.9.2)

    0 讨论(0)
  • 2021-01-07 15:34

    I found a solution that worked for me: just delete the font element before calling pygame.display.quit(), ie just del font every time you're done using it.

    The font element is the one you created using the command:

    font.SysFont("Comic Sans MS", 12)
    

    but that I personally create using pygame.font.Font(None, font_size)

    0 讨论(0)
提交回复
热议问题