TableOfContents

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

pygame.image

image模块包含读取和保存图片的函数,也包括把Surfaces变成其它包可以使用的格式的函数。

注意,没有Image类。图片是作为Surface对象读入的。Surface类允许画线、设置像素、捕捉区域等操作。

image模块是pygame必须的一个模块,但是对于扩展部分的文件格式的支持是可选的。默认情况下,它只支持不压缩的BMP图像。当编译成完整的图像格式支持后,pygame.image.load函数可以支持如下类型:

保存图像只支持有限的格式。你可以保存为下列格式:

存为PNG, JPEG格式在pygame 1.8中支持。

1. pygame.image.load

从文件中读取一个图像。

pygame.image.load(filename): return Surface

pygame.image.load(fileobj, namehint=""): return Surface

从文件中读取一个图片。你可以传过去一个文件名,或者一个Python的文件对象。

pygame会自动确定文件的类型(比如GIF或者BMP),并创建一个新的Surface对象。某些情况下,需要知道文件的扩展名(比如GIF图像文件以.gif扩展名命名)。如果传递一个文件对象,你可能要把原来的文件名作为namehint参数传过去。

返回的Surface对象包含和文件中相同的颜色格式、透明色和alpha透明。你常常要调用Surface.convert函数来创建一个拷贝,使得图像在屏幕上画得更快。

对于alpha透明,比如.png图像,在读入后使用convert_alpha()方法来保留每个像素透明信息。

pygame并不是支持所有的图像格式。但是至少它支持不压缩的BMP格式。如果pygame.image.get_extended返回True,则你可以使用大部分的图像格式(包括png、jpg和gif)。

你应该使用os.path.join来保证兼容性。比如asurf = pygame.image.load(os.path.join('data', 'bla.png'))

2. pygame.image.save

把图像保存到文件中

pygame.image.save(Surface, filename): return None

这个函数可以把Surface保存为BMP、TGA、PNG或者JPEG图像文件。如果文件扩展名不认识,默认保存为TGA格式。TGA和BMP格式都是非压缩的文件。

保存为PNG、JPEG格式在pygame1.8种支持。

3. pygame.image.tostring

把图像转换为字符串。

pygame.image.tostring(Surface, format, flipped=False): return string

创建一个字符串,这个字符串可以在其它包中用fromstring方法转换回图像。某些Python图像包希望使用自底向上的图像格式(比如PyOpenGL)。如果你给flipped传送True,则字符串中表示的图像会上下翻转。

format参数可以是以下这些值。注意并不是只有8位的Surface才能使用P格式。其它的格式也能被所有Surface使用。同时也要注意,其它的图像包支持比pygame更多的图像格式。

4. pygame.image.fromstring

从字符串创建一个新的Surface。

pygame.image.fromstring(string, size, format, flipped=False): return Surface

这个函数和pygame.image.tostring有类似的参数。size参数包括一对数字用来指定图像的高度和宽度。一旦新的Surface创建后,你就可以释放你的字符串了。

根据图像的大小和格式计算出来的大小必须和传过来的字符串大小一样。 否则,会导致一个异常。

参看pygame.image.frombuffer方法来更快的创建一个图像的方法。

5. pygame.image.frombuffer

创建一个新的Surface,它的数据空间和一个字符串共享。

pygame.image.frombuffer(string, size, format): return Surface

插件一个新的Surface,它的像素数据直接从字符串中得到。这个方法和pygame.image.fromstring一样,但是不能够上下翻转原始数据。

这个函数比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.