美文网首页
显示文字

显示文字

作者: 匿隱 | 来源:发表于2019-02-13 15:48 被阅读0次

import pygame

def base_game():
    # 1.初始化pygame, 做准备工作
    pygame.init()

    # 2.创建游戏窗口
    window = pygame.display.set_mode((400, 600))
    # 设置窗口标题
    pygame.display.set_caption('游戏')

    # 设置窗口背景颜色
    window.fill((255, 255, 255))

    # ============显示文字===========
    """
    1.创建字体对象
    a.系统字体
    font.SysFont(字体名, 字体大小)  - 返回一个字体对象
    b.自定义字体
    font.Font(字体文件路径, 字体大小)
    """
    # font = pygame.font.SysFont('Times', 40)
    font = pygame.font.Font('images/font2.ttf', 40)
    """
    2.根据字体创建文字对象
    render(文字, True, 文字颜色)  - 返回一个文字对象(Surface)
    """
    text = font.render('hello世界!', True, (255, 0, 0))
    w, h = text.get_size()
    """
    3.显示文字
    blit(渲染对象, 坐标)
    """
    window.blit(text, (400-w, 600-h))

    # 想要对窗口内容进行的修改有效,必须执行以下操作
    pygame.display.flip()

    # 3.让游戏保持运行状态(游戏循环)
    while True:
        # 4.不断检测游戏过程中是否有事件的产生
        for event in pygame.event.get():
            # 只有当事件产生后才会进入for循环
            # print('======')
            if event.type == pygame.QUIT:
                # return
                # 退出!
                exit()


def main():
    base_game()

相关文章

网友评论

      本文标题:显示文字

      本文链接:https://www.haomeiwen.com/subject/docljqtx.html