pygame.examples
module of example programs
— play the full aliens example
— run a simple starfield example
— hit the moving chimp
— display animated objects on the screen
— run a font rendering example
— run a FreeType rendering example
— display a vertical gradient
— display pygame events
— show various surfarray effects
— load and play a sound
— play various sndarray effects
— display an animated liquid effect
— display an animated 3D cube using OpenGL
— access the clipboard
— display multiple images bounce off each other using collision detection
— show lots of sprites moving around
— write an image file that is smoothscaled copy of an input file
— demonstrate joystick functionality
— demonstrate the various surface.fill method blend options
— uses alternative additive fill to that of surface.fill
— display two different custom cursors
— display various pixelarray generated effects
— interactively scale an image using smoothscale
— run a midi example
— run a Surface.scroll example that shows a magnified image
— display video captured live from an attached camera
— play an audio file

这些示例应该会帮助您开始使用pyGame。以下是你所得到的简要概述。这些示例的源代码是在公共领域中的。您可以随意将其用于您自己的项目。

有几种方法可以运行这些示例。首先,它们可以作为独立的程序运行。其次,他们可以进口,他们的 main() 调用的方法(见下文)。最后,最简单的方法是使用python-m选项:

python -m pygame.examples.<example name> <example arguments>

例:

python -m pygame.examples.scaletest someimage.png

示例的图像和声音等资源可以在pyGame/Examples/data子目录中找到。

通过在python解释器中使用以下命令,您可以找到示例文件的安装位置。

>>> import pygame.examples.scaletest
>>> pygame.examples.scaletest.__file__
'/usr/lib/python2.6/site-packages/pygame/examples/scaletest.py'

在每个操作系统和每个版本的Python上,位置都会略有不同。例如,在Windows上,它可能在‘C:/Python26/Lib/site-packages/pygame/examples/’中;在Mac OS X上,它可能在‘/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/pygame/examples/’中

您还可以通过调用每个模块的main()函数在python解释器中运行这些示例。

>>> import pygame.examples.scaletest
>>> pygame.examples.scaletest.main()

我们总是在寻找更多的示例和/或示例请求。像这样的代码可能是开始参与Python游戏的最好方式。

包形式的示例是pyGame 1.9.0中的新功能。但大多数例子都是在更早的时候出现的假游戏。

aliens.main()
play the full aliens example
aliens.main() -> None

这开始是作为一个港口 SDL 示范,外星人。现在,它已经演变成一种类似于乐趣的东西。这演示了精灵的许多不同用法和优化的blit。还有透明度、色键、字体、声音、音乐、操纵杆等等。(另外,我的最高分是117分!祝你好运)

stars.main()
run a simple starfield example
stars.main() -> None

一个简单的星空例子。您可以通过在屏幕上左键单击鼠标来更改透视中心。

chimp.main()
hit the moving chimp
chimp.main() -> None

这个简单的例子是从pyGame附带的逐行教程中衍生出来的。它是基于一个“流行的”网络横幅。注意,这里有注释,但要获得完整的解释,请按照教程中的说明进行操作。

moveit.main()
display animated objects on the screen
moveit.main() -> None

这是来自PyGame教程的完整也是最后一个例子,“我如何让它移动”。它创建了10个对象,并在屏幕上为它们设置动画。

注意,它在错误检查方面有一点不足,但它很容易阅读。:]幸运的是,这是PYTHON,我们不需要纠结于一堆错误代码。

fonty.main()
run a font rendering example
fonty.main() -> None

超级快速、超级简单的应用程序演示了使用字体模块呈现字体的不同方法

freetype_misc.main()
run a FreeType rendering example
freetype_misc.main() -> None

呈现的展示展示了 pygame.freetype.FontCreate a new Font instance from a supported font file. 类除了提供的功能外,还提供 pygame.font.Fontcreate a new Font object from a file 。它演示了直接到表面的渲染,包括垂直文本和旋转文本、不透明文本和半透明文本、水平拉伸文本和垂直拉伸文本。

vgrade.main()
display a vertical gradient
vgrade.main() -> None

演示如何使用PixelCopy和NumPy python创建垂直渐变。该应用程序将每隔半秒创建一个新的渐变,并报告创建和显示图像所需的时间。如果您还没有准备好开始使用NumPy数组,请不要担心这个数组的来源:]

eventlist.main()
display pygame events
eventlist.main() -> None

Eventlist是一种草率的pyGame风格,但它是学习pyGame事件和输入的便捷工具。屏幕顶部是几个设备值的状态,底部显示事件的滚动列表。

这根本不是高质量的‘UI’代码,但您可以看到如何实现非常非交互的状态显示,甚至是一个粗糙的文本输出控件。

arraydemo.main()
show various surfarray effects
arraydemo.main(arraytype=None) -> None

另一个充满各种冲浪阵列效果的例子。它需要安装冲浪阵列和图像模块。这个小演示也可以作为您自己使用surfarray进行任何测试的良好起点

这个 arraytype 参数已弃用;传递‘NumPy’以外的任何值都会引发ValueError。

sound.main()
load and play a sound
sound.main(file_path=None) -> None

混音器模块的极其基本的测试。加载并播放声音。全部来自命令外壳,没有图形。

如果提供,请使用音频文件‘FILE_PATH’,否则使用默认文件。

sound.py 可选的命令行参数:音频文件

sound_array_demos.main()
play various sndarray effects
sound_array_demos.main(arraytype=None) -> None

使用Sndarray和NumPy创建原始声音的偏移褪色副本。目前,它只使用回声数量和延迟的硬编码值。您可以根据需要轻松地重新创建。

