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

目录

一、获得可用字体

二、字体的中英文对照

三、设置字体

四、拓展

总结

一、获得可用字体 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()

四、拓展

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

2.匹配字体文件的字体

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() 总结

到此这篇关于pygame学习笔记之设置字体及显示中文的文章就介绍到这了,更多相关pygame设置字体及显示中文内容请搜索易知道(ezd.cc)以前的文章或继续浏览下面的相关文章希望大家以后多多支持易知道(ezd.cc)!

推荐阅读