版本15和24间的区别 (跳过第9版)
于2006-10-09 19:57:55修订的的版本15
大小: 44861
编辑: czk
备注:
于2006-10-11 21:33:17修订的的版本24
大小: 20835
编辑: czk
备注:
删除的内容标记成这样。 加入的内容标记成这样。
行号 3: 行号 3:
= 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事件会发生。


== pygame.display.init ==

初始化display模块。

pygame.display.init(): return None

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

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

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

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

== pygame.display.quit ==

uninitialize这个display模块。

pygame.display.quit(): return None

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

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

== pygame.display.get_init ==

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

pygame.display.get_init(): return bool

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

== 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参数,会自动使用默认值。这里是选项的列表:
 * pygame.FULLSCREEN 创建全屏的display
 * pygame.DOUBLEBUF 使用HWSURFACE或OPENGL时建议加上这个标志
 * pygame.HWSURFACE 使用硬件加速,只在FULLSCREEN时有效
 * pygame.OPENGL 创建一个可以使用opengl的display
 * pygame.RESIZABLE display的窗口可变大小
 * pygame.NOFRAME display的窗口没有边框和控制条

== pygame.display.get_surface ==

返回指向当前display Surface的引用

pygame.display.get_surface(): return Surface

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


== pygame.display.flip ==

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

pygame.display.flip(): return None


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

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

== 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模式上,如果使用会导致一个异常。

== pygame.display.get_driver ==

获得pygame显示后台的名字

pygame.display.get_driver(): return name

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


== pygame.display.Info ==

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

      pygame.display.Info(): return VideoInfo

创建一个简单的对象,包含图形环境的一些信息。如果在pygame.display.set_mode之前被调用,有些平台上会提供默认的显示模式的信息。在设置了显示模式后调用这个函数,可以验证特定的显示选项是否满足。VideoInfo包含下列对象:
 * hw: True if the display is hardware accelerated
 * wm: True if windowed display modes can be used
 * video_mem: The megabytes of video memory on the display. This is 0 if unknown
 * bitsize: Number of bits used to store each pixel
 * bytesize: Number of bytes used to store each pixel
 * masks: Four values used to pack RGBA values into pixels
 * shifts: Four values used to pack RGBA values into pixels
 * losses: Four values used to pack RGBA values into pixels
 * blit_hw: True if hardware Surface blitting is accelerated
 * blit_hw_CC: True if hardware Surface colorkey blitting is accelerated
 * blit_hw_A: True if hardware Surface pixel alpha blitting is accelerated
 * blit_sw: True if software Surface blitting is accelerated
 * blit_sw_CC: True if software Surface colorkey blitting is accelerated
 * blit_sw_A: True if software Surface pixel alpha blitting is acclerated

== pygame.display.get_wm_info ==

获得当前窗口系统的信息

pygame.display.get_wm_info(): return dict

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


== pygame.display.list_modes ==

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

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

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

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

== 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。

== 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来得到有效的标志列表。

== 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标志有:
 * GL_ALPHA_SIZE, GL_DEPTH_SIZE, GL_STENCIL_SIZE, GL_ACCUM_RED_SIZE,
 * GL_ACCUM_GREEN_SIZE, GL_ACCUM_BLUE_SIZE, GL_ACCUM_ALPHA_SIZE,
 * GL_MULTISAMPLEBUFFERS, GL_MULTISAMPLESAMPLES, GL_STEREO

== pygame.display.get_active ==

当display是活动的时候返回True

pygame.display.get_active(): return bool

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

== pygame.display.iconify ==

最小化display Surface

pygame.display.iconify(): return bool

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

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

== pygame.display.toggle_fullscreen ==

在窗口和全屏之间切换

      pygame.display.toggle_fullscreen(): return bool

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

== 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会创建一个线性的颜色标。低的值会使显示变暗,高的值会使显示变量。


== pygame.display.set_gamma_ramp ==

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

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

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

== pygame.display.set_icon ==

修改显示窗口的图标

pygame.display.set_icon(Surface): return None

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

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

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

== pygame.display.set_caption ==

设置当前窗口的标题

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

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


== pygame.display.get_caption ==

取得当前窗口的标题

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

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

== pygame.display.set_palette ==

设置颜色调色板

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

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

行号 281: 行号 5:
表示图像的pygame对象

{{{
pygame.Surface((width, height), flags=0, depth=0, masks=None): return Surface
pygame.Surface((width, height), flags=0, Surface): return Surface
}}}
行号 285: 行号 16:
像素格式可以通过指定像素深度或者已有的Surface来控制。flag标志位参数是其他一些Surface选项的集合,你可以指定如下的标志 像素格式可以通过指定像素深度或者已有的Surface来控制。flags标志位参数是其他一些Surface选项的集合,你可以指定如下的标志
行号 292: 行号 23:
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

 when possible, otherwise will use highly optimized software blitting methods.

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.
高级用户可以组合一组颜色屏蔽位,masks是4个整数的集合,表示像素里的哪个位用来表示一种颜色。一般的Surface不需要masks参数。

Surface可以有一些额外的属性,比如alpha平面、透明色、源矩形剪切。这些功能主要影响一个Surface如何被blit到其它的Surface。如果可能的话,blit函数会尝试使用硬件加速,否则就会使用优化的软件blit方法。

pygame支持三种类型的透明:透明色(colorkey),Surface alpha和每像素alpha。Surface alpha可以和透明色(colorkey)混合使用,但是有像素alpha的图像不能够使用其它模式。透明色(colorkey)让一种颜色值透明。任何和这个颜色相同的像素都不会被画出来。Surface alpha值是单独一个值用来改变整个图像的透明度。Surface alpha值是255表示不透明,值是0表示全透明。

而每像素alpha是不一样的,它为每个像素保存了一个透明值。这种方法允许精确的控制透明效果,但是这种方法也是最慢的。每像素alpha不能和其它方法混用。

Surface支持对单个像素的访问。访问硬件surface的像素是比较慢的,推荐不要这样做。像素可以由get_at()和set_at()函数访问。用这些函数做一些简单的操作是很好的,但是做比较复杂的像素操作就比较慢了。如果你打算做很多像素级的工作,你最好使用pygame.surfarray模块,它可以把surface像多维数组一样使用(而且比较快)。

任何直接访问Surface的像素数据的函数都要求先锁定(lock())。这些函数可以自动调用lock()和unlock()完成加锁和解锁。但是如果一个函数被调用很多次,那么就会有很多多余的加锁和解锁操作。最好是在多次使用这个函数之前,手动锁定Surface,并在结束时手动解锁。所有的需要解锁的函数的文档里面都会这样说。记住,只在需要的时候才锁住Surface。

Surface的数据在内部是表示成一个数字,包含所有的颜色。使用Surface.map_rgb和Surface.unmap_rgb来在独立的红绿蓝值和Surface的内部表示值之间的转换。

一个Surface可以引用另一个Surface的一部分。这通过Surface.subsurface函数创建。改变任何一个Surface都会影响其它的Surface。

