【问题标题】:Pygame font errorPygame字体错误
【发布时间】:2015-04-15 13:55:01
【问题描述】:

所以我决定开始做一个游戏,我测试了一下,然后我得到了这个错误:

Traceback (most recent call last):
  File "TheAviGame.py", line 15, in <module>
    font = pygame.font.Font(None,25)
pygame.error: font not initialized

到目前为止,我不知道我做错了什么...... 代码:

#!/usr/bin/python
import pygame
blue = (25,25,112)
black = (0,0,0)
red = (255,0,0)
white = (255,255,255)
groundcolor = (139,69,19)
gameDisplay = pygame.display.set_mode((1336,768))
pygame.display.set_caption("TheAviGame")
direction = 'none'
clock = pygame.time.Clock()
img = pygame.image.load('player.bmp')
imgx = 1000
imgy = 100
font = pygame.font.Font(None,25)
def mts(text, textcolor, x, y):
    text = font.render(text, True, textcolor)
    gamedisplay.blit(text, [x,y])
def gameloop():
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
            if event.pygame == pygame.KEYDOWN:
                if event.key == pygame.RIGHT:
                    imgx += 10
        gameDisplay.fill(blue)
        pygame.display.update()
        clock.tick(15)
gameloop()

【问题讨论】:

    标签: python fonts pygame


    【解决方案1】:

    您在字体中输入None

    font = pygame.font.Font(None, 25)
    

    你不能这样做。 你必须在那里放一种字体。

    你应该打电话给pygame.init()

    【讨论】:

    • 据我所知,None 有效。它只是加载 Pygame 的默认字体。
    【解决方案2】:

    为了使用某些 pygame 模块,pygame 或特定模块必须在您开始使用它们之前进行初始化 - 这就是错误消息告诉您的内容。

    初始化pygame:

    import pygame
    ...
    pygame.init()
    

    要初始化一个特定的库(例如字体):

    import pygame
    ...
    pygame.font.init()
    

    在大多数情况下,你会想要初始化所有的 pygame,所以使用第一个版本。一般来说,我会把它放在我的代码顶部,就在导入 pygame 之后

    【讨论】:

      【解决方案3】:

      您在导入后从未初始化pygamepygame.font

      # imports
      
      pygame.init() # now use display and fonts
      

      【讨论】:

      • 如果你打电话给pygame.init,你不需要打电话给pygame.font.init(虽然这样做是无害的,但不是必需的)因为pygame.init会这样做代表你。如果您愿意,您可以在初始化整个pygame 之前调用pygame.font.init 进行任何类型的字体工作。正如pygame.org/docs/ref/pygame.html#pygame.init 解释的那样,“您可能希望分别初始化不同的模块以加快您的程序或不使用您的游戏不使用的东西。”。
      • 是的,我知道。我只是指出他没有初始化 pygame 以使用显示。字体是次要问题。
      • 我尝试在 img 中进行 blitting,我所做的是在 while true 下:我在它下面做了 gameDisplay.blit(img, (100,100)) 然后 pygame.display.update() 但现在它它就像闪烁了一吨...... @MalikBrahimi
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-19
      • 1970-01-01
      • 1970-01-01
      • 2021-08-03
      • 2019-01-10
      相关资源
      最近更新 更多