【Pygame】 游戏开发 基础知识
创始人
2024-02-02 03:24:06
0

【Pygame】 第一课 游戏开发 基础知识

  • 概述
  • Pygame 的安装
  • Pygame 基础命令
    • pygame.locals 模块
    • pygame.init()
    • pygame.display.set_mode()
    • 案例
  • Pygame 显示文字
    • pygame.font.Font()
    • fill()
    • render()
    • blit()
    • pygame.display.update()
    • 案例
      • 显示英文
      • 显示中文

概述

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

在这里插入图片描述

Pygame 的安装

安装命令:

pip install pygame

导入 Pygame 包:

import pygame

在这里插入图片描述

Pygame 基础命令

pygame.locals 模块

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

导入所有常量

from pygame.locals import *

pygame.init()

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

例子:

# 导入模块
import pygame# 初始化 pygame
pygame.init()

pygame.display.set_mode()

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

格式:

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

参数:

  • 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))

在这里插入图片描述

案例

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()  # 退出

Pygame 显示文字

pygame.font.Font()

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

格式:

pygame.font.Font(filename, size)

参数:

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

例子:

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

fill()

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

格式:

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

参数:

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

例子:

screen.fill((0, 0, 200)

render()

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

格式:

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

参数:

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

例子:

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

blit()

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

格式:

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

参数:

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

例子:

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

pygame.display.update()

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

案例

显示英文

代码:

import pygame
from pygame.locals import *
import sysyellow = (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()  # 退出

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

显示中文

代码:

import pygame
from pygame.locals import *
import sysyellow = (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()  # 退出

输出结果:

在这里插入图片描述

相关内容

热门资讯

喜欢穿一身黑的男生性格(喜欢穿... 今天百科达人给各位分享喜欢穿一身黑的男生性格的知识,其中也会对喜欢穿一身黑衣服的男人人好相处吗进行解...
发春是什么意思(思春和发春是什... 本篇文章极速百科给大家谈谈发春是什么意思,以及思春和发春是什么意思对应的知识点,希望对各位有所帮助,...
网络用语zl是什么意思(zl是... 今天给各位分享网络用语zl是什么意思的知识,其中也会对zl是啥意思是什么网络用语进行解释,如果能碰巧...
为什么酷狗音乐自己唱的歌不能下... 本篇文章极速百科小编给大家谈谈为什么酷狗音乐自己唱的歌不能下载到本地?,以及为什么酷狗下载的歌曲不是...
华为下载未安装的文件去哪找(华... 今天百科达人给各位分享华为下载未安装的文件去哪找的知识,其中也会对华为下载未安装的文件去哪找到进行解...
怎么往应用助手里添加应用(应用... 今天百科达人给各位分享怎么往应用助手里添加应用的知识,其中也会对应用助手怎么添加微信进行解释,如果能...
家里可以做假山养金鱼吗(假山能... 今天百科达人给各位分享家里可以做假山养金鱼吗的知识,其中也会对假山能放鱼缸里吗进行解释,如果能碰巧解...
四分五裂是什么生肖什么动物(四... 本篇文章极速百科小编给大家谈谈四分五裂是什么生肖什么动物,以及四分五裂打一生肖是什么对应的知识点,希...
一帆风顺二龙腾飞三阳开泰祝福语... 本篇文章极速百科给大家谈谈一帆风顺二龙腾飞三阳开泰祝福语,以及一帆风顺二龙腾飞三阳开泰祝福语结婚对应...
美团联名卡审核成功待激活(美团... 今天百科达人给各位分享美团联名卡审核成功待激活的知识,其中也会对美团联名卡审核未通过进行解释,如果能...