TableOfContents

pygame.display

这个模块提供了对pygame display的控制。Pygame有唯一一个以窗口或者全屏幕显示的display Surface。你一旦创建这个display,你可以把它作为普通的Surface一样使用。改变不会马上在屏幕上显示出来,你必须使用两个翻转(flipping)函数之一来更新实际显示出来的东西。

display的起始位置(即x=0和y=0)在屏幕的左上角。两个坐标轴分别往右和往下增加。

pygame的display可以被初始化成多种模式之一。默认情况下,它被初始化成一个软件驱动的帧缓冲。你可以请求硬件加速和OpenGL的支持。这由传给pygame.display.set_mode的参数来控制。

任何时刻pygame都只能有一个活动的display。使用pygame.display.set_mod创建一个新的display会把老的display关闭。如果需要精确控制像素格式和显示的分辨率,使用pygame.display.mode_ok函数来设置显示模式,pygame.display.list_modes来列出所有可用的模式,pygame.display.Info来创建一个显示信息对象来查询display的相关信息。

一旦display Surface创建了以后,这个模块的函数会对这个唯一的display起作用。如果这个模块uninitialized后,这个Surface就不能再使用。如果设置了一个新的显示模式,现有的Surface会自动转到新的display上操作。

如果设置了显示模式,一些事件会放在事件队列当中。在程序退出的时候,pygame.QUIT事件会发生。当窗口获取或者失去输入焦点的时候,pygame.ACTIVEEVENT事件会发生。如果display设置了pygame.RESIZEABLE标记,当用户改变窗口的大小时,pygame.VIDEORESIZE事件会发生。如果使用硬件display图形是直接画在屏幕上的,当窗口的一部分必须重画时,pygame.VIDEOEXPOSE事件会发生。

1. pygame.display.init

初始化display模块。

pygame.display.init(): return None

初始化pygame的display模块。display模块在初始化之前不能做任何事情。当你调用高层的pygame.init的时候,这个模块也被自动初始化了。

pygame会从一组内部显示后台中选择一个进行初始化。显示模式的选择依赖于平台和用户的权限。在显示模式被初始化之前,可以设置环境变量SDL_VIDEODRIVER来控制使用哪个后台。系统的选择列在这里:

在某些系统上可以把pygame的display嵌入已经存在的窗口中。如果要这样做,必须设置环境变量SDL_WINDOWID来指定窗口的id或者句柄。当pygame的display被初始化的时候会检查这个变量。要注意的是嵌入的display可能会有很多古怪的副作用。

这个函数多次调用不会有什么坏处,实际上多次调用不会起任何作用。

2. pygame.display.quit

uninitialize这个display模块。

pygame.display.quit(): return None

这个函数关闭整个display模块。这意味着任何活动的display会被关闭。这个函数在程序退出的时候会被自动调用。

这个函数多次调用不会有什么坏处,实际上多次调用不会起任何作用。

3. pygame.display.get_init

如果display模块已经初始化,它返回True

pygame.display.get_init(): return bool

如果pygame.display函数当前已经初始化,返回True

4. pygame.display.set_mode

初始化display的窗口或者屏幕

pygame.display.set_mode(resolution, flags=0, depth=0): return Surface

这个函数会创建一个display Surface。参数用来指定请求display的类型。实际创建的display会是系统支持的类型中与请求的尽可能一致的类型。

resolution(分辨率)参数是一对数表示宽度和高度。flags参数是一组选项的集合。depth参数表示颜色的位数。

返回的Surface和普通的Surface一样使用,但是这个Surface上的东西会最终在显示器上看到。

最好不用使用depth参数,默认情况下会自动选择当前系统最好和最快的颜色深度。如果你的游戏需要特定的颜色格式,你可以使用这个参数来控制颜色深度。pygame会模拟系统不能实现的颜色深度,但这样会很慢。

当请求全屏显示模式时,请求的分辨率有时不能够被精确匹配。在这种情况下,pygame会选择最接近的兼容模式设置显示分辨率。而返回的Surface仍然匹配请求的分辨率。

flags参数可以包含你需要的display的类型。有多种选项可以选择,你可以使用位或运算("|")来组合它们。如果传入0或者不指定flags参数,会自动使用默认值。这里是选项的列表:

5. pygame.display.get_surface

返回指向当前display Surface的引用

pygame.display.get_surface(): return Surface

返回指向当前display Surface的引用。如果当前的显示模式还没有设置,那么返回None