每个Surface包含一个剪切区域。默认情况下,剪切区域是整个Surface。如果改变了剪切区域,所有的画图操作都会限制在一个比较小的范围之内。
行号 314: 行号 43:
      draw one image onto another
      Surface.blit(source, dest, area=None, special_flags = 0): return Rect

      Draws a source Surface onto this Surface. The draw can be positioned with the dest argument. Dest can either be pair of coordinates representing the upper left corner of the source. A Rect can also be passed as the destination and the topleft corner of the rectangle will be used as the position for the blit. The size of the destination rectangle does not effect the blit.

      An optional area rectangle can be passed as well. This represents a smaller portion of the source Surface to draw.

      An optional special flags is for passing in BLEND_ADD, BLEND_SUB, BLEND_MULT, BLEND_MIN, BLEND_MAX With other special blitting flags perhaps added in the future.

      The return rectangle is the area of the affected pixels, excluding any pixels outside the destination Surface, or outside the clipping area.

      Pixel alphas will be ignored when blitting to an 8 bit Surface.

      special_flags new in pygame 1.8.
把一个图像画到另一个上面
{{{
Surface.blit(source, dest, area=None, special_flags = 0): return Rect
}}}

把源Surface画到这个Surface上。画的位置可以由dest参数指定。dest可以是一对坐标值,表示源Surface的左上角在这个Surface上的坐标。dest也可以是一个矩形,矩形的左上角作为blit的位置,而矩形的大小不影响blit。

有一个可选的area矩形参数,可以用来指定只画源Surface的一部分。

一个可选的special_flags参数,可以是BLEND_ADD、BLEND_SUB、BLEND_MULT、BLEND_MIN、BLEND_MAX。将来也可能有其它特殊标记添加进来。

返回的矩形表示受影响的像素的区域,不包括目标Surface以外的像素,也不包括剪切区域以外的像素。

如果blit到一个8位的Surface上,像素的alpha值会被忽略。

special_flags是在pygame 1.8中新增的。

== Surface.convert ==
改变图像的像素格式
{{{
Surface.convert(Surface): return Surface
Surface.convert(depth, flags=0): return Surface
Surface.convert(masks, flags=0): return Surface
Surface.convert(): return Surface
}}}
创建一个像素格式不一样的新的Surface。新的像素格式可以由现存其它Surface确定,也可以由depth,flags或者masks决定。这些参数和pygame.Surface的参数类似。

如果没有参数,新的Surface会和display Surface的像素格式一样。这是画图最快的格式。转换所有需要多次blit的Surface是一个好主意。

转换出来的Surface不会有像素alpha。如果原来的Surface里面有,他们会被去掉。参看Surface.convert_alpha来保留或者创建每像素alpha。

== Surface.copy ==
创建一个Surface的拷贝
{{{
Surface.copy(): return Surface
}}}
创建一个Surface的复制品。新创建的Surface和原来的Surface有相同的像素格式、调色板和透明设置。

== Surface.fill ==

用一个颜色填充Surface
{{{
Surface.fill(color, rect=None): return Rect
}}}
用一种颜色填充Surface。如果没有给定rect参数,整个Surface会被填充。rect参数会限制备填充的区域。填充区域同样受到Surface的剪切区域的影响。

颜色参数可以是RGB序列或者是颜色索引值。

这个函数会返回受影响的Surface区域。

== Surface.set_colorkey ==

设置透明色colorkey
{{{
Surface.set_colorkey(Color, flags=0): return None
Surface.set_colorkey(None): return None
}}}
设置Surface的透明色。当把这个Surface blit到令一个Surface时候,和这个透明色颜色相同的像素会变成透明。color参数可以是RGB颜色或者是一个映射的整数。如果传送的是None,则Surface会没有透明色。

如果是有每像素透明的Surface,透明色会被忽略。透明色可以和Surface alpha混合使用。

可选的flags参数可以是pygame.RLEACCEL,用来在没有加速的时候提供更好的性能。设置了RLEACCEL的Surface作为源Surface blit更快,但是修改Surface的内容会更慢。


== Surface.get_at ==

获得特定像素点的颜色值
{{{
Surface.get_at((x, y)): return Color
}}}

返回指定像素的RGBA颜色值。如果Surface没有每像素alpha,alpha值总是255(不透明)。如果像素位置在Surface的区域以外,会引发一个IndexError异常。

在游戏和实时模拟中,一次取得和设置一个像素是很慢的。

这个函数会根据需要临时加锁和解锁Surface。

== Surface.set_at ==

设置特定像素点的颜色值
{{{
Surface.set_at((x, y), Color): return None
}}}

为一个像素设置RGBA或者映射的整数颜色值。如果Surface没有每像素alpha,alpha值会被忽略。对Surface区域之外或者剪切区域之外的像素设置颜色不会有任何效果。

在游戏和实时模拟中,一次取得和设置一个像素是很慢的。

这个函数会根据需要临时加锁和解锁Surface。

== Surface.set_clip ==
设置Surface的剪切区域
{{{
Surface.set_clip(rect): return None
Surface.set_clip(None): return None
}}}
每个Surface都有一个剪切区域。这个区域是个矩形,表示只有在这个矩形内的像素可以被修改。如果指定None作为矩形区域,则整个Surface都可以修改。

剪切区域总是在Surface本身的区域只内的。如果剪切区域比Surface的区域大,则会自动缩小到Surface区域之内。
行号 330: 行号 144:
== Surface.convert ==

      change the pixel format of an image
      Surface.convert(Surface): return Surface
      Surface.convert(depth, flags=0): return Surface
      Surface.convert(masks, flags=0): return Surface
      Surface.convert(): return Surface

      Creates a new copy of the Surface with the pixel format changed. The new pixel format can be determined from another existing Surface. Otherwise depth, flags, and masks arguments can be used, similar to the pygame.Surface - pygame object for representing images call.

      If no arguments are passed the new Surface will have the same pixel format as the display Surface. This is always the fastest format for blitting. It is a good idea to convert all Surfaces before they are blitted many times.

      The converted Surface will have no pixel alphas. They will be stripped if the original had them. See Surface.convert_alpha - change the pixel format of an image including per pixel alphas for preserving or creating per-pixel alphas.

== Surface.copy ==

      create a new copy of a Surface
      Surface.copy(): return Surface

      Makes a duplicate copy of a Surface. The new Surface will have the same pixel formats, color palettes, and transparency settings as the original.

== Surface.fill ==

      fill Surface with a solid color
      Surface.fill(color, rect=None): return Rect

      Fill the Surface with a solid color. If no rect argument is given the entire Surface will be filled. The rect argument will limit the fill to a specific area. The fill will also be contained by the Surface clip area.

      The color argument can be either an RGB sequence or a mapped color index.

      This will return the affected Surface area.

== Surface.set_colorkey ==

      Set the transparent colorkey
      Surface.set_colorkey(Color, flags=0): return None
      Surface.set_colorkey(None): return None

      Set the current color key for the Surface. When blitting this Surface onto a destination, and pixels that have the same color as the colorkey will be transparent. The color can be an RGB color or a mapped color integer. If None is passed, the colorkey will be unset.

      The colorkey will be ignored if the Surface is formatted to use per pixel alpha values. The colorkey can be mixed with the full Surface alpha value.

      The optional flags argument can be set to pygame.RLEACCEL to provide better performance on non accelerated displays. An RLEACCEL Surface will be slower to modify, but quicker to blit as a source.


== Surface.get_at ==

      get the color value at a single pixel
      Surface.get_at((x, y)): return Color

      Return the RGBA color value at the given pixel. If the Surface has no per pixel alpha, then the alpha value will always be 255 (opaque). If the pixel position is outside the area of the Surface an IndexError exception will be raised.

      Getting and setting pixels one at a time is generally too slow to be used in a game or realtime situation.

      This function will temporarily lock and unlock the Surface as needed.
       

