Python Pygame 字体错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28517979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pygame font error
提问by Silver Fire
So I decided to start making a game and I was testing it a bit and then I got this error:
所以我决定开始制作一个游戏,我正在测试它,然后我得到了这个错误:
Traceback (most recent call last):
File "TheAviGame.py", line 15, in <module>
font = pygame.font.Font(None,25)
pygame.error: font not initialized
I have no idea what I have done wrong so far... Code:
我不知道到目前为止我做错了什么......代码:
#!/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()
采纳答案by Malik Brahimi
You never initialized pygame
and pygame.font
after importing:
您从未初始化pygame
并pygame.font
在导入后:
# imports
pygame.init() # now use display and fonts
回答by kCODINGeroo
In order to use some pygame modules, either pygame or that specific module has to be initialised beforeyou start using them - this is what the error message is telling you.
为了使用某些 pygame 模块,必须在开始使用它们之前初始化 pygame 或该特定模块- 这就是错误消息告诉您的内容。
To initialize pygame:
初始化pygame:
import pygame
...
pygame.init()
To initialize a specific library (eg font):
初始化一个特定的库(例如字体):
import pygame
...
pygame.font.init()
In most cases, you will want to initialise all of pygame, so use the first version. In general, I would place this at the top of my code, just after importing pygame
在大多数情况下,您需要初始化所有 pygame,因此请使用第一个版本。一般来说,我会把它放在我的代码的顶部,就在导入 pygame 之后
回答by user11553057
You put None
in the font:
你None
输入字体:
font = pygame.font.Font(None, 25)
You can't do that. You have to put a type of font there.
你不能那样做。你必须在那里放一种字体。
And you should call pygame.init()
.
你应该打电话给pygame.init()
.