- Pygame 教程
- Pygame - 主页
- Pygame - 概述
- Pygame - 你好世界
- Pygame - 显示模式
- Pygame - 本地模块
- Pygame - 颜色对象
- Pygame - 事件对象
- Pygame - 键盘事件
- Pygame - 鼠标事件
- Pygame - 绘制形状
- Pygame - 加载图像
- Pygame - 在窗口中显示文本
- Pygame - 移动图像
- Pygame - 使用数字键盘移动
- Pygame - 用鼠标移动
- Pygame - 移动矩形对象
- Pygame - 使用文本作为按钮
- Pygame - 转换图像
- Pygame - 声音对象
- Pygame - 混合器通道
- Pygame - 播放音乐
- Pygame - 玩电影
- Pygame - 使用相机模块
- Pygame - 加载光标
- Pygame - 访问 CDROM
- Pygame - 精灵模块
- Pygame - PyOpenGL
- Pygame - 错误和异常
- Pygame 有用资源
- Pygame - 快速指南
- Pygame - 有用的资源
- Pygame - 讨论
Pygame - 在窗口中显示文本
要在 Pygame 窗口上显示文本,我们需要首先借助 pygame.font 模块中定义的 SysFont() 函数获取字体对象。
Fnt= SysFont(name, size, bold=False, italic=False)
通过 get_fonts() 函数可以获取当前机器安装的字体列表。
fonts = pygame.font.get_fonts() for f in fonts: print(f)
让我们定义一个表示 36 点大小的 Arial 字体的字体对象。
font = pygame.font.SysFont("Arial", 36)
接下来,我们获得一个新的 Surface 对象,用于使用 Font 对象的 render() 方法以新创建的字体渲染 Hello World 文本。
txtsurf = font.render("Hello, World", True, white)
第一个参数是单行字符串,第二个参数表示抗锯齿。如果设置为 False,则渲染图像为 8 位图像,如果为 true,则渲染图像为 24 位。还可以使用可选的背景颜色参数。
我们现在需要将文本 Surface 块传输到屏幕窗口的中心。
screen.blit(txtsurf,(200 - txtsurf.get_width() // 2, 150 - txtsurf.get_height() // 2))
例子
以下是完整的代码 -
import pygame
pygame.init()
screen = pygame.display.set_mode((400, 300))
done = False
white=(255,255,255)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
bg = (127,127,127)
while not done:
for event in pygame.event.get():
screen.fill(bg)
if event.type == pygame.QUIT:
done = True
font = pygame.font.SysFont("Arial", 36)
txtsurf = font.render("Hello, World", True, white)
screen.blit(txtsurf,(200 - txtsurf.get_width() // 2, 150 - txtsurf.get_height() // 2))
pygame.display.update()
输出
除了 SysFont() 方法之外,还可以从字体文件(具有 .ttf 扩展名)或指向 ttf 文件的 Python 文件对象获取 Font 对象。还可以使用 .ttc 文件构建字体对象。字体类定义了以下方法 -
| 大胆的() | 获取或设置字体是否应以粗体呈现。 |
| 斜体() | 获取或设置字体是否应以斜体呈现。 |
| 强调() | 获取或设置字体是否应带有下划线。 |
| 使成为() | 在新 Surface 上绘制文本 |
| 尺寸() | 计算渲染文本所需的大小 |
| 设置下划线() | 控制文本是否带有下划线 |
| 获取下划线() | 检查文本是否会带有下划线 |
| 设置粗体() | 启用粗体文本的假渲染 |
| 获取粗体() | 检查文本是否会呈现为粗体 |
| 设置斜体() | 启用斜体文本的假渲染 |
| 指标() | 获取每个角色的指标 |
| 获取斜体() | 检查文本是否会呈现斜体 |
| 获取线尺寸() | 获取字体文本的行距 |
| 获取高度() | 获取字体的高度 |
| 获取上升() | 获取字体的上升度 |
| get_descent() | 获取字体的下降 |
下面给出了使用 ttf 和 ttc 文件渲染文本的示例。
font1 = pygame.font.SysFont('chalkduster.ttf', 72)
img1 = font1.render('Hello World', True, BLUE)
font2 = pygame.font.SysFont('didot.ttc', 72)
img2 = font2.render('Hello Pygame', True, GREEN)
screen.blit(img1, (20, 50))
screen.blit(img2, (20, 120))
pygame.display.update()
在上面的示例中,预定义的字符串已被渲染为表面对象。但是,可以读取 KEYDOWN 事件的键值以交互方式输入字符串并显示它。
首先,我们渲染一个空字符串。接下来,我们定义边界矩形,然后定义一个与文本边界矩形重叠的光标矩形。KEYDOWN 事件中标识的每个击键都会附加到原始空字符串并重复呈现。
例子
以下代码最初显示一个空白窗口。按下的每个字母都会并排显示。
import pygame
pygame.init()
screen = pygame.display.set_mode((400, 300))
done = False
white=(255,255,255)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
bg = (127,127,127)
text=""
while not done:
for event in pygame.event.get():
screen.fill(bg)
if event.type == pygame.QUIT:
done = True
if event.type == pygame.KEYDOWN:
text=text+event.unicode
font = pygame.font.SysFont("Arial", 36)
img = font.render(text, True, white)
rect = img.get_rect()
cursor = pygame.Rect(rect.topright, (3, rect.height))
img = font.render(text, True, white)
rect.size=img.get_size()
cursor.topleft = rect.topright
screen.blit(img,(200 - img.get_width() // 2, 150 - img.get_height() // 2))
pygame.display.update()
输出
运行上面的代码并输入一些文本。示例输出如下 -
