Python之Pygame游戏编程详解

一、介绍

1.1 定义

Pygame是一种流行的Python游戏开发库,它提供了许多功能,使开发人员可以轻松创建2D游戏。它具有良好的跨平台支持,可以在多个操作系统上运行,例如Windows,MacOS和Linux。在本文中,我们将介绍Pygame库的特点和用法,帮助您了解如何使用Pygame库进行游戏开发。它提供了诸多操作模块,比如图像模块(image)、声音模块(mixer)、输入/输出(鼠标、键盘、显示屏)模块等。

官方地址:https://www.pygame.org/

1.2 相关函数

1) pygame.sprite代表游戏图像的更高级别的对象

pygame.sprite.Sprite:可见游戏对象的简单基类。

pygame.sprite.WeakSprite:Sprite 的子类,弱引用其组。这意味着它所属的任何组如果在其他地方没有被引用,都会被自动垃圾收集。

pygame.sprite.DirtySprite:Sprite 的子类,具有更多的属性和功能。

pygame.sprite.Group:用于保存和管理多个 Sprite 对象的容器类。

pygame.sprite.WeakDirtySprite:WeakSprite 和 DirtySprite 的子类,结合了这两个类的优点。

pygame.sprite.RenderPlain:与 pygame.sprite.Group 相同

pygame.sprite.RenderClear:与 pygame.sprite.Group 相同

pygame.sprite.RenderUpdates:跟踪脏更新的组子类。

pygame.sprite.OrderedUpdates:RenderUpdates 子类,按添加顺序绘制 Sprite。

pygame.sprite.LayeredUpdates:LayeredUpdates 是一个精灵组,它像 OrderedUpdates 一样处理图层和绘制。

pygame.sprite.LayeredDirty:LayeredDirty 组适用于 DirtySprite 对象。子类 LayeredUpdates。

pygame.sprite.GroupSingle:容纳单个精灵的组容器。

pygame.sprite.spritecollide:查找组中与另一个精灵相交的精灵。

pygame.sprite.collide_rect:使用矩形检测两个精灵之间的碰撞。

pygame.sprite.collide_rect_ratio:使用按比例缩放的矩形来检测两个精灵之间的碰撞。

pygame.sprite.collide_circle:使用圆圈检测两个精灵之间的碰撞。

pygame.sprite.collide_circle_system:使用按比例缩放的圆圈来检测两个精灵之间的碰撞。

pygame.sprite.collide_mask:使用遮罩检测两个精灵之间的碰撞。

pygame.sprite.groupcollide:找到两组之间发生碰撞的所有精灵。

pygame.sprite.spritecollideany:简单测试精灵是否与组中的任何内容相交。

2)pygame.Surface 图像和屏幕的对象

pygame.Surface.blit:将一幅图像绘制到另一幅图像上

pygame.Surface.blitz:将许多图像绘制到另一个图像上

pygame.Surface.convert:更改图像的像素格式

pygame.Surface.convert_alpha:更改图像的像素格式,包括每个像素 alpha

pygame.Surface.copy:创建 Surface 的新副本

pygame.Surface.fill:用纯色填充表面

pygame.Surface.scroll:将表面图像移动到位

pygame.Surface.set_colorkey:设置透明色键

pygame.Surface.get_colorkey:获取当前透明色键

pygame.Surface.set_alpha:设置完整 Surface 图像的 alpha 值

pygame.Surface.get_alpha:获取当前Surface透明度值

pygame.Surface.lock:锁定 Surface 内存以进行像素访问

pygame.Surface.unlock:从像素访问中解锁 Surface 内存

pygame.Surface.mustlock:测试 Surface 是否需要锁定

pygame.Surface.get_locked:测试 Surface 当前是否已锁定

pygame.Surface.get_locks:获取 Surface 的锁

pygame.Surface.get_at:获取单个像素的颜色值

pygame.Surface.set_at:设置单个像素的颜色值

pygame.Surface.get_at_mapped:获取单个像素处的映射颜色值

pygame.Surface.get_palette:获取 8 位 Surface 的颜色索引调色板

pygame.Surface.get_palette_at:获取调色板中单个条目的颜色

pygame.Surface.set_palette:设置 8 位 Surface 的调色板

pygame.Surface.set_palette_at:设置 8 位 Surface 调色板中单个索引的颜色

