pygame.font.Font("宋体",80)
时间: 2025-06-22 16:48:20 浏览: 7
### 设置 Pygame 中的宋体字体
为了在 Pygame 中使用特定字体,如宋体并将其设置为指定大小(例如80),可以按照如下方法操作:
```python
import pygame
pygame.init()
# 使用系统中的宋体路径创建 Font 对象;对于 Windows 系统,宋体通常位于 'simhei'
font = pygame.font.Font("C:/Windows/Fonts/simsun.ttc", 80)
text_surface = font.render("你好, 世界!", True, (255, 255, 255))
screen = pygame.display.set_mode((400, 300))
screen.blit(text_surface, (50, 100))
pygame.display.flip()
```
上述代码展示了如何加载自定义 TTF 字体文件来创建 `Font` 实例[^1]。注意这里假设操作系统为 Windows 并指定了宋体的具体位置 `"C:/Windows/Fonts/simsun.ttc"`。
如果希望程序能够跨平台运行,则建议先尝试通过名称获取字体而不是硬编码其绝对路径:
```python
import os
import sys
import pygame
pygame.init()
if sys.platform.startswith('win'):
font_name = "simsun"
elif sys.platform.startswith('darwin'):
font_name = "/System/Library/Fonts/Songti.ttc"
else:
font_name = "wenquanyi-micro-hei"
try:
font = pygame.font.SysFont(font_name, 80)
except Exception as e:
print(f"未能找到 {font_name} 字体: {e}")
# 如果找不到指定字体则回退到默认字体
font = pygame.font.Font(None, 80)
text_surface = font.render("你好, 世界!", True, (255, 255, 255))
screen = pygame.display.set_mode((400, 300))
screen.blit(text_surface, (50, 100))
pygame.display.flip()
```
这段改进后的版本会根据不同操作系统自动选择合适的中文字体,并处理可能遇到的问题[^3]。
阅读全文
相关推荐


















