资讯专栏INFORMATION COLUMN

pygame学习笔记之设置字体及显示中文

89542767 / 398人阅读

  pygame作为python的一个平台库,在做游戏的时候,需要学习的内容还是比较的多的,主要涉及到的内容有相关的学习笔记,包括怎么设置字体,另外还有就是怎么设置其显示中文呢?下面就给大家详细解答下。


  一、获得可用字体


  import pygame
  print(pygame.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','extra','opensansregular','opensanssemibold','']


  二、字体的中英文对照


  一般的中文字体名,使用拼音即可,如仿宋fangsong,楷体kaiti


  新细明体:PMingLiU


  细明体:MingLiU


  标楷体:DFKai-SB


  黑体:SimHei


  宋体:SimSun


  新宋体:NSimSun


  仿宋:FangSong


  楷体:KaiTi


  仿宋_GB2312:FangSong_GB2312


  楷体_GB2312:KaiTi_GB2312


  微软正黑体:Microsoft JhengHei


  微软雅黑体:Microsoft YaHei


  三、设置字体


  import pygame,sys
  pygame.init()#pygame库的初始化
  root_sf=pygame.display.set_mode((480,600))#创建窗口,设置大小
  #显示文字
  print(pygame.font.get_fonts())
  font_name=pygame.font.match_font('fangsong')#2.获得字体文件
  font=pygame.font.Font(font_name,20)#1.获取font对象(需要字体文件)
  #绘制内容:text为内容,True为是否抗锯齿,WHITE是字体颜色
  font_surface=font.render('你好',True,'white')#3.将文字生成surface对象
  root_sf.blit(font_surface,(100,100))#4.将文字surface对象放到背景surface上
  while True:#阻止窗口关闭
  #事件判断
  for event in pygame.event.get():
  if event.type==pygame.QUIT:
  sys.exit()
  #刷新屏幕
  pygame.display.flip()

01.png

  四、拓展


  1.上方方法是匹配系统的字体


  2.匹配字体文件的字体

02.png

  import pygame,sys
  pygame.init()#pygame库的初始化
  root_sf=pygame.display.set_mode((480,600))#创建窗口,设置大小
  #显示文字
  print(pygame.font.get_fonts())
  #font_name=pygame.font.match_font('fangsong')#2.获得字体文件
  #font=pygame.font.Font(font_name,20)#1.获取font对象(需要字体文件)
  font=pygame.font.Font("simhei.ttf",20)#1.获取font对象(需要字体文件)
  #绘制内容:text为内容,True为是否抗锯齿,WHITE是字体颜色
  font_surface=font.render('你好',True,'white')#3.将文字生成surface对象
  root_sf.blit(font_surface,(100,100))#4.将文字surface对象放到背景surface上
  while True:#阻止窗口关闭
  #事件判断
  for event in pygame.event.get():
  if event.type==pygame.QUIT:
  sys.exit()
  #刷新屏幕
  pygame.display.flip()

  综上所述,这篇文章就给大家介绍到这里了,希望可以给大家带来一定帮助。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/128326.html

相关文章

  • pygame学习笔记设置字体显示中文

      游戏界面,国外的一些游戏,还是以英文为主,那么,国内的游戏,大多覆盖的是中文字体。那么,我们怎么样将英文字体修改成为中文,并且能够正确的显示出来呢?下面就给大家详细解答下。  一、获得可用字体  importpygame   print(pygame.font.get_fonts())  结果:  ['arial','arialblack','bahns...

    89542767 评论0 收藏0
  • 首次公开,整理12年积累的博客收藏夹,零距离展示《收藏夹吃灰》系列博客

    摘要:时间永远都过得那么快,一晃从年注册,到现在已经过去了年那些被我藏在收藏夹吃灰的文章,已经太多了,是时候把他们整理一下了。那是因为收藏夹太乱,橡皮擦给设置私密了,不收拾不好看呀。 ...

    Harriet666 评论0 收藏0
  • Python学习路13-记分

    摘要:之所以这里要添加这四行代码,其实是为了当你重新开始也就是第二次及以后点击按钮游戏时,计分板能正确显示。当第一运行游戏时,没有这四行也能正确显示计分板。 《Python编程:从入门到实践》笔记。本篇是Python小游戏《外星人入侵》的最后一篇。 1. 前言 本篇我们将结束Pygame小游戏《外星人入侵》的开发。在本篇中,我们将添加如下内容: 添加一个Play按钮,用于根据需要启动游戏以...

    tommego 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<