pygame.Surface.map_rgb:将颜色转换为映射颜色值

pygame.Surface.unmap_rgb:将映射的整数颜色值转换为 Color

pygame.Surface.set_clip:设置Surface的当前剪切区域

pygame.Surface.get_clip:获取Surface当前的裁剪区域

pygame.Surface.subsurface:创建一个引用其父级的新曲面

pygame.Surface.get_parent:找到次表面的父级

pygame.Surface.get_abs_parent:找到地下表面的顶层父级

pygame.Surface.get_offset:找到父级内部子表面的位置

pygame.Surface.get_abs_offset:查找子表面在其顶级父表面内的绝对位置

pygame.Surface.get_size:获取 Surface 的尺寸

pygame.Surface.get_width:获取 Surface 的宽度

pygame.Surface.get_height:获取 Surface 的高度

pygame.Surface.get_rect:获取 Surface 的矩形面积

pygame.Surface.get_bitsize:获取 Surface 像素格式的位深度

pygame.Surface.get_bytesize:获取每个 Surface 像素使用的字节数

pygame.Surface.get_flags:获取用于 Surface 的附加标志

pygame.Surface.get_pitch:获取每个 Surface 行使用的字节数

pygame.Surface.get_masks:在颜色和映射整数之间转换所需的位掩码

pygame.Surface.set_masks:设置在颜色和映射整数之间转换所需的位掩码

pygame.Surface.get_shifts:颜色和映射整数之间转换所需的位移位

pygame.Surface.set_shifts:设置颜色和映射整数之间转换所需的位移位

pygame.Surface.get_losses:用于在颜色和映射整数之间转换的有效位

pygame.Surface.get_bounding_rect:找到包含数据的最小矩形

pygame.Surface.get_view:返回 Surface 像素的缓冲区视图。

pygame.Surface.get_buffer:获取 Surface 像素的缓冲区对象。

pygame.Surface._pixels_address:像素缓冲区地址

pygame.Surface.premul_alpha:返回表面的副本,其中 RGB 通道预先乘以 alpha 通道。

3)pygame.surfarray操作图像像素数据

pygame.surfarray.array2d:将像素复制到二维数组中

pygame.surfarray.pixels2d:将参考像素放入二维数组中

pygame.surfarray.array3d:将像素复制到 3d 数组中

pygame.surfarray.pixels3d:将参考像素放入 3d 数组中

pygame.surfarray.array_alpha:将像素 alpha 复制到二维数组中

pygame.surfarray.pixels_alpha:将像素 alpha 参考到二维数组中

pygame.surfarray.array_red:将红色像素复制到二维数组中

pygame.surfarray.pixels_red:将红色参考像素放入二维数组中。

pygame.surfarray.array_green:将绿色像素复制到二维数组中

pygame.surfarray.pixels_green:将绿色参考像素放入二维数组中。

pygame.surfarray.array_blue:将蓝色像素复制到二维数组中

pygame.surfarray.pixels_blue:将蓝色参考像素放入二维数组中。

pygame.surfarray.array_colorkey:将 colorkey 值复制到二维数组中

pygame.surfarray.make_surface:将数组复制到新表面

pygame.surfarray.blit_array:直接从数组值进行 Blit

pygame.surfarray.map_array:将 3d 数组映射到 2d 数组

pygame.surfarray.use_arraytype:设置用于表面阵列的阵列系统

pygame.surfarray.get_arraytype:获取当前活动的数组类型。

pygame.surfarray.get_arraytypes:获取当前支持的阵列系统类型。

4)python管理pygame的顶级功能

pygame.init:初始化所有导入的 pygame 模块

pygame.quit:取消初始化所有 pygame 模块

pygame.get_init:如果 pygame 当前已初始化,则返回 True

pygame.error:标准 pygame 异常

pygame.get_error:获取当前错误信息

pygame.set_error:设置当前错误信息

pygame.get_sdl_version:获取SDL的版本号

pygame.get_sdl_byteorder:获取SDL的字节顺序

pygame.register_quit:注册一个在 pygame 退出时调用的函数

pygame.encode_string:对 Unicode 或 bytes 对象进行编码

pygame.encode_file_path:将 Unicode 或 bytes 对象编码为文件系统路径

