pygame學(xué)習(xí)筆記之設(shè)置字體及顯示中文
一、獲得可用字體
import pygame print(pygame.font.get_fonts())
結(jié)果:
['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
三、設(shè)置字體
import pygame,sys pygame.init()#pygame庫的初始化 root_sf = pygame.display.set_mode((480,600))#創(chuàng)建窗口,設(shè)置大小 #顯示文字 print(pygame.font.get_fonts()) font_name = pygame.font.match_font('fangsong') # 2.獲得字體文件 font = pygame.font.Font(font_name, 20) # 1.獲取font對象(需要字體文件) # 繪制內(nèi)容:text為內(nèi)容,True為是否抗鋸齒, WHITE是字體顏色 font_surface = font.render('你好', True, 'white') # 3.將文字生成 surface對象 root_sf.blit(font_surface, (100, 100))#4.將文字surface對象 放到背景surface上 while True:#阻止窗口關(guān)閉 #事件判斷 for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() #刷新屏幕 pygame.display.flip()
四、拓展
1.上方方法是匹配系統(tǒng)的字體
2.匹配字體文件的字體
import pygame,sys pygame.init()#pygame庫的初始化 root_sf = pygame.display.set_mode((480,600))#創(chuàng)建窗口,設(shè)置大小 #顯示文字 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對象(需要字體文件) # 繪制內(nèi)容:text為內(nèi)容,True為是否抗鋸齒, WHITE是字體顏色 font_surface = font.render('你好', True, 'white') # 3.將文字生成 surface對象 root_sf.blit(font_surface, (100, 100))#4.將文字surface對象 放到背景surface上 while True:#阻止窗口關(guān)閉 #事件判斷 for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() #刷新屏幕 pygame.display.flip()
總結(jié)
到此這篇關(guān)于pygame學(xué)習(xí)筆記之設(shè)置字體及顯示中文的文章就介紹到這了,更多相關(guān)pygame設(shè)置字體及顯示中文內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Django數(shù)據(jù)庫如何在原有表中添加新字段
這篇文章主要介紹了Django數(shù)據(jù)庫如何在原有表中添加新字段問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09在python中用print()輸出多個格式化參數(shù)的方法
今天小編就為大家分享一篇在python中用print()輸出多個格式化參數(shù)的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07python tkinter庫實現(xiàn)氣泡屏保和鎖屏
這篇文章主要為大家詳細介紹了python tkinter庫實現(xiàn)氣泡屏保和鎖屏,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-07-07