iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >pygame学习笔记之设置字体及显示中文
  • 401
分享到

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

2024-04-02 19:04:59 401人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

目录一、获得可用字体二、字体的中英文对照三、设置字体四、拓展总结一、获得可用字体 import pygame print(pygame.font.get_fonts()) 结果:

一、获得可用字体

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

--结束END--

本文标题: pygame学习笔记之设置字体及显示中文

本文链接: https://www.lsjlt.com/news/119123.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作