5)pygame.BufferProxy 表面像素的阵列协议视图

该类是Surface对象通过数组协议导出的一个缓冲对象

pygame.BufferProxy.parent:返回该 BufferProxy 的 Surface 对象, 或者调用 BufferProxy 的对象

pygame.BufferProxy.length:返回缓冲区的长度,单位是字节。类似于len字段

pygame.BufferProxy.length:将缓冲区的数据拷贝并返回,为str或bytes对象

pygame.BufferProxy.write:将字节写入缓冲区

6)pygame.Color颜色表示

该类是pygame中用于描述颜色的模块

pygame.Color.r:取或设置 Color 的红色值。

pygame.Color.g:取或设置 Color 的绿色值。

pygame.Color.b:取或设置 Color 的蓝色值。

pygame.Color.a:取或设置 Color 的 alpha 值。

pygame.Color.cmy:取或设置 Color 的 CMY 表示形式。

pygame.Color.white:取或设置 Color 的 HSVA 表示形式。

pygame.Color.hsla:取或设置 Color 的 HSLA 表示形式。

pygame.Color.i1i2i3:取或设置 Color 的 I1I2I3 表示形式。

pygame.Color.normalize:返回颜色的标准化 RGBA 值。

pygame.Color. Correct_gamma:将特定的伽马值应用于颜色。

pygame.Color.set_length:将 Color 中的元素数量设置为 1、2、3 或 4。

pygame.Color.grayscale:返回颜色的灰度

pygame.Color.lerp:返回给定 Color 的线性插值。

pygame.Color.premul_alpha:返回一个 Color,其中 r、g、b 分量已乘以 alpha。

pygame.Color.update:设置颜色元素

7)pygame.cursors加载和编译光标图像

pygame.cursors.compile:从简单字符串创建二进制游标数据

pygame.cursors.load_xbm:从 XBM 文件加载游标数据

pygame.cursors.Cursor:代表光标的 pygame 对象

8)pygame.display配置显示表面

pygame.display.init:初始化显示模块

pygame.display.quit:取消初始化显示模块

pygame.display.get_init:如果显示模块已初始化,则返回 True

pygame.display.set_mode:初始化窗口或屏幕以进行显示

pygame.display.get_surface:获取当前设置的显示表面的引用

pygame.display.flip:将完整显示 Surface 更新到屏幕

pygame.display.update:更新软件显示的部分屏幕

pygame.display.get_driver:获取pygame显示后端的名称

pygame.display.Info:创建视频显示信息对象

pygame.display.get_wm_info:获取有关当前窗口系统的信息

pygame.display.get_desktop_sizes:获取活动桌面的大小

pygame.display.list_modes:获取可用全屏模式的列表

pygame.display.mode_ok:为显示模式选择最佳颜色深度

pygame.display.gl_get_attribute:获取当前显示器的 OpenGL 标志的值

pygame.display.gl_set_attribute:请求显示模式的 OpenGL 显示属性

pygame.display.get_active:当屏幕上的显示处于活动状态时返回 True

pygame.display.iconify:图标化显示表面

pygame.display.toggle_fullscreen:在全屏和窗口显示之间切换

pygame.display.set_gamma:更改硬件伽玛斜坡

pygame.display.set_gamma_ramp:使用自定义查找更改硬件伽玛斜坡

pygame.display.set_icon:更改显示窗口的系统图像

pygame.display.set_caption:设置当前窗口标题

pygame.display.get_caption:获取当前窗口标题

pygame.display.set_palette:设置索引显示的显示调色板

pygame.display.get_num_displays:返回显示数量

pygame.display.get_window_size:返回窗口或屏幕的大小

pygame.display.get_allow_screensaver:返回是否允许屏幕保护程序运行。

pygame.display.set_allow_screensaver:设置屏保是否可以运行

9)pygame.draw在曲面上绘制简单的形状,例如直线和椭圆

pygame.draw.rect:画一个矩形

pygame.draw.polygon:画一个多边形

pygame.draw.circle:画一个圆圈

pygame.draw.ellipse:画一个椭圆

pygame.draw.arc:画一条椭圆弧

pygame.draw.line:画一条直线

pygame.draw.lines:绘制多条连续的直线段

pygame.draw.aline:画一条抗锯齿直线