== Surface.set_at ==

      set the color value for a single pixel
      Surface.set_at((x, y), Color): return None

      Set the RGBA or mapped integer color value for a single pixel. If the Surface does not have per pixel alphas, the alpha value is ignored. Settting pixels outside the Surface area or outside the Surface clipping will have no effect.

      Getting and setting pixels one at a time is generally too slow to be used in a game or realtime situation.

      This function will temporarily lock and unlock the Surface as needed.

== Surface.set_clip ==

      set the current clipping area of the Surface
      Surface.set_clip(rect): return None
      Surface.set_clip(None): return None

      Each Surface has an active clipping area. This is a rectangle that represents the only pixels on the Surface that can be modified. If None is passed for the rectangle the full Surface will be available for changes.

      The clipping area is always restricted to the area of the Surface itself. If the clip rectangle is too large it will be shrunk to fit inside the Surface.
       
行号 410: 行号 145:

      get the current clipping are of the Surface
      Surface.get_clip(): return Rect

      Return a rectangle of the current clipping area. The Surface will always return a valid rectangle that will never be outside the bounds of the image. If the Surface has had None set for the clipping area, the Surface will return a rectangle with the full area of the Surface.
获得Surface的当前剪切区域
{{{
Surface.get_clip(): return Rect
}}}
返回Surface的当前剪切区域。一个Surface总是返回一个有效的矩形,不会超过图像的边界范围。如果Surface设置了None作为剪切区域,则Surface会返回整个Surface的区域。
行号 419: 行号 153:
      get the dimensions of the Surface
      Surface.get_size(): return (width, height)

      Return the width and height of the Surface in pixels.
获得Surface的大小
{{{
Surface.get_size(): return (width, height)
}}}
返回Surface的像素高度和宽度
行号 427: 行号 161:
      get the width of the Surface
      Surface.get_width(): return width

      Return the width of the Surface in pixels.
       
获得Surface的宽度
{{{
Surface.get_width(): return width
}}}
返回Surface的像素宽度
行号 435: 行号 169:
      get the height of the Surface
      Surface.get_height(): return height

      Return the height of the Surface in pixels.
= 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.

    * JPG
    * PNG
    * GIF (non animated)
    * BMP
    * PCX
    * TGA (uncompressed)
    * TIF
    * LBM (and PBM)
    * PBM (and PGM, PPM)
    * XPM

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

    * BMP
    * TGA
    * PNG
    * JPEG

PNG, JPEG saving new in pygame 1.8.

== pygame.image.load ==

      load new image from a file
      pygame.image.load(filename): return Surface
      pygame.image.load(fileobj, namehint=""): return Surface

      Load an image from a file source. You can pass either a filename or a Python file-like object.

      Pygame will automatically determine the image type (e.g., GIF or bitmap) and create a new Surface object from the data. In some cases it will need to know the file extension (e.g., GIF images should end in ".gif"). If you pass a raw file-like object, you may also want to pass the original filename as the namehint argument.

      The returned Surface will contain the same color format, colorkey and alpha transparency as the file it came from. You will often want to call Surface.convert - change the pixel format of an image with no arguments, to create a copy that will draw more quickly on the screen.

      For alpha transparency, like in .png images use the convert_alpha() method after loading so that the image has per pixel transparency.

      Pygame may not always be built to support all image formats. At minimum it will support uncompressed BMP. If pygame.image.get_extended - test if extended image formats can be loaded returns 'True', you should be able to load most images( including png, jpg and gif ).

      You should use os.path.join() for compatibility.

        eg. asurf = pygame.image.load(os.path.join('data', 'bla.png'))


== pygame.image.save ==

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

      This will save your Surface as either a BMP, TGA, PNG, or JPEG image. If the filename extension is unrecognized it will default to TGA. Both TGA, and BMP file formats create uncompressed files.

      PNG, JPEG saving new in pygame 1.8.


== pygame.image.tostring ==

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

      Creates a string that can be transferred with the 'fromstring' method in other Python imaging packages. Some Python image packages prefer their images in bottom-to-top format (PyOpenGL for example). If you pass True for the flipped argument, the string buffer will be vertically flipped.

      The format argument is a string of one of the following values. Note that only 8bit Surfaces can use the "P" format. The other formats will work for any Surface. Also note that other Python image packages support more formats than Pygame.
          o P, 8bit palettized Surfaces
          o RGB, 24bit image
          o RGBX, 32bit image with unused space
          o RGBA, 32bit image with an alpha channel
          o ARGB, 32bit image with alpha channel first
       

== pygame.image.fromstring ==

      create new Surface from a string buffer
      pygame.image.fromstring(string, size, format, flipped=False): return Surface

      This function takes arguments similar to pygame.image.tostring - transfer image to string buffer. The size argument is a pair of numbers representing the width and height. Once the new Surface is created you can destroy the string buffer.

      The size and format image must compute the exact same size as the passed string buffer. Otherwise an exception will be raised.

      See the pygame.image.frombuffer - create a new Surface that shares data inside a string buffer method for a potentially faster way to transfer images into Pygame.

= 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.
获得Surface的高度
{{{
Surface.get_height(): return height
}}}
返回Surface的像素高度



= pygame.draw =

在Surface上画一些简单的图形。这些函数可以在任何格式的Surface上使用。在硬件Surface上使用会比普通的软件Surface上慢。

大部分的函数都包括一个width参数来表示画笔的大小(即形状的边缘的宽度)。如果width是0则整个图形会被颜色填充。

所有的函数都有Surface剪切区域,它的操作会被限制在这个区域里面。这些函数返回一个矩形区域表示被改动的像素的范围。

大部分表示颜色的参数是一个RGB三元组,也可以使用RGBA四元组。如果Surface包含alpha,四元组中alpha值会被直接写入到Surface里面,画图函数并不会进行透明的绘画。颜色参数也可以是一个整数,可以映射到Surface的像素格式。

这些函数在操作时必须临时锁定Surface。如果有很多一系列的绘图操作,可以使用Surface的锁定和解锁操作来加速。
行号 537: 行号 192:
      draw a rectangle shape
      pygame.draw.rect(Surface, color, Rect, width=0): return Rect

      Draws a rectangular shape on the Surface. The given Rect is the area of the rectangle. The width argument is the thickness to draw the outer edge. If width is zero then the rectangle will be filled.

      Keep in mind the Surface.fill - fill Surface with a solid color method works just as well for drawing filled rectangles. In fact the Surface.fill - fill Surface with a solid color can be hardware accelerated on some platforms with both software and hardware display modes.
       
画一个矩形

pygame.draw.rect(Surface, color, Rect, width=0): return Rect

在Surface上画一个矩形。指定的Rect是矩形的区域。width参数是矩形框的粗细。如果width是0,整个矩形会被填充。

要记住的是,Surface.fill也可以画一个填充的矩形。实际上,Surface.fill在某些平台上可以用硬件加速。
      
行号 547: 行号 203:
      draw a shape with any number of sides
      pygame.draw.polygon(Surface, color, pointlist, width=0): return Rect

      Draws a polygonal shape on the Surface. The pointlist argument is the vertices of the polygon. The width argument is the thickness to draw the outer edge. If width is zero then the polygon will be filled.

      For aapolygon, use aalines with the 'closed' parameter.
       
画一个有任意条边的图形

pygame.draw.polygon(Surface, color, pointlist, width=0): return Rect

