• 【Pygame】 游戏开发 基础知识


    概述

    Pygame 是一个跨平台的 Python 模块, 专为电子游戏设计. Pygame 在已经非常优秀的 SDL 库的基础上增加了许多功能.

    在这里插入图片描述

    Pygame 的安装

    安装命令:

    pip install pygame
    
    • 1

    导入 Pygame 包:

    import pygame
    
    • 1

    在这里插入图片描述

    Pygame 基础命令

    pygame.locals 模块

    pygame.locals模块包括了 pygame 中定义的各种常量.

    导入所有常量

    from pygame.locals import *
    
    • 1

    pygame.init()

    pygame.init()是启动 pygame 并初始化的命令, 类似 python 中的__init__.

    例子:

    # 导入模块
    import pygame
    
    # 初始化 pygame
    pygame.init()
    
    • 1
    • 2
    • 3
    • 4
    • 5

    pygame.display.set_mode()

    pygame.display.set_mode()是初始化 pygame 窗口的命令.

    格式:

    pygame.display.set_mode(size=(0, 0), flags=0, depth=0, display=0, vsync=0)
    
    • 1

    参数:

    • size: 窗口大小 (分辨率), 类型为元组, 分别表示宽和高
    • flags: 额外参数
    • depth: 位深 (色彩深度)
    • display: 显示模式, 可以使用 & 或 | 一次设置 2 种模式
      • pygame.FULLSCREEN: 全屏
      • pygame.DOUBLEBUF: OPENGL 兼容
      • pygame.HWSURFACE: 硬件加速
      • pygame.OPENGL: 可使用 OpenGl 的显示
      • pygame.RESIZABLE: 可调整大小的显示
      • pygame.SCALED: 适应电脑屏幕大小
      • pygame.SHOWN: 可视 (默认)
      • pygame.HIDDEN: 隐藏

    例子:

    # 显示一个分辨率 600*400 的窗口
    screen = pygame.display.set_mode((600, 400))
    
    • 1
    • 2

    在这里插入图片描述

    案例

    import pygame
    import sys
    
    # 导入pygame中的常量
    from pygame.locals import *
    
    # 初始化pygame
    pygame.init()
    
    # 设置游戏窗口的尺寸, set_mode 函数的参数用元组表示尺寸 (width和height)
    pygame.display.set_mode((600, 600))
    
    # 捕获游戏的事件
    typelist = [QUIT]
    
    while True:
        # 获取事件
        for event in pygame.event.get():
            # 接收到退出事件, 退出程序
            if event.type in typelist:
                sys.exit()  # 退出
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    Pygame 显示文字

    pygame.font.Font()

    pygame.font.Font()可以帮助我们来设置字体和字体大小.

    格式:

    pygame.font.Font(filename, size)
    
    • 1

    参数:

    • filename: 字体文件路径
    • size: 字体大小

    例子:

    # 设置字体和字号
    myFont = pygame.font.Font(None, 60)
    
    • 1
    • 2

    fill()

    screen.fill()用于填充 pygame 窗口背景色的命令.

    格式:

    screen.fill(color, rect=None, special_flags=0)
    
    • 1

    参数:

    • color: 背景的颜色, RGB
    • rect: 颜色填充的范围
    • special_flags: 额外参数

    例子:

    screen.fill((0, 0, 200)
    
    • 1

    render()

    Font.render()用于创建文本并转换为图像.

    格式:

    Font.render(text, antialias, color, background=None)
    
    • 1

    参数:

    • text: 文字
    • antialias: 反锯齿, True / False
    • color: 颜色, 格式 RGB
    • background: 背景颜色, 默认为 None

    例子:

    textImage = myFont.render("Hello Pygame", True, (255, 255, 0)
    
    • 1

    blit()

    screen.blit()用于将图像显示到我们要显示的地方.

    格式:

    screen.blit(source, dest, area=None, special_flags=0)
    
    • 1

    参数:

    • source: 需要移动的表面 (图像)
    • dest: 目标位置, 类型元组
    • area: 显示面积, 默认为 None
    • special_flags: 额外参数

    例子:

    screen.blit(textImage, (10, 60))
    
    • 1

    pygame.display.update()

    pygame.display.update()用于更新显示.

    案例

    显示英文

    代码:

    import pygame
    from pygame.locals import *
    import sys
    
    yellow = (255, 255, 0)  # 文字颜色
    blue = (0, 0, 200)  # 背景颜色
    
    # 初始化 pygame
    pygame.init()
    
    # 设置窗口尺寸
    screen = pygame.display.set_mode((600, 400))
    
    # 设置字体和字号
    myFont = pygame.font.Font(None, 60)
    
    # 将文字转换为图像, 消除锯齿
    textImage = myFont.render("Hello Pygame", True, yellow)
    
    # 填充背景
    screen.fill(blue)
    
    # 显示文字
    screen.blit(textImage, (10, 60))
    
    # 更新显示
    pygame.display.update()
    
    # 捕获游戏事件
    typelist = [QUIT]
    
    while True:
        # 获取事件
        for event in pygame.event.get():
            # 接收到退出事件, 退出程序
            if event.type in typelist:
                sys.exit()  # 退出
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    输出结果:
    在这里插入图片描述

    显示中文

    代码:

    import pygame
    from pygame.locals import *
    import sys
    
    yellow = (255, 255, 0)  # 文字颜色
    blue = (0, 0, 200)  # 背景颜色
    
    # 初始化 pygame
    pygame.init()
    
    # 设置窗口尺寸
    screen = pygame.display.set_mode((600, 400))
    
    # 设置字体和字号 (仿宋)
    myFont = pygame.font.Font("C:\Windows\Fonts\simfang.ttf", 60)
    
    # 将文字转换为图像, 消除锯齿
    textImage = myFont.render("你好 Pygame", True, yellow)
    
    # 填充背景
    screen.fill(blue)
    
    # 显示文字
    screen.blit(textImage, (10, 60))
    
    # 更新显示
    pygame.display.update()
    
    # 捕获游戏事件
    typelist = [QUIT]
    
    while True:
        # 获取事件
        for event in pygame.event.get():
            # 接收到退出事件, 退出程序
            if event.type in typelist:
                sys.exit()  # 退出
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    输出结果:

    在这里插入图片描述

  • 相关阅读:
    AcWing算法提高课-5.6.1同余方程
    靓仔的python机器学习入门2.2-特征工程-特征提取
    【云原生之k8s】K8s 管理工具 kubectl 详解(三)
    壹基金为爱同行走进绿水青山,为乡村儿童送去健康水
    Java设计模式中策略模式
    一篇文章带你学会Hadoop-3.3.4集群部署
    Pycharm 搭建 Django 项目 (非常详细)
    Java面试题:Spring中的循环依赖,给程序员带来的心理阴影
    javaEE幼儿园学生管理系统
    Python爬虫基础学习-互联网、HTTP与HTML
  • 原文地址:https://blog.csdn.net/weixin_46274168/article/details/127944837