pygame.draw.aalines:绘制多个连续的抗锯齿直线段

10)pygame.event管理来自各种输入设备和窗口平台的传入事件

pygame.event.pump:内部处理 pygame 事件处理程序

pygame.event.get:从队列中获取事件

pygame.event.poll:从队列中获取单个事件

pygame.event.wait:等待队列中的单个事件

pygame.event.peek:测试事件类型是否正在队列中等待

pygame.event.clear:从队列中删除所有事件

pygame.event.event_name:从事件 ID 中获取字符串名称

pygame.event.set_blocked:控制队列中允许哪些事件

pygame.event.set_allowed:控制队列中允许哪些事件

pygame.event.get_blocked:测试某种类型的事件是否被队列阻止

pygame.event.set_grab:控制与其他应用程序共享输入设备

pygame.event.get_grab:测试程序是否共享输入设备

pygame.event.set_keyboard_grab:抓取可以捕获系统键盘快捷键,例如 Alt+Tab 或 Meta/Super 键。

pygame.event.get_keyboard_grab:获取当前键盘抓取状态

pygame.event.post:将新事件放入队列中

pygame.event.custom_type:制作自定义用户事件类型

pygame.event.Event:用于表示事件的 pygame 对象

11)pygame.examples各种程序演示了各个 pygame 模块的使用

pygame.examples.alien.main:播放完整的外星人示例

pygame.examples.stars.main:运行一个简单的星空示例

pygame.examples.chimp.main:击中移动的黑猩猩

pygame.examples.moveit.main:在屏幕上显示动画对象

pygame.examples.fonty.main:运行字体渲染示例

pygame.examples.freetype_misc.main:运行 FreeType 渲染示例

pygame.examples.vgrade.main:显示垂直渐变

pygame.examples.eventlist.main:显示 pygame 事件

pygame.examples.arraymo.main:显示各种冲浪阵列效果

pygame.examples.sound.main:加载并播放声音

pygame.examples.sound_array_demos.main:播放各种sndarray效果

pygame.examples.liquid.main:显示动画液体效果

pygame.examples.glcube.main:使用 OpenGL 显示动画 3D 立方体

pygame.examples.scrap_clipboard.main:访问剪贴板

pygame.examples.mask.main:使用碰撞检测显示多个图像相互反弹

pygame.examples.testsprite.main:显示大量移动的精灵

pygame.examples.headless_no_windows_needed.main:写入一个图像文件,该图像文件是输入文件的平滑缩放副本

pygame.examples.joystick.main:演示操纵杆功能

pygame.examples.blend_fill.main:演示各种 Surface.fill 方法混合选项

pygame.examples.blit_blends.main:使用 Surface.fill 的替代添加剂填充

pygame.examples.cursors.main:显示两个不同的自定义光标

pygame.examples.pixelarray.main:显示各种像素阵列生成的效果

pygame.examples.scaletest.main:使用 smoothscale 交互缩放图像

pygame.examples.midi.main:运行一个 MIDI 示例

pygame.examples.scroll.main:运行一个显示放大图像的 Surface.scroll 示例

pygame.examples.camera.main:显示从连接的摄像机实时捕获的视频

pygame.examples.playmus.main:播放音频文件

12)pygame.font加载和渲染 TrueType 字体

pygame.font.init:初始化字体模块

pygame.font.quit:取消初始化字体模块

pygame.font.get_init:true 如果字体模块已初始化

pygame.font.get_default_font:获取默认字体的文件名

pygame.font.get_sdl_ttf_version:获取SDL_ttf版本

pygame.font.get_fonts:获取所有可用的字体

pygame.font.match_font:在系统上查找特定字体

pygame.font.SysFont:从系统字体创建一个 Font 对象

pygame.font.Font:从文件创建一个新的 Font 对象

13)pygame.freetype 增强的 pygame 模块用于加载和渲染字体

pygame.freetype.get_error:返回最新的 FreeType 错误

pygame.freetype.get_version:返回 FreeType 版本

pygame.freetype.init:初始化底层 FreeType 库。

pygame.freetype.quit:关闭底层 FreeType 库。

pygame.freetype.get_init:如果 FreeType 模块当前已初始化,则返回 True。

pygame.freetype.was_init:已弃用:使用 get_init() 代替。