在Surface上画一个多边形。pointlist参数是多边形顶点的列表。width参数是多边形的边的粗细。如果width是0,多边形是被填充的。

对于aapolygon,使用带closed参数的aalines。
行号 557: 行号 214:
      draw a circle around a point
      pygame.draw.circle(Surface, color, pos, radius, width=0): return Rect

      Draws a circular shape on the Surface. The pos argument is the center of the circle, and radius is the size. The width argument is the thickness to draw the outer edge. If width is zero then the circle will be filled.
       
围绕一个点画一个圆

pygame.draw.circle(Surface, color, pos, radius, width=0): return Rect

在Surface上画一个圆形。pos参数是圆的圆心,radius是半径大小。width参数是圆边的粗细,如果width是0圆会被填充。
行号 565: 行号 222:
      draw a round shape inside a rectangle
      pygame.draw.ellipse(Surface, color, Rect, width=0): return Rect

      Draws an elliptical shape on the Surface. The given rectangle is the area that the circle will fill. The width argument is the thickness to draw the outer edge. If width is zero then the ellipse will be filled.
       
在矩形区域中画一个椭圆的形状

pygame.draw.ellipse(Surface, color, Rect, width=0): return Rect

在Surface上画一个椭圆。给定的矩形区域是椭圆填充的区域。width是边的粗细。如果width是0,椭圆会被填充。
行号 573: 行号 230:
      draw a partial section of an ellipse
      pygame.draw.arc(Surface, color, Rect, start_angle, stop_angle, width=1): return Rect

      Draws an elliptical arc on the Surface. The rect argument is the area that the ellipse will fill. The two angle arguments are the initial and final angle in radians, with the zero on the right. The width argument is the thickness to draw the outer edge.
       
画椭圆的一部分

pygame.draw.arc(Surface, color, Rect, start_angle, stop_angle, width=1): return Rect

在Surface上画一个椭圆状的弧线。rect参数指定椭圆填充的矩形。两个角度参数指定起始和终止的角度(以弧度为单位),朝右为0度。width参数是弧线的粗细。
行号 581: 行号 238:
      draw a straight line segment
      pygame.draw.line(Surface, color, start_pos, end_pos, width=1): return Rect

      Draw a straight line segment on a Surface. There are no endcaps, the ends are squared off for thick lines.
       
画一条直线段

pygame.draw.line(Surface, color, start_pos, end_pos, width=1): return Rect

在Surface上画一条直线段。线段终点没有箭头,宽的线段终点是方形的。
行号 589: 行号 246:
      draw multiple contiguous line segments
      pygame.draw.lines(Surface, color, closed, pointlist, width=1): return Rect

      Draw a sequence of lines on a Surface. The pointlist argument is a series of points that are connected by a line. If the closed argument is true an additional line segment is drawn between the first and last points.

      This does not draw any endcaps or miter joints. Lines with sharp corners and wide line widths can have improper looking corners.
       
画多条连续的线段

pygame.draw.lines(Surface, color, closed, pointlist, width=1): return Rect

在Surface上画一系列的直线。pointlist是一系列点的列表。如果closed参数是True,则在最后一点和第一点之间会画一条线段。

这个函数不会画终点箭头和中间连接头。线段有尖锐的拐角,粗的线段会有看上去不正确的拐角。
行号 599: 行号 256:
      draw fine antialiased lines
      pygame.draw.aaline(Surface, color, startpos, endpos, blend=1): return Rect

      Draws an anti-aliased line on a surface. This will respect the clipping rectangle. A bounding box of the affected area is returned returned as a rectangle. If blend is true, the shades will be be blended with existing pixel shades instead of overwriting them. This function accepts floating point values for the end points.
画抗锯齿的线段

pygame.draw.aaline(Surface, color, startpos, endpos, blend=1): return Rect

在Surface上画一条抗锯齿的直线段。如果blend是True,则阴影部分是和原始像素混合而不是直接覆盖的。这个函数接受浮点数作为点的坐标。
行号 606: 行号 264:
      pygame.draw.aalines(Surface, color, closed, pointlist, blend=1): return Rect

      Draws a sequence on a surface. You must pass at least two points in the sequence of points. The closed argument is a simple boolean and if true, a line will be draw between the first and last points. The boolean blend argument set to true will blend the shades with existing shades instead of overwriting them. This function accepts floating point values for the end points.

= 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.
pygame.draw.aalines(Surface, color, closed, pointlist, blend=1): return Rect

画多条连续的线段,每个线段都是抗锯齿的。pointlist里面至少要两个点。closed参数如果是True,则在第一个点和最后一点之间会画一条直线。如果blend参数是True,则阴影部分是和原始像素混合而不是直接覆盖的。这个函数接受浮点数作为点的坐标。

= pygame.font =

font模块允许允许在新创建的Surface上画TrueType字体。这个模块是可选的,并依赖于SDL_ttf。你应该测试pygame.font是否可用,并在尝试使用前初始化它。

字体的大部分工作使用Font对象来实现。这个模块它自己仅有初始化函数和pygame.font.Font函数用来创建Font对象。

你可以使用pygame.font.SysFont从系统载入字体。有一些其它的函数用来帮助查找系统字体。

pygame自带一个默认的字体。这个字体总是可以用None作为字体名字被使用。
行号 621: 行号 280:
      initialize the font module
      pygame.font.init(): return None

      This method is called automatically by pygame.init - initialize all imported pygame modules. It initializes the font module. The module must be initialized before any other functions will work.

      It is safe to call this function more than once.
初始化font模块

pygame.font.init(): return None

这个方法会在pygame.init时自动调用。它初始化font模块。在使用这个模块的其他函数之前必须先初始化。

多次调用这个函数也不会有副作用。
行号 631: 行号 290:
      uninitialize the font module
      pygame.font.quit(): return None

      Manually uninitialize SDL_ttf's font system. This is called automatically by pygame.quit - uninitialize all pygame modules.

      It is safe to call this function even if font is currently not initialized.
uninitialize font模块

pygame.font.quit(): return None

手动uninitialize SDL_ttf的字体系统。这个函数会被pygame.quit自动调用。

如果font模块没有被初始化,调用这个函数也不会有问题。
行号 640: 行号 300:
      true if the font module is initialized
      pygame.font.get_init(): return bool

      Test if the font module is initialized or not.
返回True如果font模块被初始化了

pygame.font.get_init(): return bool

测试font模块是否被初始化了。
行号 647: 行号 308:
      get the filename of the default font
      pygame.font.get_default_font(): return string

      Return the filename of the system font. This is not the full path to the file. This file can usually be found in the same directory as the font module, but it can also be bundled in separate archives.
得到默认字体的文件名

pygame.font.get_default_font(): return string

返回系统字体的文件名。这个不是文件的完整路径。这个文件通常可以在和font模块相同的目录中找到,但是它也可能在其它文档中。
行号 655: 行号 316:
      get all available fonts
      pygame.font.get_fonts(): return list of strings

      Returns a list of all the fonts available on the system. The names of the fonts will be set to lowercase with all spaces and punctuation removed. This works on most systems, but some will return an empty list if they cannot find fonts.
获得所有可用的字体

pygame.font.get_fonts(): return list of strings

返回一个系统所有可用字体的列表。字体的名字会用小写,所有的空格和其它字符都被去掉了。这在大部分系统都可以用,但是某些系统找不到字体会返回一个空的列表。
行号 663: 行号 324:
      find a specific font on the system
      
