pygame中文乱码问题

Posted mez_Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了pygame中文乱码问题相关的知识,希望对你有一定的参考价值。

一开始在做Flappy Bird小游戏时面临中文乱码显示问题,如下所示:

一开始我还以为是Python编码问题,然而Python3中自动解决了中文显示问题,最简单的例子就是用print()语句进行打印,于是想起来是pygame界面显示中文的问题。

在通过网上查找的方式下,总结了3种方式,这里把自己探索的过程列举下来,并不是所有方式都能正常解决的,主要是展现出解决问题的方式,供大家参考。

1.采用font.get_fonts()函数查看系统字体。

这里将系统字体粘贴下来,每个字体代表什么意思,大家自行查阅,大部分都是微软提供的字体。

['arial', 'arialblack', 'bahnschrift', 'calibri', 'cambriacambriamath', 'cambria', 'candara', 'comicsansms', 'consolas', 'constantia', 'corbel', 'couriernew', 'ebrima', 'franklingothicmedium', 'gabriola', 'gadugi', 'georgia', 'impact', 'inkfree', 'javanesetext', 'leelawadeeui', 'leelawadeeuisemilight', 'lucidaconsole', 'lucidasans', 'malgungothic', 'malgungothicsemilight', 'microsofthimalaya', 'microsoftjhengheimicrosoftjhengheiui', 'microsoftjhengheimicrosoftjhengheiuibold', 'microsoftjhengheimicrosoftjhengheiuilight', 'microsoftnewtailue', 'microsoftphagspa', 'microsoftsansserif', 'microsofttaile', 'microsoftyaheimicrosoftyaheiui', 'microsoftyaheimicrosoftyaheiuibold', 'microsoftyaheimicrosoftyaheiuilight', 'microsoftyibaiti', 'mingliuextbpmingliuextbmingliuhkscsextb', 'mongolianbaiti', 'msgothicmsuigothicmspgothic', 'mvboli', 'myanmartext', 'nirmalaui', 'nirmalauisemilight', 'palatinolinotype', 'segoemdl2assets', 'segoeprint', 'segoescript', 'segoeui', 'segoeuiblack', 'segoeuiemoji', 'segoeuihistoric', 'segoeuisemibold', 'segoeuisemilight', 'segoeuisymbol', 'simsunnsimsun', 'simsunextb', 'sitkasmallsitkatextsitkasubheadingsitkaheadingsitkadisplaysitkabanner', 'sitkasmallsitkatextboldsitkasubheadingboldsitkaheadingboldsitkadisplayboldsitkabannerbold', 'sitkasmallsitkatextbolditalicsitkasubheadingbolditalicsitkaheadingbolditalicsitkadisplaybolditalicsitkabannerbolditalic', 'sitkasmallsitkatextitalicsitkasubheadingitalicsitkaheadingitalicsitkadisplayitalicsitkabanneritalic', 'sylfaen', 'symbol', 'tahoma', 'timesnewroman', 'trebuchetms', 'verdana', 'webdings', 'wingdings', 'yugothicyugothicuisemiboldyugothicuibold', 'yugothicyugothicuilight', 'yugothicmediumyugothicuiregular', 'yugothicregularyugothicuisemilight', 'dengxian', 'fangsong', 'kaiti', 'simhei', 'holomdl2assets', '方正粗黑宋简体', 'numberonly', 'acaslonproboldopentype', 'acaslonprobolditalicopentype', 'acaslonproitalicopentype', 'acaslonproregularopentype', 'acaslonprosemiboldopentype', 'acaslonprosemibolditalicopentype', 'adobefangsongstdregularopentype', 'adobefanheitistdboldopentype', 'adobegothicstdboldopentype', 'adobeheitistdregularopentype', 'adobekaitistdregularopentype', 'adobenaskhmediumopentype', 'agaramondproboldopentype', 'agaramondprobolditalicopentype', 'agaramondproitalicopentype', 'agaramondproregularopentype', 'birchstdopentype', 'blackoakstdopentype', 'brushscriptstdopentype', 'chaparralproboldopentype', 'chaparralprobolditopentype', 'chaparralproitalicopentype', 'chaparralprolightitopentype', 'chaparralproregularopentype', 'charlemagnestdboldopentype', 'cooperblackstditalicopentype', 'cooperblackstdopentype', 'giddyupstdopentype', 'hobostdopentype', 'kozgoproboldopentype', 'kozgoproextralightopentype', 'kozgoproheavyopentype', 'kozgoprolightopentype', 'kozgopromediumopentype', 'kozgoproregularopentype', 'kozminproboldopentype', 'kozminproextralightopentype', 'kozminproheavyopentype', 'kozminprolightopentype', 'kozminpromediumopentype', 'kozminproregularopentype', 'lithosproblackopentype', 'lithosproregularopentype', 'mesquitestdopentype', 'minionproboldcnopentype', 'minionproboldcnitopentype', 'minionpromediumopentype', 'minionpromediumitopentype', 'minionprosemiboldopentype', 'minionprosemibolditopentype', 'myriadarabicopentype', 'nuevastdboldopentype', 'nuevastdboldcondopentype', 'nuevastdboldconditalicopentype', 'nuevastdcondopentype', 'nuevastdconditalicopentype', 'nuevastditalicopentype', 'ocrastdopentype', 'oratorstdslantedopentype', 'oratorstdopentype', 'poplarstdopentype', 'prestigeelitestdbdopentype', 'rosewoodstdregularopentype', 'stencilstdopentype', 'tektonproboldopentype', 'tektonproboldcondopentype', 'tektonproboldextopentype', 'tektonproboldoblopentype', 'trajanproboldopentype', 'trajanproregularopentype', 'adobearabicboldopentype', 'adobearabicbolditalicopentype', 'adobearabicitalicopentype', 'adobearabicregularopentype', 'adobedevanagariboldopentype', 'adobedevanagaribolditalicopentype', 'adobedevanagariitalicopentype', 'adobedevanagariregularopentype', 'adobehebrewboldopentype', 'adobehebrewbolditalicopentype', 'adobehebrewitalicopentype', 'adobehebrewregularopentype', 'adobemingstdlightopentype', 'adobemyungjostdmediumopentype', 'adobesongstdlightopentype', 'kozgopr6nboldopentype', 'kozgopr6nextralightopentype', 'kozgopr6nheavyopentype', 'kozgopr6nlightopentype', 'kozgopr6nmediumopentype', 'kozgopr6nregularopentype', 'kozminpr6nboldopentype', 'kozminpr6nextralightopentype', 'kozminpr6nheavyopentype', 'kozminpr6nlightopentype', 'kozminpr6nmediumopentype', 'kozminpr6nregularopentype', 'lettergothicstdboldopentype', 'lettergothicstdboldslantedopentype', 'lettergothicstdslantedopentype', 'lettergothicstdopentype', 'minionproboldopentype', 'minionprobolditopentype', 'minionproitopentype', 'minionproregularopentype', 'myriadhebrewopentype', 'myriadproboldopentype', 'myriadproboldcondopentype', 'myriadproboldconditopentype', 'myriadprobolditopentype', 'myriadprocondopentype', 'myriadproconditopentype', 'myriadproitopentype', 'myriadproregularopentype', 'myriadprosemiboldopentype', 'myriadprosemibolditopentype', 'buxtonsketch', 'segoemarker', 'sketchflowprint', 'fzshuti', 'fzyaoti', 'lisu', 'stcaiyun', 'stfangsong', 'sthupo', 'stkaiti', 'stliti', 'stsong', 'stxihei', 'stxingkai', 'stxinwei', 'stzhongsong', 'youyuan', 'extra', 'arialms', 'century', 'wingdings2', 'wingdings3', 'icomoon']