pygame.freetype.get_cache_size:返回字形大小写大小

pygame.freetype.get_default_resolution:返回默认像素大小(以每英寸点数为单位)

pygame.freetype.set_default_resolution:设置模块的默认像素大小(以每英寸点数为单位)

pygame.freetype.SysFont:从系统字体创建一个 Font 对象

pygame.freetype.get_default_font:获取默认字体的文件名

pygame.freetype.Font:从支持的字体文件创建新的 Font 实例。

14)pygame.gfxdraw抗锯齿绘制函数

pygame.gfxdraw.pixel:绘制一个像素

pygame.gfxdraw.hline:画一条水平线

pygame.gfxdraw.vline:画一条垂直线

pygame.gfxdraw.line:画一条线

pygame.gfxdraw.rectangle:画一个矩形

pygame.gfxdraw.box:绘制一个填充的矩形

pygame.gfxdraw.circle:画一个圆圈

pygame.gfxdraw.aacircle:绘制一个抗锯齿圆

pygame.gfxdraw.filled_circle:画一个实心圆

pygame.gfxdraw.ellipse:画一个椭圆

pygame.gfxdraw.aaellipse:绘制一个抗锯齿椭圆

pygame.gfxdraw.filled_ellipse:绘制一个实心椭圆

pygame.gfxdraw.arc:画一个圆弧

pygame.gfxdraw.pie:画一个饼

pygame.gfxdraw.trigon:画一个三角形/三角形

pygame.gfxdraw.atrigon:绘制抗锯齿三角形/三角形

pygame.gfxdraw.filled_trigon:绘制实心三角形/三角形

pygame.gfxdraw.polygon:画一个多边形

pygame.gfxdraw.aapolygon:绘制抗锯齿多边形

pygame.gfxdraw.filled_polygon:绘制一个填充多边形

pygame.gfxdraw.textured_polygon:绘制一个有纹理的多边形

pygame.gfxdraw.bezier:绘制贝塞尔曲线

15)pygame.image加载、保存和传输表面

pygame.image.load:从文件(或类似文件的对象)加载新图像

pygame.image.save:将图像保存到文件(或类似文件的对象)

pygame.image.get_sdl_image_version:获取正在使用的 SDL_Image 库的版本号

pygame.image.get_extended:测试是否可以加载扩展图像格式

pygame.image.tostring:将图像传输到字节缓冲区

pygame.image.tobytes:将图像传输到字节缓冲区

pygame.image.fromstring:从字节缓冲区创建新的 Surface

pygame.image.frombytes:从字节缓冲区创建新的 Surface

pygame.image.frombuffer:创建一个新的 Surface,在字节缓冲区内共享数据

pygame.image.load_basic:从文件(或类似文件的对象)加载新的 BMP 图像

pygame.image.load_extended:从文件(或类似文件的对象)加载图像

pygame.image.save_extended:将 png/jpg 图像保存到文件(或类似文件的对象)

16)pygame.joystick管理操纵杆设备

pygame.joystick.init:初始化操纵杆模块。

pygame.joystick.quit:取消初始化操纵杆模块。

pygame.joystick.get_init:如果操纵杆模块已初始化,则返回 True。

pygame.joystick.get_count:返回操纵杆的数量。

pygame.joystick.Joystick:创建一个新的操纵杆对象。

17)pygame.key管理键盘设备

pygame.key.get_focused:如果显示器正在接收来自系统的键盘输入,则为 true

pygame.key.get_pressed:获取所有键盘按钮的状态

pygame.key.get_mods:确定正在按住哪些修饰键

pygame.key.set_mods:临时设置按下哪些修饰键

pygame.key.set_repeat:控制如何重复按住的按键

pygame.key.get_repeat:查看按住的按键如何重复

pygame.key.name:获取密钥标识符的名称

pygame.key.key_code:从密钥名称获取密钥标识符

pygame.key.start_text_input:开始处理 Unicode 文本输入事件

pygame.key.stop_text_input:停止处理 Unicode 文本输入事件

pygame.key.set_text_input_rect:控制候选列表的位置

18)pygame.localsPygame 常量

该模块包含 pygame 使用的各种常量。其内容自动放置在 pygame 模块命名空间中。

19)pygame.mixer 加载并播放声音