pygame.font.match_font(name, bold=False, italic=False): return path

      Returns the full path to a font file on the system. If bold or italic are set to true, this will attempt to find the correct family of font.

      
The font name can actually be a comma separated list of font names to try. If none of the given names are found, None is returned.

      Example:

          
print pygame.font.match_font('bitstreamverasans')
          # output is: /usr/share/fonts/truetype/ttf-bitstream-vera/Vera.ttf
          # (but only if you have Vera on your system)
在系统中查找一个特定的字体

pygame.font.match_font(name, bold=False, italic=False): return path

返回一个系统中字体文件的完整路径。如果bold或者italic参数是True,函数会尝试去查找正确的字体族。

字体的名字可以是逗号隔开的字体的列表。如果没有一个给定字体找到,函数返回
None

比如:{{{
print pygame.font.match_font('bitstreamverasans')
# output is: /usr/share/fonts/truetype/ttf-bitstream-vera/Vera.ttf
# (but only if you have Vera on your system)
}}}
行号 679: 行号 340:
      create a Font object from the system fonts
      pygame.font.SysFont(name, size, bold=False, italic=False): return Font

      Return a new Font object that is loaded from the system fonts. The font will match the requested bold and italic flags. If a suitable system font is not found this will fallback on loading the default pygame font. The font name can be a comma separated list of font names to look for.
从系统字体创建一个Font对象

pygame.font.SysFont(name, size, bold=False, italic=False): return Font

返回一个新建的从系统字体装载的Font对象。字体会对应请求的bold和italic标志。如果不能找到合适的系统字体,这个函数会载入缺省的pygame字体。字体名字可以是逗号隔开的字体名字的列表。
行号 686: 行号 348:
      create a new Font object from a file
      pygame.font.Font(filename, size): return Font
      pygame.font.Font(object, size): return Font
            Font.render - draw text on a new Surface draw text on a new Surface
            Font.size - determine the amount of space needed to render text determine the amount of space needed to render text
            Font.set_underline - control if text is rendered with an underline control if text is rendered with an underline
            Font.get_underline - check if text will be rendered with an underline check if text will be rendered with an underline
            Font.set_bold - enable fake rendering of bold text enable fake rendering of bold text
            Font.get_bold - check if text will be rendered bold check if text will be rendered bold
            Font.set_italic - enable fake rendering of italic text enable fake rendering of italic text
            Font.get_italic - check if the text will be rendered italic check if the text will be rendered italic
            Font.get_linesize - get the line space of the font text get the line space of the font text
            Font.get_height - get the height of the font get the height of the font
            Font.get_ascent - get the ascent of the font get the ascent of the font
            Font.get_descent - get the descent of the font get the descent of the font

      Load a new font from a given filename or a python file object. The size is the height of the font in pixels. If the filename is None the Pygame default font will be loaded. If a font cannot be loaded from the arguments given an exception will be raised. Once the font is created the size cannot be changed.

      Font objects are mainly used to render text into new Surface objects. The render can emulate bold or italic features, but it is better to load from a font with actual italic or bold glyphs. The rendered text can be regular strings or unicode.
从文件创建一个Font对象

pygame.font.Font(filename, size): return Font

pygame.font.Font(object, size): return Font

从给定的文件名或者python文件对象载入新的字体。size是字体的高度。如果文件名是None,pygame的默认字体会被载入。如果参数指定的字体不能载入,会引发一个异常。一旦字体创建后,字体的大小不能被改变。

Font对象主要用来把文字画成新的Surface对象。画的过程可以模拟黑体和斜体的效果,但是最好使用真正的斜体和黑体字体。被画文字可以使普通字符串,也可以是unicode串。
行号 708: 行号 360:
      draw text on a new Surface
      Font.render(text, antialias, color, background=None): return Surface

      This creates a new Surface with the specified text rendered on it. Pygame provides no way to directly draw text on an existing Surface: instead you must use Font.render - draw text on a new Surface to create an image (Surface) of the text, then blit this image onto another Surface.

      The text can only be a single line: newline characters are not rendered. The antialias argument is a boolean: if true the characters will have smooth edges. The color argument is the color of the text [e.g.: (0,0,255) for blue]. The optional background argument is a color to use for the text background. If no background is passed the area outside the text will be transparent.

      The Surface returned will be of the dimensions required to hold the text. (the same as those returned by Font.size()). If an empty string is passed for the text, a blank surface will be returned that is one pixel wide and the height of the font.

      Depending on the type of background and antialiasing used, this returns different types of Surfaces. For performance reasons, it is good to know what type of image will be used. If antialiasing is not used, the return image will always be an 8bit image with a two color palette. If the background is transparent a colorkey will be set. Antialiased images are rendered to 24-bit RGB images. If the background is transparent a pixel alpha will be included.

      Optimization: if you know that the final destination for the text (on the screen) will always have a solid background, and the text is antialiased, you can improve performance by specifying the background color. This will cause the resulting image to maintain transparency information by colorkey rather than (much less efficient) alpha values.

      If you render '\n' a unknown char will be rendered. Usually a rectangle. Instead you need to handle new lines yourself.

      Font rendering is not thread safe: only a single thread can render text any time.
在新的Surface上画出文字。

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

这个函数创建一个新Surface对象,它上面包含了画出来的文字。pygame没有提供直接在存在的Surface上画文字的方法,而必须采用Font.render来创建一个新的Surface,然后把它blit到其他Surface上。

文字只能包含一行,换行符不会被画出来。antialias抗锯齿参数如果是真的,字符会有光滑的边缘。color参数指定了文字的颜色(比如(0,0,255)为蓝色)。可选的background参数用来指定文字背景的颜色。如果没有指定background,背景是透明的。

返回的Surface的大小是包含文字的必须的大小。(和Font.size()一样。)如果text是空字符串,会返回一个空白的Surface,只有一个像素宽和一个字符的高度。

返回的Surface的类型依赖于background和antialias的类型。因为性能的原因,最好要知道图像是哪种类型的。如果没有使用antialias,返回的图像是带两个调色板的8位的图像。如果background是透明的,会设置一个透明色colorkey。抗锯齿的图像会使用24位RGB图像。如果背景是透明的,会使用像素alpha。

优化:如果你知道最后文字的背景是单一的颜色,并且文字是抗锯齿的,那么你可以通过指定背景色来改进性能。这使得图像通过透明色colorkey而不是alpha(这个要慢得多)来指定透明信息。

如果你画'\n'字符,一个未知的字符会被画出来,通常是一个矩形。你应该自己去处理多行字符串。

画字体不是线程安全的:一个时刻只能有一个线程在画文字。
行号 728: 行号 380:
      determine the amount of space needed to render text
      Font.size(text): return (width, height)

      Returns the dimensions needed to render the text. This can be used to help determine the positioning needed for text before it is rendered. It can also be used for wordwrapping and other layout effects.

      Be aware that most fonts use kerning which adjusts the widths for specific letter pairs. For example, the width for "ae" will not always match the width for "a" + "e".
确定画文字需要的大小

Font.size(text): return (width, height)

返回画文字需要的大小。这个函数可以帮助确定文字的位置,也可以用来确定文字换行和其他布局效果。