这个 arraytype 参数已弃用;传递‘NumPy’以外的任何值都会引发ValueError。

liquid.main()
display an animated liquid effect
liquid.main() -> None

此示例是在与闪电战游戏语言进行快速比较后创建的。尽管如此,它演示了快速的8位设置(使用色彩映射表)。

glcube.main()
display an animated 3D cube using OpenGL
glcube.main() -> None

使用PyOpenGL和PYGAME,这将创建一个旋转的3D多色立方体。

scrap_clipboard.main()
access the clipboard
scrap_clipboard.main() -> None

一个简单的剪贴板支持演示示例。

mask.main()
display multiple images bounce off each other using collision detection
mask.main(*args) -> None

立场论据:

one or more image file names.

这 pygame.masks 演示将显示多个相互弹跳的移动精灵。可以提供多个精灵图像。

如果作为程序运行,则 mask.py 将一个或多个图像文件作为命令行参数。

testsprite.main()
show lots of sprites moving around
testsprite.main(update_rects = True, use_static = False, use_FastRenderGroup = False, screen_dims = [640, 480], use_alpha = False, flags = 0) -> None

可选的关键字参数:

update_rects - use the RenderUpdate sprite group class
use_static - include non-moving images
use_FastRenderGroup - Use the FastRenderGroup sprite group
screen_dims - pygame window dimensions
use_alpha - use alpha blending
flags - additional display mode flags

就像 testsprite.c 这是随SDL一起提供的,这个电子游戏版本显示了大量的精灵在四处移动。

如果作为独立程序运行,则不采用命令行参数。

headless_no_windows_needed.main()
write an image file that is smoothscaled copy of an input file
headless_no_windows_needed.main(fin, fout, w, h) -> None

论据:

fin - name of an input image file
fout - name of the output file to create/overwrite
w, h - size of the rescaled image, as integer width and height

如何在没有窗口系统的情况下使用pyGame,比如在无头服务器上。

缩略图生成与缩放是一个例子,你可以用pyGame来做什么。

NOTE :pyGame Scale函数使用MMX/SSE(如果可用),并且可以在多个线程中运行。

如果 headless_no_windows_needed.py 作为程序运行,它接受以下命令行参数:

-scale inputimage outputimage new_width new_height
eg. -scale in.png outpng 50 50
joystick.main()
demonstrate joystick functionality
joystick.main() -> None

显示完全支持操纵杆的演示。

New in pygame 2.0.2.

blend_fill.main()
demonstrate the various surface.fill method blend options
blend_fill.main() -> None

一个交互式演示,允许用户选择要应用于曲面的blend_xxx选项。

blit_blends.main()
uses alternative additive fill to that of surface.fill
blit_blends.main() -> None

假的添加剂混合。使用NumPy。它夹不住了。按r、g、b,有点像Blend_Fill。

cursors.main()
display two different custom cursors
cursors.main() -> None

显示带有交叉杆光标的箭头或圆。

pixelarray.main()
display various pixelarray generated effects
pixelarray.main() -> None

显示各种像素阵列生成的效果。

scaletest.main()
interactively scale an image using smoothscale
scaletest.main(imagefile, convert_alpha=False, run_speed_test=True) -> None

论据:

imagefile - file name of source image (required)
convert_alpha - use convert_alpha() on the surf (default False)
run_speed_test - (default False)

调整屏幕上图像大小的平滑缩放示例。垂直和水平方向键用于更改显示图像的宽度和高度。如果Convert_Alpha选项为True,则强制源图像具有源Alpha,而不管原始图像是否具有源Alpha。如果RUN_SPEED_TEST为True,则执行后台计时测试,而不是执行交互式定标器。

如果 scaletest.py 作为程序运行,则命令行选项为:

ImageFile [-t] [-convert_alpha]
[-t] = Run Speed Test
[-convert_alpha] = Use convert_alpha() on the surf.
midi.main()
run a midi example
midi.main(mode='output', device_id=None) -> None

参数:

mode - if 'output' run a midi keyboard output example
          'input' run a midi event logger input example
          'list' list available midi devices
       (default 'output')
device_id - midi device number; if None then use the default midi input or
            output device for the system

输出示例显示如何将鼠标点击或计算机键盘事件转换为MIDI音符。它实现了一个基本的按钮小部件和状态机。

输入示例显示了如何将MIDI输入转换为PYGAME事件。

通过使用虚拟MIDI接插线,输出和输入示例可以作为单独的进程运行并连接在一起,以便在控制台上显示键盘输出。

PYGAME 1.9.0新手

scroll.main()
run a Surface.scroll example that shows a magnified image
scroll.main(image_file=None) -> None

此示例显示了缩放系数为8的可滚动图像。它使用 Surface.scroll() 函数来移动显示图面上的图像。剪裁矩形保护页边距区域。如果作为函数调用,则该示例接受可选的图像文件路径。如果作为程序运行,它需要一个可选的文件路径命令行参数。如果未提供文件,则使用默认图像文件。

运行时,单击黑色三角形可在三角形指向的方向上移动一个像素。或者使用箭头键。关闭窗口或按 ESC 戒烟。

camera.main()
display video captured live from an attached camera
camera.main() -> None

一个简单的实时视频播放器,它使用它在系统上找到的第一个可用的摄像头。

playmus.main()
play an audio file
playmus.main(file_path) -> None

一个简单的音乐播放器,带有窗口和键盘回放控制。播放可以暂停并倒带到开头。




Edit on GitHub