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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值