注意,大部分字体都会对特定的字符使用字据调整。比如说字符串“ae”的宽度不总是和"a"+"e"的宽度一致。
行号 738: 行号 390:
      control if text is rendered with an underline
      Font.set_underline(bool): return None

      When enabled, all rendered fonts will include an underline. The underline is always one pixel thick, regardless of font size. This can be mixed with the bold and italic modes.
控制文字是否有下划线

Font.set_underline(bool): return None

当开启这个选项, 所有的文字都会有下划线。不管字体多大,下划线总是只有一个像素的宽度。这个选项可以和粗体、斜体混合使用。

=== Font.get_underline ===

检查画出来的文字是否有下划线。

Font.get_underline(): return bool

当字体设置有下划线,这个函数返回真。
行号 744: 行号 405:
=== Font.get_underline ===

      check if text will be rendered with an underline
      Font.get_underline(): return bool

      Return True when the font underline is enabled.
       
行号 754: 行号 407:
      enable fake rendering of bold text
      Font.set_bold(bool): return None

      Enables the bold rendering of text. This is a fake stretching of the font that doesn't look good on many font types. If possible load the font from a real bold font file. While bold, the font will have a different width than when normal. This can be mixed with the italic and underline modes.
       
开启文字的伪粗体

Font.set_bold(bool): return None

开启伪粗体。这是对字体进行伸展而得到的伪粗体,用在很多字体上不好看。如果可能的话,应该载入一个真正的粗体。当这个选项开启后,字符会有不同的宽度。这个选项可以和斜体和下划线混合使用。
行号 762: 行号 415:
      check if text will be rendered bold
      Font.get_bold(): return bool

      Return True when the font bold rendering mode is enabled.
       
检查字体设置是否用粗体。

Font.get_bold(): return bool

如果字体设置了粗体,返回真。
行号 770: 行号 423:
      enable fake rendering of italic text
      Font.set_bold(bool): return None

      Enables fake rendering of italic text. This is a fake skewing of the font that doesn't look good on many font types. If possible load the font from a real italic font file. While italic the font will have a different width than when normal. This can be mixed with the bold and underline modes.
       
设置字体的伪斜体

Font.set_bold(bool): return None

开启字体的伪斜体。这是对字体进行倾斜而得到的伪斜体,用在很多字体上不好看。如果可能的话,应该载入真正的斜体。当开启了斜体后,字符的宽度可能会不同。这个选项可以和粗体、下划线混合使用。
行号 778: 行号 431:
      check if the text will be rendered italic
      Font.get_italic(): return bool

      Return True when the font italic rendering mode is enabled.
       
检查字体设置是否为斜体

Font.get_italic(): return bool

如果字体设置了斜体,则返回真。
行号 786: 行号 440:
      get the line space of the font text
      Font.get_linesize(): return int

      Return the height in pixels for a line of text with the font. When rendering multiple lines of text this is the recommended amount of space between lines.
       
获得行的高度。

Font.get_linesize(): return int

返回一行文字的像素高度。当画很多行字符的时候,这个高度是两行间的间隔的推荐值。
行号 794: 行号 448:
      get the height of the font 获取字体的高度。
行号 797: 行号 452:
      Return the height in pixels of the actual rendered text. This is the average size for each glyph in the font.
       
返回文字的实际像素高度。这是这种这种字体里的字符的平均高度。
行号 802: 行号 456:
      get the ascent of the font
      Font.get_ascent(): return int

      Return the height in pixels for the font ascent. The ascent is the number of pixels from the font baseline to the top of the font.
       
获得字体的ascent高度。

Font.get_ascent(): return int

返回字体的ascent像素高度。ascent是从字体的基线到字体顶部的像素高度。
行号 810: 行号 464:
      get the descent of the font
      Font.get_descent(): return int

      Return the height in pixels for the font descent. The descent is the number of pixels from the font baseline to the bottom of the font.
获得字体的descent高度

Font.get_descent(): return int

返回字体的descent高度。descent是指从字体基线到字体底部的像素高度。

TableOfContents

Surface

表示图像的pygame对象

pygame.Surface((width, height), flags=0, depth=0, masks=None): return Surface
pygame.Surface((width, height), flags=0, Surface): return Surface

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

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

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

  • HWSURFACE, 在视频内存中创建图像
  • SRCALPHA, 像素格式中会包含一个alpha通道

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

高级用户可以组合一组颜色屏蔽位,masks是4个整数的集合,表示像素里的哪个位用来表示一种颜色。一般的Surface不需要masks参数。

Surface可以有一些额外的属性,比如alpha平面、透明色、源矩形剪切。这些功能主要影响一个Surface如何被blit到其它的Surface。如果可能的话,blit函数会尝试使用硬件加速,否则就会使用优化的软件blit方法。

pygame支持三种类型的透明:透明色(colorkey),Surface alpha和每像素alpha。Surface alpha可以和透明色(colorkey)混合使用,但是有像素alpha的图像不能够使用其它模式。透明色(colorkey)让一种颜色值透明。任何和这个颜色相同的像素都不会被画出来。Surface alpha值是单独一个值用来改变整个图像的透明度。Surface alpha值是255表示不透明,值是0表示全透明。

而每像素alpha是不一样的,它为每个像素保存了一个透明值。这种方法允许精确的控制透明效果,但是这种方法也是最慢的。每像素alpha不能和其它方法混用。

Surface支持对单个像素的访问。访问硬件surface的像素是比较慢的,推荐不要这样做。像素可以由get_at()和set_at()函数访问。用这些函数做一些简单的操作是很好的,但是做比较复杂的像素操作就比较慢了。如果你打算做很多像素级的工作,你最好使用pygame.surfarray模块,它可以把surface像多维数组一样使用(而且比较快)。

任何直接访问Surface的像素数据的函数都要求先锁定(lock())。这些函数可以自动调用lock()和unlock()完成加锁和解锁。但是如果一个函数被调用很多次,那么就会有很多多余的加锁和解锁操作。最好是在多次使用这个函数之前,手动锁定Surface,并在结束时手动解锁。所有的需要解锁的函数的文档里面都会这样说。记住,只在需要的时候才锁住Surface。

Surface的数据在内部是表示成一个数字,包含所有的颜色。使用Surface.map_rgb和Surface.unmap_rgb来在独立的红绿蓝值和Surface的内部表示值之间的转换。

一个Surface可以引用另一个Surface的一部分。这通过Surface.subsurface函数创建。改变任何一个Surface都会影响其它的Surface。

每个Surface包含一个剪切区域。默认情况下,剪切区域是整个Surface。如果改变了剪切区域,所有的画图操作都会限制在一个比较小的范围之内。

1. Surface.blit

把一个图像画到另一个上面

Surface.blit(source, dest, area=None, special_flags = 0): return Rect

把源Surface画到这个Surface上。画的位置可以由dest参数指定。dest可以是一对坐标值,表示源Surface的左上角在这个Surface上的坐标。dest也可以是一个矩形,矩形的左上角作为blit的位置,而矩形的大小不影响blit。

有一个可选的area矩形参数,可以用来指定只画源Surface的一部分。

一个可选的special_flags参数,可以是BLEND_ADD、BLEND_SUB、BLEND_MULT、BLEND_MIN、BLEND_MAX。将来也可能有其它特殊标记添加进来。

返回的矩形表示受影响的像素的区域,不包括目标Surface以外的像素,也不包括剪切区域以外的像素。

如果blit到一个8位的Surface上,像素的alpha值会被忽略。

special_flags是在pygame 1.8中新增的。

2. Surface.convert

改变图像的像素格式

