【发布时间】:2018-07-29 23:10:22
【问题描述】:
import sys, pygame as pg, random
class Game:
def __init__(self):
#initialize game window, etc
pg.init()
pg.font.init()
pg.mixer.init()
self.screen = pg.display.set_mode((800, 600))
pg.display.set_caption('myFirstGame')
self.running = True
self.font_name = pg.font.match_font('calibri')
def new(self):
#resets the game
self.score = 0
self.run()
def run(self):
#game loop
self.playing = True
while self.playing:
self.draw()
def draw(self):
#game loop draw
self.screen.fill(0, 0, 0)
self.all_sprites.draw(self.screen)
self.draw_text(str(self.score), 22, white, 800 / 2, 20)
#after drawing everything, flip the display
pg.display.flip()
def draw_text(self, text, size, color, x, y):
font = pg.font.Font(self.font_name, size)
text_surface = font.render(text, True, color)
text_rect = text_surface.get_rect()
text_rect.midtop = (x, y)
self.screen.blit(text_surface, text_rect)
g = Game()
while g.running:
g.new()
g.show_go_screen()
pg.quit()
所以我正在关注一个教程并收到此错误...
AttributeError: 模块 'pygame.font' 没有属性 'match_font'
我觉得这个错误与 pygame 的安装有关。我正在通过 MSVC 运行 python 并通过 View > Other Windows > Python Environments 安装 Pygame .. 我似乎无法让字体工作。我正在关注一个试图从中学习的教程,甚至复制粘贴了适用于教程创建者的代码并得到相同的错误。谁能指出我正确的方向?
顺便说一句……它不是整个代码……我删掉了很多与字体无关的东西……宽度、屏幕等变量都是整个代码的有效变量。 FONT_NAME 在 settings.py 中定义为 'calibri' ,它与整个代码一起导入..
【问题讨论】:
-
我得到一个不同的错误(在添加缺少的变量之后)
pygame.error: font not initialized。 Pygame 未初始化,因为您忘记了pg.init()此处的括号。检查是否可以解决您的错误。如果还是不行,需要发minimal, complete and verifiable example。 -
它在我的整个代码中.. 我只是在帖子中意外省略了它.. 让我编辑以反映这一点,以免造成混淆.. 在 MSVC 中我仍然存在同样的错误
-
感谢 skrx 的回复...我编辑了代码以反映我的错误重复
-
我无法重现该错误。尝试从命令行运行程序。并请发布完整的回溯(错误消息)。
-
这个错误似乎是 MSVC 独有的,所以我认为我为那个 IDE 安装 pygame 的方式有问题。所以我不知道该怎么想。我在不同的 IDE(Atom 1.28.2)中尝试过,它按预期工作