如何在pygame中显示一些文本? [英] How to display some text in pygame?

查看:159
本文介绍了如何在pygame中显示一些文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在pygame项目上,想显示一些文本.我之前已经在另一个程序中完成了此操作,并且工作正常,但是当我在该项目中编写完全相同的内容时,就会出现此错误:

I am currently on a pygame project and wanted to display some text. I have done this before in another program and it works just fine but when I write the exact same thing in this project, it gives this error :

Traceback (most recent call last):
  File "C:\Users\Fazelifar\Desktop\Dot Game.py", line 197, in <module>
    myfont = pygame.font.SysFont(None, 15)
  File "C:\Users\Fazelifar\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pygame\sysfont.py", line 362, in SysFont
    return constructor(fontname, size, set_bold, set_italic)
  File "C:\Users\Fazelifar\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pygame\sysfont.py", line 285, in font_constructor
    font = pygame.font.Font(fontpath, size)
pygame.error: font not initialized

这是我的代码:

myfont = pygame.font.SysFont(None, 15)
def txt_display (txt , color , x , y):
   txt = lala.render(txt , True , black)
   main.blit(txt , (x , y))

请帮助我陷入困境

推荐答案

pygame必须先初始化,然后才能分别创建Font和SysFont的实例.更准确地说, pygame.font 模组必须初始化.

pygame has to be initialized, before a instance of Font respectively SysFont can be created. More accurate, the pygame.font modul has to be initialized.

pygame.font.init()

但是请注意, pygame.init()也会初始化 pygame.font 模块. pygame.init() 初始化所有导入的pygame模块.

But note, pygame.init() would initialize the pygame.font module, too. pygame.init() initialize all imported pygame modules.

pygame的实例名称.font.SysFont myfont ,而不是 lala .因此必须是:

The name of the instance of pygame.font.SysFont is myfont, rather than lala. So it has to be:

txt = lala.render(txt,True,黑色)

txt = myfont.render(txt , True , black)

这篇关于如何在pygame中显示一些文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