Surface.convert(Surface): return Surface
Surface.convert(depth, flags=0): return Surface
Surface.convert(masks, flags=0): return Surface
Surface.convert(): return Surface

创建一个像素格式不一样的新的Surface。新的像素格式可以由现存其它Surface确定,也可以由depth,flags或者masks决定。这些参数和pygame.Surface的参数类似。

如果没有参数,新的Surface会和display Surface的像素格式一样。这是画图最快的格式。转换所有需要多次blit的Surface是一个好主意。

转换出来的Surface不会有像素alpha。如果原来的Surface里面有,他们会被去掉。参看Surface.convert_alpha来保留或者创建每像素alpha。

3. Surface.copy

创建一个Surface的拷贝

Surface.copy(): return Surface

创建一个Surface的复制品。新创建的Surface和原来的Surface有相同的像素格式、调色板和透明设置。

4. Surface.fill

用一个颜色填充Surface

Surface.fill(color, rect=None): return Rect

用一种颜色填充Surface。如果没有给定rect参数,整个Surface会被填充。rect参数会限制备填充的区域。填充区域同样受到Surface的剪切区域的影响。

颜色参数可以是RGB序列或者是颜色索引值。

这个函数会返回受影响的Surface区域。

5. Surface.set_colorkey

设置透明色colorkey

Surface.set_colorkey(Color, flags=0): return None
Surface.set_colorkey(None): return None

设置Surface的透明色。当把这个Surface blit到令一个Surface时候,和这个透明色颜色相同的像素会变成透明。color参数可以是RGB颜色或者是一个映射的整数。如果传送的是None,则Surface会没有透明色。

如果是有每像素透明的Surface,透明色会被忽略。透明色可以和Surface alpha混合使用。

可选的flags参数可以是pygame.RLEACCEL,用来在没有加速的时候提供更好的性能。设置了RLEACCEL的Surface作为源Surface blit更快,但是修改Surface的内容会更慢。

6. Surface.get_at

获得特定像素点的颜色值

Surface.get_at((x, y)): return Color

返回指定像素的RGBA颜色值。如果Surface没有每像素alpha,alpha值总是255(不透明)。如果像素位置在Surface的区域以外,会引发一个IndexError异常。

在游戏和实时模拟中,一次取得和设置一个像素是很慢的。

这个函数会根据需要临时加锁和解锁Surface。

7. Surface.set_at

设置特定像素点的颜色值

Surface.set_at((x, y), Color): return None

为一个像素设置RGBA或者映射的整数颜色值。如果Surface没有每像素alpha,alpha值会被忽略。对Surface区域之外或者剪切区域之外的像素设置颜色不会有任何效果。

在游戏和实时模拟中,一次取得和设置一个像素是很慢的。

这个函数会根据需要临时加锁和解锁Surface。

8. Surface.set_clip

设置Surface的剪切区域

Surface.set_clip(rect): return None
Surface.set_clip(None): return None

每个Surface都有一个剪切区域。这个区域是个矩形,表示只有在这个矩形内的像素可以被修改。如果指定None作为矩形区域,则整个Surface都可以修改。

剪切区域总是在Surface本身的区域只内的。如果剪切区域比Surface的区域大,则会自动缩小到Surface区域之内。

9. Surface.get_clip

获得Surface的当前剪切区域

Surface.get_clip(): return Rect

返回Surface的当前剪切区域。一个Surface总是返回一个有效的矩形,不会超过图像的边界范围。如果Surface设置了None作为剪切区域,则Surface会返回整个Surface的区域。

10. Surface.get_size

获得Surface的大小

Surface.get_size(): return (width, height)

返回Surface的像素高度和宽度

11. Surface.get_width

获得Surface的宽度

Surface.get_width(): return width

返回Surface的像素宽度

12. Surface.get_height

获得Surface的高度

Surface.get_height(): return height

返回Surface的像素高度

pygame.draw

在Surface上画一些简单的图形。这些函数可以在任何格式的Surface上使用。在硬件Surface上使用会比普通的软件Surface上慢。

大部分的函数都包括一个width参数来表示画笔的大小(即形状的边缘的宽度)。如果width是0则整个图形会被颜色填充。

所有的函数都有Surface剪切区域,它的操作会被限制在这个区域里面。这些函数返回一个矩形区域表示被改动的像素的范围。

大部分表示颜色的参数是一个RGB三元组,也可以使用RGBA四元组。如果Surface包含alpha,四元组中alpha值会被直接写入到Surface里面,画图函数并不会进行透明的绘画。颜色参数也可以是一个整数,可以映射到Surface的像素格式。

这些函数在操作时必须临时锁定Surface。如果有很多一系列的绘图操作,可以使用Surface的锁定和解锁操作来加速。

1. pygame.draw.rect

画一个矩形

pygame.draw.rect(Surface, color, Rect, width=0): return Rect

在Surface上画一个矩形。指定的Rect是矩形的区域。width参数是矩形框的粗细。如果width是0,整个矩形会被填充。

要记住的是,Surface.fill也可以画一个填充的矩形。实际上,Surface.fill在某些平台上可以用硬件加速。

2. pygame.draw.polygon

画一个有任意条边的图形

pygame.draw.polygon(Surface, color, pointlist, width=0): return Rect

在Surface上画一个多边形。pointlist参数是多边形顶点的列表。width参数是多边形的边的粗细。如果width是0,多边形是被填充的。

对于aapolygon,使用带closed参数的aalines。

3. pygame.draw.circle

围绕一个点画一个圆

pygame.draw.circle(Surface, color, pos, radius, width=0): return Rect

在Surface上画一个圆形。pos参数是圆的圆心,radius是半径大小。width参数是圆边的粗细,如果width是0圆会被填充。

4. pygame.draw.ellipse

在矩形区域中画一个椭圆的形状

pygame.draw.ellipse(Surface, color, Rect, width=0): return Rect

在Surface上画一个椭圆。给定的矩形区域是椭圆填充的区域。width是边的粗细。如果width是0,椭圆会被填充。

5. pygame.draw.arc

画椭圆的一部分

pygame.draw.arc(Surface, color, Rect, start_angle, stop_angle, width=1): return Rect

在Surface上画一个椭圆状的弧线。rect参数指定椭圆填充的矩形。两个角度参数指定起始和终止的角度(以弧度为单位),朝右为0度。width参数是弧线的粗细。

6. pygame.draw.line

画一条直线段

pygame.draw.line(Surface, color, start_pos, end_pos, width=1): return Rect

在Surface上画一条直线段。线段终点没有箭头,宽的线段终点是方形的。

7. pygame.draw.lines

画多条连续的线段

pygame.draw.lines(Surface, color, closed, pointlist, width=1): return Rect

在Surface上画一系列的直线。pointlist是一系列点的列表。如果closed参数是True,则在最后一点和第一点之间会画一条线段。

这个函数不会画终点箭头和中间连接头。线段有尖锐的拐角,粗的线段会有看上去不正确的拐角。

8. pygame.draw.aaline

画抗锯齿的线段

pygame.draw.aaline(Surface, color, startpos, endpos, blend=1): return Rect

在Surface上画一条抗锯齿的直线段。如果blend是True,则阴影部分是和原始像素混合而不是直接覆盖的。这个函数接受浮点数作为点的坐标。

9. pygame.draw.aalines

pygame.draw.aalines(Surface, color, closed, pointlist, blend=1): return Rect

