Pygame不使用指定的字体(Pygame not using specified font)

其他开发 IT屋
百度翻译此文   有道翻译此文
问 题

So I am having a problem in pygame where I specify the font and size to use, but when my program is run, the font and size are the default.

Here is where I define the text

def font(self):
    '''*****FATAL - THIS LINE CAUSES ERROR (When compiled to .exe using cx_freeze)*****'''
    #font = pygame.font.SysFont("Coure", 20)
    font_color = (0, 0, 0)
    font = pygame.font.Font("coure.fon", 20)
    self.text = font.render("Level "+str(self.level) + " " + self.name + " Health " + str(self.health), True, font_color)

The line commented out under the "Fatal" comment works perfectly when run from the editor, however causes this error once it is run as an .exe

Fatal Python error: (pygame parachute) Segmentation Fault
Traceback (most recent call last):
File "C:\Python34\lib\site-packages\pygame\pkgdata.py", line 67, in   getResource
return open(os.path.normpath(path), 'rb')
FileNotFoundError: [Errno 2] No such file or directory:    'C:\\Users\\Jared\\Desktop\\Python\\Boss Battle\\build\\exe.win32-3.4\\library.zip\\pygame\\freesansbold.ttf'

I don't have the freesansbold font installed on my computer, but that shouldn't matter, since it should be looking for the coure font in the folder of the game. The weird thing is that I say "Coure" and the file is named "coure.fon", but when I write that, it is unrecognized.

I guess Im looking for some guidence on how to fix either the error, or how to define which text file I want it to use. Thanks for any help!

解决方案

Open up your pygame package folder. It should be something like C:\Python34\Lib\site-packages\pygame. There should be a True Type Font File titled freesansbold.ttf. Copy that file then open the folder containing your exe program. There should be a zipped file called library. Open it up and go to the pygame folder inside the zipped file. Should look something like this \build\exe.win32-3.4\library.zip\pygame. And just paste the freesansbold.ttf file in that folder and it should work perfectly. The freesansbold font comes with the pygame package, but for some reason py2exe and cx_Freeze don't include it, so you have to add it manually.

本文地址:IT屋 » Pygame not using specified font

问 题
所以我在pygame中指定了要使用的字体和大小,但是当我的程序运行时,字体和大小是默认的。



这里是我定义文本的地方



  def font(self):
'''*****致命的 - 这条线导致错误(当使用cx_freeze编译为.exe时)*****'''
#font = pygame.font.SysFont(“Coure”,20)
font_color =(0 ,0,0)
font = pygame.font.Font(“coure.fon”,20)
self.text = font.render(“Level”+ str(self.level)+“” + self.name +“Health”+ str(self.health),True,font_color)


在“致命”注释下注释掉的内容在编辑器中运行时是完美的,但是一旦它以.exe的形式运行,就会导致这个错误



 致命的Python错误:(pygame parachute)分段错误
Traceback(最近调用最后一个):
文件“C:\Python34\lib\site-packages\pygame\pkgdata .py“,第67行, getResource
return open(os.path.normpath(path),'rb')
FileNotFoundError:[Errno 2]没有这样的文件或目录:'C:\\ Users \\ Jared \桌面\Python \\\Boss Battle \\\\ build \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ '


我的计算机上没有安装freesansbold字体,因为它应该在游戏的文件夹中寻找coure字体。奇怪的是,我说“Coure”,文件名为“coure.fon”,但是当我写这个,它是无法识别的。



有关如何解决错误的一些指导,或者如何定义我希望使用哪个文本文件。感谢您的帮助!


解决方案
打开你的pygame包文件夹。它应该像 C:\Python34\Lib\site-packages\pygame 。应该有一个名为 freesansbold.ttf 的TrueType字体文件。复制该文件,然后打开包含您的exe程序的文件夹。应该有一个名为库的压缩文件。打开它并进入压缩文件中的pygame文件夹。应该看起来像这样 \build\exe.win32-3.4\library.zip\pygame 。只需将 freesansbold.ttf 文件粘贴到该文件夹​​中即可。 freesansbold字体自带pygame包,但由于某些原因, py2exe 和 cx_Freeze 不包含它,所以你必须手动添加它。


本文地址:IT屋 » Pygame不使用指定的字体

官方微信
扫一扫关注IT屋
微信公众号搜索 “ IT屋 ” ,选择关注
与百万开发者在一起