当然,在这些字体中,用户也可以自己从网上下载字体,其实都在C:\\Windows\\Fonts路径下,只是这些字体的名称显示不同。

然后我们从列表中选择任一字体进行测试【还是用我们一开始用的Flappy Bird】:

这里我们选择列表中的唯一中文字体进行设置,也是成功的显示中文字体。结果如下:

 

 我们同样采取一样的方式换成任意其它字体进行测试:consolas为例

然后我们选择lisu【也就是隶书】:

也能正常显示,其它的就不再一一进行测试,有的字体支持中文,有的不支持,当然了,也和每一个字体的含义有关,如微软字体中The New Roman字体就是我们最常用的英文和特殊符号的格式。感兴趣的可以进行测试其它字体以及他们的含义。

2.使用绝对路径的方式

上面我们已经介绍过,因为这些字体的来源都是C:\\Windows\\Fonts路径下的字体文件,所以我们也可以使用绝对路径的形式进行传递参数。

我们就以楷体进行测试。

记住这里的文件名并不是叫楷体常规,而是我们查看属性得到的文件名!!!!

我们复制路径粘贴到pycharm中的代码中,

 这里我们发现不能正常显示,但是这里并不能直接否定不能直接显示,所以为了进行验证这种方式的可行性,我们专门用只有文本的pygame窗口进行显示,发现结果能正常显示。这里补充一下路径URL的斜杠问题,我们复制的路径是\\斜杠,而我们使用的是/斜杠,这里在正则表达式那一篇文章中已经介绍过,我们如果使用\\斜杠需要进行转义,否则Python解释器并不认识,解决方式是要么你用/斜杠,要么你使用\\斜杠,但需要在路径前面加上字母u,表示原生字符。如下所示:

运行结果就是上面显示的那样,中文也是可以正常显示的,在我们的Flappy Bird程序中,可能是本人水平有限,暂时没有找到其不能显示 的原因。

这里需要注意,如果使用系统字体Arial,这个字体我们在Word中最常见,但是很多初学者并不清楚这个字体,最简单的来说在微软字体中它经常被用来设置数字字体,而且他对特殊符号和中文不起作用。这里将百科贴下来做一个简单了解,感兴趣的可以自行进行搜索。

 我们在pycharm中进行验证。

很显然,这种字体不能用来设置中文字体,但是英文还是可以正常显示的。

这是在Word中的Arial字体,如果设置中文和标点符号,是不能设置的,只有数字可以设置为该字体。这里需要注意。

3.使用相对路径

与第二个方法类似,只不过URL使用的是相对路径,至于相对路径和绝对路径这里就不再进行介绍他们了,大部分人都知道,不知道的可以自行百度进行搜索。

这里介绍一下这种方式的应用场景,一般是能用相对路径是绝对不用绝对路径的,因为绝对路径是固定不变的,一旦文件的位置发生改变,绝对路径就会失效,而相对路径则不然。因此有了良好的可迁移性。还有一种常用的应用场景,就是我们可以把自己下载的字体文件进行设置,比如说我们下载的字体文件保存到了桌面,我们可以将此文件复制到我们写代码的Python文件的路径下,可以同一路径,也可以建立文件夹成为子路径,这一点根据需要进行设置即可。

好了,这些就是我为解决pygame界面显示中文乱码的过程,希望大家可以参考各种方法,找到适合自己的一种即可。

对了,最终再补充一点,如果使用默认的字体设置代码,中文也是不能正常显示的。

 

 

以上是关于pygame中文乱码问题的主要内容,如果未能解决你的问题,请参考以下文章

Vue get发送请求中文乱码问题

linux解决中文乱码问题

servlet处理中文乱码问题

解决获取url上中文乱码问题

Mysql存储过程中文乱码问题

怎样解决SQL数据库中文乱码问题