pygame.mixer.init:初始化混频器模块

pygame.mixer.pre_init:预设混音器初始化参数

pygame.mixer.quit:取消初始化混音器

pygame.mixer.get_init:测试混音器是否已初始化

pygame.mixer.stop:停止播放所有声道

pygame.mixer.pause:暂时停止所有声道的播放

pygame.mixer.unpause:恢复暂停的声道播放

pygame.mixer.fadeout:停止前逐渐减弱所有声音的音量

pygame.mixer.set_num_channels:设置播放通道总数

pygame.mixer.get_num_channels:获取播放频道总数

pygame.mixer.set_reserved:保留频道不被自动使用

pygame.mixer.find_channel:找到一个未使用的频道

pygame.mixer.get_busy:测试是否有声音被混合

pygame.mixer.get_sdl_mixer_version:获取混音器的 SDL 版本

pygame.mixer.Sound:从文件或缓冲区对象创建新的 Sound 对象

pygame.mixer.Channel:创建一个 Channel 对象来控制播放

20)pygame.mouse管理鼠标设备和显示

pygame.mouse.get_pressed:获取鼠标按钮的状态

pygame.mouse.get_pos:获取鼠标光标位置

pygame.mouse.get_rel:获取鼠标移动量

pygame.mouse.set_pos:设置鼠标光标位置

pygame.mouse.set_visible:隐藏或显示鼠标光标

pygame.mouse.get_visible:获取鼠标光标当前的可见状态

pygame.mouse.get_focused:检查显示器是否正在接收鼠标输入

pygame.mouse.set_cursor:将鼠标光标设置为新光标

pygame.mouse.get_cursor:获取当前鼠标光标

21)pygame.mixer.music播放流媒体音乐曲目

pygame.mixer.music.load:加载音乐文件进行播放

pygame.mixer.music.unload:卸载当前加载的音乐以释放资源

pygame.mixer.music.play:开始播放音乐流

pygame.mixer.music.rewind:重新开始音乐

pygame.mixer.music.stop:停止音乐播放

pygame.mixer.music.pause:暂时停止音乐播放

pygame.mixer.music.unpause:恢复暂停的音乐

pygame.mixer.music.fadeout:淡出后停止音乐播放

pygame.mixer.music.set_volume:设置音乐音量

pygame.mixer.music.get_volume:获取音乐音量

pygame.mixer.music.get_busy:检查音乐流是否正在播放

pygame.mixer.music.set_pos:设置播放位置

pygame.mixer.music.get_pos:获取音乐播放时间

pygame.mixer.music.queue:将声音文件排队以跟随当前的

pygame.mixer.music.set_endevent:让音乐在播放停止时发送事件

pygame.mixer.music.get_endevent:获取播放停止时通道发送的事件

22)pygame.PixelArray 操作图像像素数据

pygame.PixelArray.surface:获取 PixelArray 使用的 Surface。

pygame.PixelArray.itemsize:返回像素数组项的字节大小

pygame.PixelArray.my:返回维数。

pygame.PixelArray.shape:返回数组大小。

pygame.PixelArray.strides:返回每个数组维度的字节偏移量。

pygame.PixelArray.make_surface:从当前 PixelArray 创建一个新 Surface。

pygame.PixelArray.replace:将 PixelArray 中传递的颜色替换为另一种颜色。

pygame.PixelArray.extract:从 PixelArray 中提取传递的颜色。

pygame.PixelArray.compare:将 PixelArray 与另一个 PixelArray 进行比较。

pygame.PixelArray.transpose:交换 x 轴和 y 轴。

pygame.PixelArray.close:关闭 PixelArray,并释放 Surface 锁。

23)pygame.Rect 矩形的灵活容器

pygame.Rect.copy:复制矩形

pygame.Rect.move:移动矩形

pygame.Rect.move_ip:将矩形移动到位

pygame.Rect.inflate:增大或缩小矩形尺寸

pygame.Rect.inflate_ip:就地增大或缩小矩形尺寸

pygame.Rect.scale_by:按给定的乘数缩放矩形

pygame.Rect.scale_by_ip:就地增大或缩小矩形尺寸

pygame.Rect.update:设置矩形的位置和大小

pygame.Rect.clamp:将矩形移动到另一个矩形内

