脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - pygame学习笔记之设置字体及显示中文

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

2022-07-05 14:02IT技术学习 Python

游戏界面中文字也是非常常见的元素之一,pygame专门提供了Font模块来支持文字的显示,下面这篇文章主要给大家介绍了关于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()

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

四、拓展

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

2.匹配字体文件的字体

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

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设置字体及显示中文内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文地址:https://blog.csdn.net/tscaxx/article/details/122793223

延伸 · 阅读

精彩推荐