python - Pygame字体错误

标签 python fonts pygame

所以我决定开始制作游戏并对其进行了一些测试,然后我得到了这个错误:

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()

最佳答案

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

# imports

pygame.init() # now use display and fonts

关于python - Pygame字体错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28517979/

相关文章:

python - 使用 bool() 简化 'if' 语句

python - 如何在 Windows 下使用 netifaces 获取有意义的网络接口(interface)名称而不是 GUID?

Android - 带有自定义字体的 Html.fromHtml()

java - 如何仅更改 swing 应用程序的所有字体的大小?

python - ' bool 对象不可调用'pygame

python - 将 Sprite 片段集成到迷宫程序中

python - pygame.gfxdraw.aacircle 对于大圈子来说很有趣,还有其他选择吗?

python - 如何使用 Requests 和 JSON 打印变量

python - Python 中的悬挂缩进与垂直对齐

html - Chrome 无法正确显示我的表情符号