6. pygame.display.flip

把整个display Surface上的东西更新到屏幕上

pygame.display.flip(): return None

这个函数会更新整个display的内容。如果显示模式设置成pygame.HWSURFACE和pygame.DOUBLEBUF,这个操作会等待垂直同步并交换surfaces。如果使用其它的显示模式,这个操作只是简单的更新整个Surface的内容。

当使用pygame.OPENGL显示模式时,这个操作会进行一个gl缓冲交换操作。

7. pygame.display.update

更新软件display的部分内容

pygame.display.update(rectangle=None): return None

pygame.display.update(rectangle_list): return None

这个函数好像是为软件display优化的pygame.display.flip。它允许只有屏幕的一部分被更新而不是整个。如果没有指定参数,它和pygame.display.flip一样更新整个窗口。

你可以传给这个函数一个矩形或者一组矩形作为参数。一次传送多个矩形,比多次调用每次一个(或者一部分)矩形更有效率。如果传送一个矩形的列表,则列表中可以含有None,它会被自动跳过。

这个函数不能用在pygame.OPENGL模式上,如果使用会导致一个异常。

8. pygame.display.get_driver

获得pygame显示后台的名字

pygame.display.get_driver(): return name

pygame在初始化时从多个可以选择的显示后台中选择一个。这个函数返回选择的显示后台的名字。这为 display的加速能力提供了一些有限的信息。

9. pygame.display.Info

创建一个视频显示信息对象

创建一个简单的对象,包含图形环境的一些信息。如果在pygame.display.set_mode之前被调用,有些平台上会提供默认的显示模式的信息。在设置了显示模式后调用这个函数,可以验证特定的显示选项是否满足。VideoInfo包含下列对象:

10. pygame.display.get_wm_info

获得当前窗口系统的信息

pygame.display.get_wm_info(): return dict

创建一个字典,关键字为字符串类型。关键字和值是由系统任意创建的。某些系统上可能没有任何信息,会返回一个空的字典。大部分系统上都有一个"window"关键字,它的值是当前display的系统id。

11. pygame.display.list_modes

获得一个可用的全屏模式列表

pygame.display.list_modes(depth=0, flags=pygame.FULLSCREEN): return list

这个函数返回一个指定颜色深度的所有可用的分辨率的列表。如果给定的参数没有显示模式可用,那么会返回一个空的列表。返回-1表示任何分辨率都可以用(这可能是因为指定的是窗口模式)。列表中的模式按照分辨率从大到小排列。

如果depth是0,SDL会自动选择当前或最好的颜色深度。flags参数默认是pygame.FULLSCREEN,你可以添加额外的选项。

12. pygame.display.mode_ok

选择一个最好的颜色深度

pygame.display.mode_ok(size, flags=0, depth=0): return depth

这个函数使用和pygame.display.set_mode相同的参数。它用来确定请求的模式是否可用。指定的模式不能使用它会返回0,否则它会返回一个最佳的颜色深度。

通常depth参数不会被使用。然而某些平台上支持多种颜色深度都同样好,传递这个参数用来指定哪个是更好的。

最有用的flags是pygame.HWSURFACE、pygame.DOUBLEBUF和pygame.FULLSCREEN。如果被请求的flags不能够设置,函数会返回0。

13. pygame.display.gl_get_attribute

获得当前display的opengl标志

pygame.display.gl_get_attribute(flag): return value

如果在调用pygame.display.set_mode时使用了pygame.OPENGL标志,最好检查一下请求的OpenGL属性的值。参看pygame.display.get_set_attribute来得到有效的标志列表。

14. pygame.display.gl_set_attribute

请求设置display的opengl显示属性

pygame.display.gl_set_attribute(flag, value): return None

当调用pygame.display.set_mode时指定了pygame.OPENGL标志,pygame会自动设置OpenGL的属性比如说颜色和双缓冲。你可能需要控制OpenGL提供的其它一些属性。把这些属性作为flag,它的值作为value调用这个函数。这个函数必须在pygame.display.set_mode之前调用。

OPENGL标志有:

15. pygame.display.get_active

当display是活动的时候返回True

pygame.display.get_active(): return bool

在pygame.display.set_mode被调用之后,display Surface会在屏幕上显示出来。大部分窗口的display可以被用户隐藏。如果display Surface被用户隐藏或者最小化,这个函数会返回False。

16. pygame.display.iconify

最小化display Surface

