【问题标题】:What does this error mean? Pygame Error: Font Not Initialized这个错误是什么意思? Pygame 错误:字体未初始化
【发布时间】:2019-09-19 22:49:42
【问题描述】:

我目前正在制作一个小游戏,但它显示了一个奇怪的错误消息。即:

'Pygame Error': Font Not Initialized

这是什么意思?

这是我的代码:

import pygame, random, sys, os, time
import sys
from pygame.locals import *
WINDOWWIDTH = 800
WINDOWHEIGHT = 600
levens = 3
dead = False
Mousevisible = False

def terminate():
    pygame.quit()
    sys.exit()

def waitForPlayerToPressKey():
    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                terminate()
            if event.type == KEYDOWN:
                if event.key == K_ESCAPE: #escape quits
                    terminate()
                return

def drawText(text, font, surface, x, y):
    textobj = font.render(text, 1, TEXTCOLOR)
    textrect = textobj.get_rect()
    textrect.topleft = (x, y)
    surface.blit(textobj, textrect)
#fonts
font = pygame.font.SysFont(None, 30)

#def gameover():
    #if dead == True:
        #pygame.quit

# set up pygame, the window, and the mouse cursor
pygame.init()
mainClock = pygame.time.Clock()
windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
pygame.display.set_caption('hit the blocks')
pygame.mouse.set_visible(Mousevisible)

# "Start" screen
drawText('Press any key to start the game.', font, windowSurface, (WINDOWWIDTH / 3) - 30, (WINDOWHEIGHT / 3))
drawText('And Enjoy', font, windowSurface, (WINDOWWIDTH / 3), (WINDOWHEIGHT / 3)+30)
pygame.display.update()
waitForPlayerToPressKey()
zero=0
    

这是错误:

Traceback (most recent call last):
  File "C:\Users\flori\AppData\Local\Programs\Python\Python37-32\schietspel.py", line 30, in <module>
    font = pygame.font.SysFont(None, 30)
  File "C:\Users\flori\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pygame\sysfont.py", line 320, in SysFont
    return constructor(fontname, size, set_bold, set_italic)
  File "C:\Users\flori\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pygame\sysfont.py", line 243, in font_constructor
    font = pygame.font.Font(fontpath, size)
pygame.error: font not initialized

【问题讨论】:

  • 如果您不提供您的代码(或其相关部分),我们将无法为您提供帮助。
  • 总是将完整的错误消息(完整的 Traceback)放在有问题的地方(作为文本,而不是屏幕截图)。还有其他有用的信息。它应该告诉你你在哪一行代码中遇到了问题。
  • 显示您的代码 - 我们无法在您的脑海中阅读。
  • 一开始您应该使用Google 来查找有关'Pygame Error': Font Not Initialized 的更多信息。也许有人已经有这个问题,他解决了。这样你就可以在几个小时前得到答案。

标签: python python-3.x pygame


【解决方案1】:

问题是您在初始化游戏之前设置了字体。要解决此问题,请将 font = pygame.font.SysFont(None, 30) 移到 pygame.init() 之后。

此外,为了让您的代码正常工作,您还需要定义 TEXTCOLOR。它应该是一个具有 RGB 值的元组,例如。 TEXTCOLOR = (255, 255, 255) 表示白色(您可以将其与WINDOWHEIGHT 放在顶部)。

【讨论】:

    【解决方案2】:

    我认为你必须初始化 pygame.font。

    from pygame.locals import * 之后尝试pygame.font.init()

    【讨论】:

      【解决方案3】:

      对我来说,在pygame.font.Font() 之前添加pygame.init() 解决了这个问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-01-23
        • 2018-11-29
        • 2011-03-01
        • 2012-11-06
        相关资源
        最近更新 更多