網(wǎng)站首頁 編程語言 正文
一、查看可用字體
import pygame
print(pygame.font.get_fonts())
二、設(shè)置字體
1.使用系統(tǒng)字體
self.font=pygame.font.SysFont(None,48)#None系統(tǒng)默認(rèn)字體
self.font = pygame.font.SysFont("arial", 16)
2.使用字體文件
字體文件可以外下載
self.font = pygame.font.Font("my_font.ttf", 16) #字體,字號(hào)
3.設(shè)置中文()見前2
#系統(tǒng)自帶
self.font = pygame.font.SysFont("SimHei", 32) # 顯示中文
在網(wǎng)上下載一個(gè)中文字體文件,將這個(gè)文件與我們的程序放在同一個(gè)文件夾,如果是中文的文件名,將它改成英文文件名。例如,下載了迷你簡氈筆黑.TTF,將文件名改成了mnjzbh.ttf,并將程序的第一句改成:
self.font=pygame.font.Font('mnjzbh.ttf',32) # 顯示中文
三、附錄:常見系統(tǒng)字體對(duì)照表
一般的中文字體名,使用拼音即可,如 仿宋fangsong, 楷體kaiti
新細(xì)明體:PMingLiU
細(xì)明體:MingLiU
標(biāo)楷體:DFKai-SB
黑體:SimHei
宋體:SimSun
新宋體:NSimSun
仿宋:FangSong
楷體:KaiTi
仿宋_GB2312:FangSong_GB2312
楷體_GB2312:KaiTi_GB2312
微軟正黑體:Microsoft JhengHei
微軟雅黑體:Microsoft YaHei
一個(gè)應(yīng)用小例子
import pygame.font
class BUtton:
def __init__(self, ai_game, msg):
"""初始化按鈕屬性"""
# msg是要在按鈕上顯示的文本
self.screen = ai_game.screen
self.screen_rect = ai_game.screen.get_rect()
# 設(shè)置按鈕的尺寸和其他屬性
self.width, self.height = 200, 50
self.buttom_color=(0,255,0)
self.text_color=(255,255,255)
self.font=pygame.font.SysFont(None,48)
#創(chuàng)建按鈕的rect對(duì)象,并使其居中
self.rect=pygame.Rect(0,0,self.width,self.height)
self.rect.center=self.screen_rect.center
#按鈕的標(biāo)簽只創(chuàng)建一次
self._pre_msg(msg)
def _pre_msg(self,msg):
"""將msg渲染為圖像,并使其在按鈕上居中"""
self.msg_image=self.font.render(msg,True,self.text_color
,self.buttom_color)
#布爾實(shí)參指定開啟還是關(guān)閉反鋸齒功能,反鋸齒讓文本的邊緣更平滑
self.msg_image_rect=self.msg_image.get_rect()
#讓圖像文本在按鈕上居中
self.msg_image_rect.center=self.rect.center
def draw_button(self):
"""繪制一個(gè)用顏色填充的按鈕,再繪制文本"""
self.screen.fill(self.buttom_color,self.rect)
self.screen.blit(self.msg_image,self.msg_image_rect)
附:在 Pygame 屏幕中添加文字
font = pygame.font.SysFont("freesansbold.ttf", 30) # 30:font size
text = font.render("content", True, (0,0,0)) # (0,0,0) color of font
self.window.blit(text,(10,10)) # (10,10) rect left top
總結(jié)
原文鏈接:https://blog.csdn.net/huadong_xiaolin/article/details/126257202
相關(guān)推薦
- 2022-07-11 deepstream 問題
- 2022-07-15 SQL?Server中執(zhí)行動(dòng)態(tài)SQL_MsSql
- 2022-03-31 C語言與C++項(xiàng)目實(shí)現(xiàn)相互調(diào)用_C 語言
- 2022-10-27 go?熔斷原理分析與源碼解讀_Golang
- 2023-07-25 使用線程池異步執(zhí)行定時(shí)任務(wù)
- 2022-06-16 .Net?Core解決WebAPI中返回時(shí)間格式帶T的問題_實(shí)用技巧
- 2022-06-20 Python使用ffmpeg合成視頻、音頻的實(shí)現(xiàn)方法_python
- 2022-05-09 .NET中常見的加解密算法詳解_實(shí)用技巧
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支