SoFunction
Updated on 2024-12-12

Setting fonts and displaying Chinese in pygame study notes

一、获得可用字体

import pygame
 
print(.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', '*nbaiti', '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
 
()#pygameLibrary initialization
 
root_sf = .set_mode((480,600))#Create a window,Setting the size
 
#display text
print(.get_fonts())
font_name = .match_font('fangsong')  # 2.Getting font files
font = (font_name, 20)  # 1.gainfontboyfriend(Font files required)
# Drawing content:textcontent,TrueFor anti-aliasing or not, WHITEIt's the font color.
font_surface = ('Hello', True, 'white')  # 3.Generate the text surfaceboyfriend
root_sf.blit(font_surface, (100, 100))#4.textualsurfaceboyfriend Put it in the background.surfacefirst (of multiple parts)
 
while True:#Preventing windows from closing
    #event judgment
    for event in ():
        if  == :
            ()
 
    #refresh screen
    ()

IV. Expansion

1. The upper method is to match the system's fonts

2. Match the fonts of the font file

import pygame,sys
 
()Initialization of the #pygame library
 
root_sf = .set_mode((480,600))# Create window, set size
 
#Display text
print(.get_fonts())
# font_name = .match_font('fangsong') # 2. Get font file
# font = (font_name, 20) # 1. Get the font object (need font file)
font = ("", 20)  # 1. Get the font object (requires a font file)
 
# draw content: text is content, True is anti-aliasing or not, WHITE is font color
font_surface = ('Hello', True, 'white')  # 3. Generate the text into a surface object
root_sf.blit(font_surface, (100, 100))#4. Place the text surface object on top of the background surface.
 
while True:#Block windows from closing
    #Event Judgment
    for event in ():
        if  == :
            ()
 
    #Refresh the screen
    ()

summarize

to this article on the pygame learning notes to set the font and display the Chinese article is introduced to this, more related pygame set the font and display the Chinese content please search for my previous posts or continue to browse the following related articles I hope you will support me in the future more!