pygame.Rect.clamp_ip:将矩形移动到另一个矩形内

pygame.Rect.clip:在另一个矩形内裁剪一个矩形

pygame.Rect.clipline:在矩形内裁剪一条线

pygame.Rect.union:将两个矩形合并为一个

pygame.Rect.union_ip:将两个矩形就地连接成一个

pygame.Rect.unionall:许多矩形的并集

pygame.Rect.unionall_ip:许多矩形的并集

pygame.Rect.fit:按长宽比调整矩形大小并移动矩形

pygame.Rect.normalize:正确的负尺寸

pygame.Rect.contains:测试一个矩形是否在另一个矩形内部

pygame.Rect.collidepoint:测试一个点是否在矩形内

pygame.Rect.colliderect:测试两个矩形是否重叠

pygame.Rect.collidelist:测试列表中的一个矩形是否相交

pygame.Rect.collidelistall:测试列表中的所有矩形是否相交

pygame.Rect.collideobjects:测试列表中的任何对象是否相交

pygame.Rect.collideobjectsall:测试列表中的所有对象是否相交

pygame.Rect.collidedict:测试字典中的一个矩形是否相交

pygame.Rect.collidedictall:测试字典中的所有矩形是否相交

24)pygame.scrap 本机剪贴板访问

pygame.scrap.init:初始化废料模块。

pygame.scrap.get_init:如果 scrap 模块当前已初始化,则返回 True。

pygame.scrap.get:从剪贴板获取指定类型的数据。

pygame.scrap.get_types:获取可用剪贴板类型的列表。

pygame.scrap.put:将数据放入剪贴板。

pygame.scrap.contains:检查剪贴板中给定类型的数据是否可用。

pygame.scrap.lost:指示 pygame 应用程序是否已丢失剪贴板所有权。

pygame.scrap.set_mode:设置剪贴板访问模式。

25)pygame.sndarray操纵声音样本数据

pygame.sndarray.array:将声音样本复制到数组中

pygame.sndarray.samples:将声音样本引用到数组中

pygame.sndarray.make_sound:将数组转换为 Sound 对象

pygame.sndarray.use_arraytype:设置用于声音阵列的阵列系统

pygame.sndarray.get_arraytype:获取当前活动的数组类型。

pygame.sndarray.get_arraytypes:获取当前支持的阵列系统类型。

26)pygame.tests 测试pygame

pygame.tests.run:运行 pygame 单元测试套件

27)pygame.time管理时间和帧率

pygame.time.get_ticks:获取以毫秒为单位的时间

pygame.time.wait:暂停程序一段时间

pygame.time.delay:暂停程序一段时间

pygame.time.set_timer:在事件队列中重复创建事件

pygame.time.Clock:创建一个对象来帮助跟踪时间

28)pygame.transform调整图像大小并移动图像

pygame.transform.flip:垂直和水平翻转

pygame.transform.scale:调整大小到新分辨率

pygame.transform.scale_by:使用标量调整大小到新分辨率

pygame.transform.rotate:旋转图像

pygame.transform.rotozoom:过滤缩放和旋转

pygame.transform.scale2x:专门的图像倍增器

pygame.transform.smoothscale:平滑地将表面缩放到任意尺寸

pygame.transform.smoothscale_by:使用标量调整大小到新分辨率

pygame.transform.get_smoothscale_backend:返回正在使用的 smoothscale 滤波器版本:"GENERIC"、"MMX"或"SSE"

pygame.transform.set_smoothscale_backend:将 smoothscale 滤波器版本设置为以下之一:"GENERIC"、"MMX"或"SSE"

pygame.transform.chop:获取已删除内部区域的图像的副本

pygame.transform.laplacian:查找曲面中的边缘

pygame.transform.average_surfaces:从多个曲面中找出平均曲面。

pygame.transform.average_color:求表面的平均颜色

pygame.transform.grayscale:对表面进行灰度化

pygame.transform.threshold:查找表面中的哪些像素以及有多少像素在"search_color"或"search_surf"的阈值内。

二、Pygame的安装和使用

1、安装

python 复制代码
pip install pygame

2、应用案例

1)按键(Alt + W/A/S/D)操作小球移动

python 复制代码
# -*- coding:utf-8 -*-
import pygame

# 初始化pygame
pygame.init()

