Python中的字体渲染错误

0 投票
1 回答
2238 浏览
提问于 2025-04-18 10:01

我现在正在用Python 3.2.3和Pygame为我的游戏渲染字体。最近我遇到了一个错误:

Traceback (most recent call last):
  File "E:\FinalProject.py", line 249, in <module>
    drawLevel1(screen, guy)
  File "E:\FinalProject.py", line 107, in drawLevel1
    text = font.render("Level : %s" % (lvlNum), 1, (0,0,0))
AttributeError: 'module' object has no attribute 'render'

出现在以下代码中:

pygame.font.init()
pygame.font.SysFont("Grobold", 20)
if lvlNum == level1 or lvlNum == level2 or lvlNum == level3:
    text = font.render("Level : %s" % (lvlNum), 1, (0,0,0))

我不知道这个错误为什么会发生。任何关于这个错误的帮助都非常感谢。

1 个回答

3

与其使用 pygame.font.SysFont("Grobold", 20) 这种方式,不如试试下面的方法:

pygame.font.init()
font = pygame.font.SysFont("Grobold", 20) #Assign it to a variable font
text = font.render("Hello", 1, (0,0,0)) #Call render from the font variable

撰写回答