pygame.display.iconify(): return bool

让display surface窗口最小化或者隐藏。不是所有系统都支持最小化的。这个函数在成功时会返回True。

当display被最小化,pygame.display.get_active会返回False。窗口最小化时事件队列会收到一个ACTIVEEVENT事件。

17. pygame.display.toggle_fullscreen

在窗口和全屏之间切换

在窗口和全屏模式之间切换。这个函数只有在unix x11下才能使用。大部分情况下,使用pygame.display.set_mode重新设置flags标志会更好。

18. pygame.display.set_gamma

修改硬件gamma梯度

pygame.display.set_gamma(red, green=None, blue=None): return bool

在显示硬件上设置红绿蓝gamma值。如果green和blue参数没有传,则它们的值和red一样。不是所有的硬件都支持gamma梯度,如果函数调用成功会返回True

gamma值为1.0会创建一个线性的颜色标。低的值会使显示变暗,高的值会使显示变量。

19. pygame.display.set_gamma_ramp

指定一个查询表来修改硬件gamma梯度

pygame.display.set_gamma_ramp(red, green, blue): return bool

指定一个查询表来设置red、green、blue三个gamma梯度值。每个参数都应该是256个整数,每个整数都应该在0和0xffff之间。不是所有的硬件都支持这个功能,如果函数调用成功它会返回True。

20. pygame.display.set_icon

修改显示窗口的图标

pygame.display.set_icon(Surface): return None

设置运行时系统中代表窗口的图标。所有的窗口都默认使用一个简单的pygame图标。

你可以传任何surface给它,但是多数系统只能使用大约32x32的图像。图像可以有一个colorkey指定透明色。

一些系统在图标显示后就不允许改变图标。这个函数可以在pygame.display.set_mode之前调用来创建图标。

21. pygame.display.set_caption

设置当前窗口的标题

pygame.display.set_caption(title, icontitle=None): return None

如果display有一个窗口标题,这个函数可以修改窗口的名字title。一些系统支持用一个可选的更短的名字icontitle用在最小化的窗口上。

22. pygame.display.get_caption

取得当前窗口的标题

pygame.display.get_caption(): return (title, icontitle)

返回display窗口的标题和短标题。这两个值通常是一样的。

23. pygame.display.set_palette

设置颜色调色板

pygame.display.set_palette(palette=None): return None

这个函数可以修改8位display的颜色调色板。这个函数不会修改实际display Surface的调色板,而只是修改用来显示这个Surface的调色板。如果没有指定palette参数,会恢复系统默认的调色板。调色板是RGB三元组的列表。

Surface

pygame里的Surface是用来表示图像的对象。Surface有一定的大小和像素格式。如果是8bit像素格式的Surface,它还会用一个调色板映射到24比特颜色。

调用pygame.Surface可以创建表示图像的新对象。Surface会整个全是黑的。唯一需要指定的参数是大小。如果不指定其他参数,Surface的像素格式会与display Surface的像素格式尽量一致。

像素格式可以通过指定像素深度或者已有的Surface来控制。flag标志位参数是其他一些Surface选项的集合,你可以指定如下的标志

这些参数都仅仅是一个请求,在实际中可能并不能实现。

Advance users can combine a set of bitmasks with a depth value. The masks are a set of 4 integers representing which bits in a pixel will represent each color. Normal Surfaces should not require the masks argument.

Surfaces can have many extra attributes like alpha planes, colorkeys, source rectangle clipping. These functions mainly effect how the Surface is blitted to other Surfaces. The blit routines will attempt to use hardware acceleratio

There are three types of transparency supported in Pygame: colorkeys, surface alphas, and pixel alphas. Surface alphas can be mixed with colorkeys, but an image with per pixel alphas cannot use the other modes. Colorkey transparency makes a single color value transparent. Any pixels matching the colorkey will not be drawn. The surface alpha value is a single value that changes the transparency for the entire image. A surface alpha of 255 is opaque, and a value of 0 is completely transparent.

Per pixel alphas are different because they store a transparency value for every pixel. This allows for the most precise transparency effects, but it also the slowest. Per pixel alphas cannot be mixed with surface alpha and colorkeys.