# 设置窗口标题
title = "Pygame应用案例-按键移动的小球"
pygame.display.set_caption(title)

screen = pygame.display.set_mode((1280, 720))
clock = pygame.time.Clock()
running = True
dt = 0

player_pos = pygame.Vector2(screen.get_width() / 2, screen.get_height() / 2)

while running:
    # 事件轮询 - pygame.QUIT 事件表示用户单击X关闭您的窗口
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # 用一种颜色填充屏幕以擦除上一帧中的任何内容
    screen.fill("purple")

    pygame.draw.circle(screen, "red", player_pos, 40)

    keys = pygame.key.get_pressed()
    if keys[pygame.K_w]:
        player_pos.y -= 300 * dt
    if keys[pygame.K_s]:
        player_pos.y += 300 * dt
    if keys[pygame.K_a]:
        player_pos.x -= 300 * dt
    if keys[pygame.K_d]:
        player_pos.x += 300 * dt

    # 更新全部显示 
    pygame.display.flip()

    # 将FPS限制为60
    # dt是自上一帧以来的增量时间(以秒为单位),用于帧速率
    dt = clock.tick(60) / 1000

pygame.quit()

2)规则移动的小球

python 复制代码
# -*- coding:utf-8 -*-
import sys
import pygame

# 初始化pygame
pygame.init()

# 设置窗口标题
title = "Pygame应用案例-规则移动的小球"
pygame.display.set_caption(title)

# 显示窗口
width = 800
height = 600
screen = pygame.display.set_mode((width, height))  

ball = pygame.image.load("ball.png")  # 加载图片
ballrect = ball.get_rect()  # 根据图片大小获取矩形区域

speed = [5, 5]  # 设置移动的X轴、Y轴距离
clock = pygame.time.Clock()  # 创建时钟对象
# 执行死循环,确保窗口一直显示
while True:

    # 表示每秒钟60次帧刷新
    clock.tick(60)

    # 检查事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:  # 如果点击关闭窗口,则退出
            pygame.quit()  # 退出pygame
            sys.exit()

    ballrect = ballrect.move(speed)  # 通过再次赋值移动小球
    # 碰到左右边缘
    if ballrect.left < 0 or ballrect.right > width:
        speed[0] = -speed[0]  #如果碰到边距则反方向移动
    # 碰到上下边缘
    if ballrect.top < 0 or ballrect.bottom > height:
        speed[1] = -speed[1]

    # 填充颜色
    screen.fill("purple")
    # 此函数是将图片推送到窗口上,第一个参数为图片第二个参数为图片位置
    screen.blit(ball, ballrect) 
    # 更新全部显示 
    pygame.display.flip()  
 
pygame.quit()

更多游戏案例可前往官网(https://www.pygame.org/tags/all)下载并查看

相关推荐
技术无疆1 天前
【Python】Uvicorn:Python 异步 ASGI 服务器详解
运维·服务器·开发语言·网络·python·pygame·python3.11
不写八个3 天前
Python编写的贪吃蛇小游戏
开发语言·python·pygame
@技术无疆6 天前
【Python】Flask-Admin:构建强大、灵活的后台管理界面
开发语言·后端·python·oracle·flask·pip·pygame
@技术无疆8 天前
【Python】FeinCMS:轻量级且可扩展的Django内容管理系统
数据库·python·django·sqlite·pip·pygame·httpx
api茶飘香11 天前
如何根据拍立淘API返回值进行商品数据分析
python·数据挖掘·数据分析·django·virtualenv·pygame·tornado
技术无疆14 天前
【Python】Tartiflette:用 Python 实现的 GraphQL 服务器
服务器·开发语言·后端·python·pygame·graphql·python3.11
技术无疆15 天前
【Python】探索Magenta:音乐与艺术的机器智能创作
开发语言·人工智能·python·scikit-learn·pip·pygame·python3.11
技术无疆15 天前
【Python】Anaconda插件:Sublime Text中的Python开发利器
ide·python·编辑器·pip·pygame·sublime text·python3.11
IT小辉同学15 天前
用 Pygame 实现一个乒乓球游戏
python·游戏·pygame
小泽耳机数码推荐家18 天前
百元级蓝牙耳机推荐有哪些?四大优选品牌衷心推荐
python·django·virtualenv·pygame