pygame游戏开发系列3-显示文字

游戏界面中文字也是非常常见的元素之一,pygame 专门提供了 Font 模块来支持文字的显示。文字在显示的时候支持系统字体,也支持自定义字体,文字内容可以缩放和旋转。
如果对pygame感兴趣想要系统学习,可以看看我录的pygame的视频: https://www.bilibili.com/video/BV1bE411p7Ue

"""__author__ = YuTing"""
import pygame

pygame.init()
window = pygame.display.set_mode((400, 600))
pygame.display.set_caption('显示文字')
window.fill((255, 255, 255))
# ==============显示文字============
# 1.创建字体对象(选笔)
# font.SysFont(字体名,字体大小, 是否加粗=False, 是否倾斜=False)   - 创建系统字体对象
# font.Font(字体文件路径, 字体大小)
# font = pygame.font.SysFont('Times', 30)
font = pygame.font.Font('files/t2.TTF', 20)

# 2.通过字体创建文字对象
# 字体对象.render(文字内容,True,文字颜色,背景颜色=None)
text = font.render('hello pygame', True, (0, 0, 255), (0, 255, 0))

# 3.显示文字
window.blit(text, (0, 0))

# 4.获取文字内容的宽度和高度
text_w, text_h = text.get_size()
window.blit(text, (400-text_w, 0))

# 5.文字缩放和旋转
new_text = pygame.transform.rotozoom(text, 45, 1.5)
window.blit(new_text, (100, 200))


pygame.display.flip()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()

在这里插入图片描述

发布了40 篇原创文章 · 获赞 11 · 访问量 1464

猜你喜欢

转载自blog.csdn.net/yuting209/article/details/105295398