There is support for pixel access for the Surfaces. Pixel access on hardware surfaces is slow and not recommended. Pixels can be accessed using the get_at() and set_at() functions. These methods are fine for simple access, but will be considerably slow when doing of pixel work with them. If you plan on doing a lot of pixel level work, it is recommended to use the pygame.surfarray module, which can treat the surfaces like large multidimensional arrays (and it's quite quick).

Any functions that directly access a surface's pixel data will need that surface to be lock()'ed. These functions can lock() and unlock() the surfaces themselves without assistance. But, if a function will be called many times, there will be a lot of overhead for multiple locking and unlocking of the surface. It is best to lock the surface manually before making the function call many times, and then unlocking when you are finished. All functions that need a locked surface will say so in their docs. Remember to leave the Surface locked only while necessary.

Surface pixels are stored internally as a single number that has all the colors encoded into it. Use the Surface.map_rgb - convert a color into a mapped color value and Surface.unmap_rgb - convert a mapped integer color value into a Color to convert between individual red, green, and blue values into a packed integer for that Surface.

Surfaces can also reference sections of other Surfaces. These are created with the Surface.subsurface - create a new surface that references its parent method. Any change to either Surface will effect the other.

Each Surface contains a clipping area. By default the clip area covers the entire Surface. If it is changed, all drawing operations will only effect the smaller area.

1. Surface.blit

2. Surface.convert

3. Surface.copy

4. Surface.fill

5. Surface.set_colorkey

6. Surface.get_at

7. Surface.set_at

8. Surface.set_clip

9. Surface.get_clip

10. Surface.get_size

11. Surface.get_width

12. Surface.get_height

image

The image module contains functions for loading and saving pictures, as well as transferring Surfaces to formats usable by other packages.

Note that there is no Image class; an image is loaded as a Surface object. The Surface class allows manipulation (drawing lines, setting pixels, capturing regions, etc.).

The image module is a required dependency of Pygame, but it only optionally supports any extended file formats. By default it can only load uncompressed BMP images. When built with full image support, the pygame.image.load - load new image from a file function can support the following formats.

Saving images only supports a limited set of formats. You can save to the following formats.

PNG, JPEG saving new in pygame 1.8.

1. pygame.image.load

2. pygame.image.save

3. pygame.image.tostring

4. pygame.image.fromstring

draw

Draw several simple shapes to a Surface. These functions will work for rendering to any format of Surface. Rendering to hardware Surfaces will be slower than regular software Surfaces.

Most of the functions take a width argument to represent the size of stroke around the edge of the shape. If a width of 0 is passed the function will actually solid fill the entire shape.

All the drawing functions respect the clip area for the Surface, and will be constrained to that area. The functions return a rectangle representing the bounding area of changed pixels.

Most of the arguments accept a color argument that is an RGB triplet. These can also accept an RGBA quadruplet. The alpha value will be written directly into the Surface if it contains pixel alphas, but the draw function will not draw transparently. The color argument can also be an integer pixel value that is already mapped to the Surface's pixel format.

These functions must temporarily lock the Surface they are operating on. Many sequential drawing calls can be sped up by locking and unlocking the Surface object around the draw calls.

1. pygame.draw.rect

2. pygame.draw.polygon

3. pygame.draw.circle

4. pygame.draw.ellipse

5. pygame.draw.arc

6. pygame.draw.line

7. pygame.draw.lines

8. pygame.draw.aaline

9. pygame.draw.aalines

font

The font module allows for rendering TrueType fonts into a new Surface object. This module is optional and requires SDL_ttf as a dependency. You should test that pygame.font is available and initialized before attempting to use the module.

Most of the work done with fonts are done by using the actual Font objects. The module by itself only has routines to initialize the module and create Font objects with pygame.font.Font - create a new Font object from a file.

You can load fonts from the system by using the pygame.font.SysFont - create a Font object from the system fonts function. There are a few other functions to help lookup the system fonts.

Pygame comes with a builtin default font. This can always be accessed by passing None as the font name.

1. pygame.font.init

2. pygame.font.quit

3. pygame.font.get_init

4. pygame.font.get_default_font

5. pygame.font.get_fonts

6. pygame.font.match_font

7. pygame.font.SysFont

8. pygame.font.Font

8.1. Font.render

8.2. Font.size

8.3. Font.set_underline

8.4. Font.get_underline

8.5. Font.set_bold

8.6. Font.get_bold

8.7. Font.set_italic

8.8. Font.get_italic

8.9. Font.get_linesize

8.10. Font.get_height

8.11. Font.get_ascent

8.12. Font.get_descent

The end

ch3n2k.com | Copyright (c) 2004-2020 czk.