画多条连续的线段,每个线段都是抗锯齿的。pointlist里面至少要两个点。closed参数如果是True,则在第一个点和最后一点之间会画一条直线。如果blend参数是True,则阴影部分是和原始像素混合而不是直接覆盖的。这个函数接受浮点数作为点的坐标。

pygame.font

font模块允许允许在新创建的Surface上画TrueType字体。这个模块是可选的,并依赖于SDL_ttf。你应该测试pygame.font是否可用,并在尝试使用前初始化它。

字体的大部分工作使用Font对象来实现。这个模块它自己仅有初始化函数和pygame.font.Font函数用来创建Font对象。

你可以使用pygame.font.SysFont从系统载入字体。有一些其它的函数用来帮助查找系统字体。

pygame自带一个默认的字体。这个字体总是可以用None作为字体名字被使用。

1. pygame.font.init

初始化font模块

pygame.font.init(): return None

这个方法会在pygame.init时自动调用。它初始化font模块。在使用这个模块的其他函数之前必须先初始化。

多次调用这个函数也不会有副作用。

2. pygame.font.quit

uninitialize font模块

pygame.font.quit(): return None

手动uninitialize SDL_ttf的字体系统。这个函数会被pygame.quit自动调用。

如果font模块没有被初始化,调用这个函数也不会有问题。

3. pygame.font.get_init

返回True如果font模块被初始化了

pygame.font.get_init(): return bool

测试font模块是否被初始化了。

4. pygame.font.get_default_font

得到默认字体的文件名

pygame.font.get_default_font(): return string

返回系统字体的文件名。这个不是文件的完整路径。这个文件通常可以在和font模块相同的目录中找到,但是它也可能在其它文档中。

5. pygame.font.get_fonts

获得所有可用的字体

pygame.font.get_fonts(): return list of strings

返回一个系统所有可用字体的列表。字体的名字会用小写,所有的空格和其它字符都被去掉了。这在大部分系统都可以用,但是某些系统找不到字体会返回一个空的列表。

6. pygame.font.match_font

在系统中查找一个特定的字体

pygame.font.match_font(name, bold=False, italic=False): return path

返回一个系统中字体文件的完整路径。如果bold或者italic参数是True,函数会尝试去查找正确的字体族。

字体的名字可以是逗号隔开的字体的列表。如果没有一个给定字体找到,函数返回None。

比如:

print pygame.font.match_font('bitstreamverasans')
# output is: /usr/share/fonts/truetype/ttf-bitstream-vera/Vera.ttf
# (but only if you have Vera on your system)

7. pygame.font.SysFont

从系统字体创建一个Font对象

pygame.font.SysFont(name, size, bold=False, italic=False): return Font

返回一个新建的从系统字体装载的Font对象。字体会对应请求的bold和italic标志。如果不能找到合适的系统字体,这个函数会载入缺省的pygame字体。字体名字可以是逗号隔开的字体名字的列表。

8. pygame.font.Font

从文件创建一个Font对象

pygame.font.Font(filename, size): return Font

pygame.font.Font(object, size): return Font

从给定的文件名或者python文件对象载入新的字体。size是字体的高度。如果文件名是None,pygame的默认字体会被载入。如果参数指定的字体不能载入,会引发一个异常。一旦字体创建后,字体的大小不能被改变。

Font对象主要用来把文字画成新的Surface对象。画的过程可以模拟黑体和斜体的效果,但是最好使用真正的斜体和黑体字体。被画文字可以使普通字符串,也可以是unicode串。

8.1. Font.render

在新的Surface上画出文字。

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

这个函数创建一个新Surface对象,它上面包含了画出来的文字。pygame没有提供直接在存在的Surface上画文字的方法,而必须采用Font.render来创建一个新的Surface,然后把它blit到其他Surface上。

文字只能包含一行,换行符不会被画出来。antialias抗锯齿参数如果是真的,字符会有光滑的边缘。color参数指定了文字的颜色(比如(0,0,255)为蓝色)。可选的background参数用来指定文字背景的颜色。如果没有指定background,背景是透明的。

返回的Surface的大小是包含文字的必须的大小。(和Font.size()一样。)如果text是空字符串,会返回一个空白的Surface,只有一个像素宽和一个字符的高度。

返回的Surface的类型依赖于background和antialias的类型。因为性能的原因,最好要知道图像是哪种类型的。如果没有使用antialias,返回的图像是带两个调色板的8位的图像。如果background是透明的,会设置一个透明色colorkey。抗锯齿的图像会使用24位RGB图像。如果背景是透明的,会使用像素alpha。

优化:如果你知道最后文字的背景是单一的颜色,并且文字是抗锯齿的,那么你可以通过指定背景色来改进性能。这使得图像通过透明色colorkey而不是alpha(这个要慢得多)来指定透明信息。

如果你画'\n'字符,一个未知的字符会被画出来,通常是一个矩形。你应该自己去处理多行字符串。

画字体不是线程安全的:一个时刻只能有一个线程在画文字。

8.2. Font.size

确定画文字需要的大小

Font.size(text): return (width, height)

返回画文字需要的大小。这个函数可以帮助确定文字的位置,也可以用来确定文字换行和其他布局效果。

注意,大部分字体都会对特定的字符使用字据调整。比如说字符串“ae”的宽度不总是和"a"+"e"的宽度一致。

8.3. Font.set_underline

控制文字是否有下划线

Font.set_underline(bool): return None

当开启这个选项, 所有的文字都会有下划线。不管字体多大,下划线总是只有一个像素的宽度。这个选项可以和粗体、斜体混合使用。

8.4. Font.get_underline

检查画出来的文字是否有下划线。

Font.get_underline(): return bool

当字体设置有下划线,这个函数返回真。

8.5. Font.set_bold

开启文字的伪粗体

Font.set_bold(bool): return None

开启伪粗体。这是对字体进行伸展而得到的伪粗体,用在很多字体上不好看。如果可能的话,应该载入一个真正的粗体。当这个选项开启后,字符会有不同的宽度。这个选项可以和斜体和下划线混合使用。

8.6. Font.get_bold

检查字体设置是否用粗体。

Font.get_bold(): return bool

如果字体设置了粗体,返回真。

8.7. Font.set_italic

设置字体的伪斜体

Font.set_bold(bool): return None

开启字体的伪斜体。这是对字体进行倾斜而得到的伪斜体,用在很多字体上不好看。如果可能的话,应该载入真正的斜体。当开启了斜体后,字符的宽度可能会不同。这个选项可以和粗体、下划线混合使用。

8.8. Font.get_italic

检查字体设置是否为斜体

Font.get_italic(): return bool

如果字体设置了斜体,则返回真。

8.9. Font.get_linesize

获得行的高度。

Font.get_linesize(): return int

返回一行文字的像素高度。当画很多行字符的时候,这个高度是两行间的间隔的推荐值。

8.10. Font.get_height

获取字体的高度。

  • Font.get_height(): return int

返回文字的实际像素高度。这是这种这种字体里的字符的平均高度。

8.11. Font.get_ascent

获得字体的ascent高度。

Font.get_ascent(): return int

返回字体的ascent像素高度。ascent是从字体的基线到字体顶部的像素高度。

8.12. Font.get_descent

获得字体的descent高度

Font.get_descent(): return int

返回字体的descent高度。descent是指从字体基线到字体底部的像素高度。

The end

Pygame图形接口基础 (2008-02-23 15:34:18由localhost编辑)

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