Pygame/gfxdraw

Language.png本文当前翻译进度约为20%。
The current progress of translating this article is approximately 20%。
Icon-info.png这些 API 是试验性的,可能会在将来的版本中更变或消失。采用这些 API 的程序可能会在下个版本中崩溃。

pygame.gfxdraw

pygame 包不会自动加载 gfxdraw,所以必须明确导入。

import pygame
import pygame.gfxdraw

对于所有函数,颜色可以是以下格式之一: For all functions the arguments are strictly positional and integers are accepted for coordinates and radii. The color argument can be one of the following formats:

  • 一个 pygame.Color 对象
  • 一个 RGB 元组或列表
  • 一个 RGBA 元组或列表

The functions rectangle() and box() will accept any (X, Y, 宽, 高) sequence for their rect argument, though pygame.Rectpygame object for storing rectangular coordinates instances are preferred.

要描画一个填色、抗锯齿的形状,首先使用抗锯齿版(aa*)的函数,然后使用填色版(filled_*)的。举例:

col = (255, 0, 0)
surf.fill((255, 255, 255))
pygame.gfxdraw.aacircle(surf, x, y, 30, col)
pygame.gfxdraw.filled_circle(surf, x, y, 30, col)


对于多线程的备注:每个函数在调用的 C 语言部分释放 GIL。

注:关于另外的绘图方法,参见 pygame.draw。draw 和 gfxdraw 使用不同的 API,具有不同的功能。gfxdraw 包含了 SDL_gfx 库中的原语,而不是修改版。

New in pygame 1.9.0.

pygame.gfxdraw.pixel()

画一个像素 pixel(surface, x, y, color颜色) → 无 Draws a single pixel, at position (x ,y), on the given surface.

Parameters: surface (Surface) -- surface to draw on x (int) -- x coordinate of the pixel y (int) -- y coordinate of the pixel color (Color or tuple(int, int, int, [int])) -- color to draw with, the alpha value is optional if using a tuple (RGB[A]) Returns: None


pygame.gfxdraw.hline()

画一条横线 hline(surface, x1, x2, y, color颜色) → 无 Draws a straight horizontal line ((x1, y) to (x2, y)) on the given surface. There are no endcaps.

Parameters: surface (Surface) -- surface to draw on x1 (int) -- x coordinate of one end of the line x2 (int) -- x coordinate of the other end of the line y (int) -- y coordinate of the line color (Color or tuple(int, int, int, [int])) -- color to draw with, the alpha value is optional if using a tuple (RGB[A]) Returns: None


pygame.gfxdraw.vline()

画一条竖线 vline(surface, x, y1, y2, color颜色) → 无 Draws a straight vertical line ((x, y1) to (x, y2)) on the given surface. There are no endcaps.

Parameters: surface (Surface) -- surface to draw on x (int) -- x coordinate of the line y1 (int) -- y coordinate of one end of the line y2 (int) -- y coordinate of the other end of the line color (Color or tuple(int, int, int, [int])) -- color to draw with, the alpha value is optional if using a tuple (RGB[A]) Returns: None


pygame.gfxdraw.line()

画一条线 line(surface, x1, y1, x2, y2, color颜色) → 无 Draws a straight line ((x1, y1) to (x2, y2)) on the given surface. There are no endcaps.

Parameters: surface (Surface) -- surface to draw on x1 (int) -- x coordinate of one end of the line y1 (int) -- y coordinate of one end of the line x2 (int) -- x coordinate of the other end of the line y2 (int) -- y coordinate of the other end of the line color (Color or tuple(int, int, int, [int])) -- color to draw with, the alpha value is optional if using a tuple (RGB[A]) Returns: None


pygame.gfxdraw.rectangle()

画一个矩形 rectangle(surface, rect矩形, color颜色) → 无 Draws an unfilled rectangle on the given surface. For a filled rectangle use box().

Parameters: surface (Surface) -- surface to draw on rect (Rect) -- rectangle to draw, position and dimensions color (Color or tuple(int, int, int, [int])) -- color to draw with, the alpha value is optional if using a tuple (RGB[A]) Returns: None


Note The rect.bottom and rect.right attributes of a pygame.Rectpygame object for storing rectangular coordinates always lie one pixel outside of its actual border. Therefore, these values will not be included as part of the drawing.

pygame.gfxdraw.box()

画一个填色矩形 box(surface, rect矩形, color颜色) → 无 Draws a filled rectangle on the given surface. For an unfilled rectangle use rectangle().

Parameters: surface (Surface) -- surface to draw on rect (Rect) -- rectangle to draw, position and dimensions color (Color or tuple(int, int, int, [int])) -- color to draw with, the alpha value is optional if using a tuple (RGB[A]) Returns: None


Note The rect.bottom and rect.right attributes of a pygame.Rectpygame object for storing rectangular coordinates always lie one pixel outside of its actual border. Therefore, these values will not be included as part of the drawing. Note The pygame.Surface.fill()fill Surface with a solid color method works just as well for drawing filled rectangles. In fact pygame.Surface.fill()fill Surface with a solid color can be hardware accelerated on some platforms with both software and hardware display modes.

pygame.gfxdraw.circle()

画一个圆形 circle(surface, x, y, r半径, color颜色) → 无 Draws an unfilled circle on the given surface. For a filled circle use filled_circle().

Parameters: surface (Surface) -- surface to draw on x (int) -- x coordinate of the center of the circle y (int) -- y coordinate of the center of the circle r (int) -- radius of the circle color (Color or tuple(int, int, int, [int])) -- color to draw with, the alpha value is optional if using a tuple (RGB[A]) Returns: None


pygame.gfxdraw.aacircle()

画一个抗锯齿圆形 aacircle(surface, x, y, r半径, color颜色) → 无 Draws an unfilled antialiased circle on the given surface.

Parameters: surface (Surface) -- surface to draw on x (int) -- x coordinate of the center of the circle y (int) -- y coordinate of the center of the circle r (int) -- radius of the circle color (Color or tuple(int, int, int, [int])) -- color to draw with, the alpha value is optional if using a tuple (RGB[A]) Returns: None


pygame.gfxdraw.filled_circle()

画一个填色圆形 filled_circle(surface, x, y, r半径, color颜色) → 无 Draws a filled circle on the given surface. For an unfilled circle use circle().

Parameters: surface (Surface) -- surface to draw on x (int) -- x coordinate of the center of the circle y (int) -- y coordinate of the center of the circle r (int) -- radius of the circle color (Color or tuple(int, int, int, [int])) -- color to draw with, the alpha value is optional if using a tuple (RGB[A]) Returns: None


pygame.gfxdraw.ellipse()

画一个椭圆形 ellipse(surface, x, y, rx横向半径, ry纵向半径, color颜色) → 无 Draws an unfilled ellipse on the given surface. For a filled ellipse use filled_ellipse().

Parameters: surface (Surface) -- surface to draw on x (int) -- x coordinate of the center of the ellipse y (int) -- y coordinate of the center of the ellipse rx (int) -- horizontal radius of the ellipse ry (int) -- vertical radius of the ellipse color (Color or tuple(int, int, int, [int])) -- color to draw with, the alpha value is optional if using a tuple (RGB[A]) Returns: None


pygame.gfxdraw.aaellipse()

画一个抗锯齿椭圆形 aaellipse(surface, x, y, rx横向半径, ry纵向半径, color颜色) → 无 Draws an unfilled antialiased ellipse on the given surface.

Parameters: surface (Surface) -- surface to draw on x (int) -- x coordinate of the center of the ellipse y (int) -- y coordinate of the center of the ellipse rx (int) -- horizontal radius of the ellipse ry (int) -- vertical radius of the ellipse color (Color or tuple(int, int, int, [int])) -- color to draw with, the alpha value is optional if using a tuple (RGB[A]) Returns: None


pygame.gfxdraw.filled_ellipse()

画一个填色椭圆形 filled_ellipse(surface, x, y, rx横向半径, ry纵向半径, color) → 无 Draws a filled ellipse on the given surface. For an unfilled ellipse use ellipse().

Parameters: surface (Surface) -- surface to draw on x (int) -- x coordinate of the center of the ellipse y (int) -- y coordinate of the center of the ellipse rx (int) -- horizontal radius of the ellipse ry (int) -- vertical radius of the ellipse color (Color or tuple(int, int, int, [int])) -- color to draw with, the alpha value is optional if using a tuple (RGB[A]) Returns: None


pygame.gfxdraw.arc()

画一个圆弧 arc(surface, x, y, r, start_angle, stop_angle, color颜色) → 无 Draws an arc on the given surface. For an arc with its endpoints connected to its center use pie().

The two angle arguments are given in degrees and indicate the start and stop positions of the arc. The arc is drawn in a clockwise direction from the start_angle to the stop_angle. If start_angle == stop_angle, nothing will be drawn

Parameters: surface (Surface) -- surface to draw on x (int) -- x coordinate of the center of the arc y (int) -- y coordinate of the center of the arc r (int) -- radius of the arc start_angle (int) -- start angle in degrees stop_angle (int) -- stop angle in degrees color (Color or tuple(int, int, int, [int])) -- color to draw with, the alpha value is optional if using a tuple (RGB[A]) Returns: None


Note This function uses degrees while the pygame.draw.arc()draw an elliptical arc function uses radians.

pygame.gfxdraw.pie()

画一个饼形 pie(surface, x, y, r, start_angle, stop_angle, color颜色) → 无 Draws an unfilled pie on the given surface. A pie is an arc() with its endpoints connected to its center.

The two angle arguments are given in degrees and indicate the start and stop positions of the pie. The pie is drawn in a clockwise direction from the start_angle to the stop_angle. If start_angle == stop_angle, a straight line will be drawn from the center position at the given angle, to a length of the radius.

Parameters: surface (Surface) -- surface to draw on x (int) -- x coordinate of the center of the pie y (int) -- y coordinate of the center of the pie r (int) -- radius of the pie start_angle (int) -- start angle in degrees stop_angle (int) -- stop angle in degrees color (Color or tuple(int, int, int, [int])) -- color to draw with, the alpha value is optional if using a tuple (RGB[A]) Returns: None


Comments 1

pygame.gfxdraw.trigon()

画一个三角形 trigon(surface, x1, y1, x2, y2, x3, y3, color颜色) → 无 Draws an unfilled trigon (triangle) on the given surface. For a filled trigon use filled_trigon().

A trigon can also be drawn using polygon() e.g. polygon(surface, ((x1, y1), (x2, y2), (x3, y3)), color)

Parameters: surface (Surface) -- surface to draw on x1 (int) -- x coordinate of the first corner of the trigon y1 (int) -- y coordinate of the first corner of the trigon x2 (int) -- x coordinate of the second corner of the trigon y2 (int) -- y coordinate of the second corner of the trigon x3 (int) -- x coordinate of the third corner of the trigon y3 (int) -- y coordinate of the third corner of the trigon color (Color or tuple(int, int, int, [int])) -- color to draw with, the alpha value is optional if using a tuple (RGB[A]) Returns: None


pygame.gfxdraw.aatrigon()

画一个抗锯齿三角形 aatrigon(surface, x1, y1, x2, y2, x3, y3, color颜色) → 无 Draws an unfilled antialiased trigon (triangle) on the given surface.

An aatrigon can also be drawn using aapolygon() e.g. aapolygon(surface, ((x1, y1), (x2, y2), (x3, y3)), color)

Parameters: surface (Surface) -- surface to draw on x1 (int) -- x coordinate of the first corner of the trigon y1 (int) -- y coordinate of the first corner of the trigon x2 (int) -- x coordinate of the second corner of the trigon y2 (int) -- y coordinate of the second corner of the trigon x3 (int) -- x coordinate of the third corner of the trigon y3 (int) -- y coordinate of the third corner of the trigon color (Color or tuple(int, int, int, [int])) -- color to draw with, the alpha value is optional if using a tuple (RGB[A]) Returns: None


pygame.gfxdraw.filled_trigon()

画一个填色三角形 filled_trigon(surface, x1, y1, x2, y2, x3, y3, color) -> None Draws a filled trigon (triangle) on the given surface. For an unfilled trigon use trigon().

A filled_trigon can also be drawn using filled_polygon() e.g. filled_polygon(surface, ((x1, y1), (x2, y2), (x3, y3)), color)

Parameters: surface (Surface) -- surface to draw on x1 (int) -- x coordinate of the first corner of the trigon y1 (int) -- y coordinate of the first corner of the trigon x2 (int) -- x coordinate of the second corner of the trigon y2 (int) -- y coordinate of the second corner of the trigon x3 (int) -- x coordinate of the third corner of the trigon y3 (int) -- y coordinate of the third corner of the trigon color (Color or tuple(int, int, int, [int])) -- color to draw with, the alpha value is optional if using a tuple (RGB[A]) Returns: None


pygame.gfxdraw.polygon()

画一个多边形 polygon(surface, points, color) -> None Draws an unfilled polygon on the given surface. For a filled polygon use filled_polygon().

The adjacent coordinates in the points argument, as well as the first and last points, will be connected by line segments. e.g. For the points [(x1, y1), (x2, y2), (x3, y3)] a line segment will be drawn from (x1, y1) to (x2, y2), from (x2, y2) to (x3, y3), and from (x3, y3) to (x1, y1).

Parameters: surface (Surface) -- surface to draw on points (tuple(coordinate) or list(coordinate)) -- a sequence of 3 or more (x, y) coordinates, where each coordinate in the sequence must be a tuple/list/pygame.math.Vector2a 2-Dimensional Vector of 2 ints/floats (float values will be truncated) color (Color or tuple(int, int, int, [int])) -- color to draw with, the alpha value is optional if using a tuple (RGB[A]) Returns: None


Raises: ValueError -- if len(points) < 3 (must have at least 3 points) IndexError -- if len(coordinate) < 2 (each coordinate must have at least 2 items)

pygame.gfxdraw.aapolygon()

画一个抗锯齿多边形 aapolygon(surface, points, color) -> None Draws an unfilled antialiased polygon on the given surface.

The adjacent coordinates in the points argument, as well as the first and last points, will be connected by line segments. e.g. For the points [(x1, y1), (x2, y2), (x3, y3)] a line segment will be drawn from (x1, y1) to (x2, y2), from (x2, y2) to (x3, y3), and from (x3, y3) to (x1, y1).

Parameters: surface (Surface) -- surface to draw on points (tuple(coordinate) or list(coordinate)) -- a sequence of 3 or more (x, y) coordinates, where each coordinate in the sequence must be a tuple/list/pygame.math.Vector2a 2-Dimensional Vector of 2 ints/floats (float values will be truncated) color (Color or tuple(int, int, int, [int])) -- color to draw with, the alpha value is optional if using a tuple (RGB[A]) Returns: None


Raises: ValueError -- if len(points) < 3 (must have at least 3 points) IndexError -- if len(coordinate) < 2 (each coordinate must have at least 2 items) Comments 1

pygame.gfxdraw.filled_polygon()

画一个填色多边形 filled_polygon(surface, points, color) -> None Draws a filled polygon on the given surface. For an unfilled polygon use polygon().

The adjacent coordinates in the points argument, as well as the first and last points, will be connected by line segments. e.g. For the points [(x1, y1), (x2, y2), (x3, y3)] a line segment will be drawn from (x1, y1) to (x2, y2), from (x2, y2) to (x3, y3), and from (x3, y3) to (x1, y1).

Parameters: surface (Surface) -- surface to draw on points (tuple(coordinate) or list(coordinate)) -- a sequence of 3 or more (x, y) coordinates, where each coordinate in the sequence must be a tuple/list/pygame.math.Vector2a 2-Dimensional Vector of 2 ints/floats (float values will be truncated)` color (Color or tuple(int, int, int, [int])) -- color to draw with, the alpha value is optional if using a tuple (RGB[A]) Returns: None


Raises: ValueError -- if len(points) < 3 (must have at least 3 points) IndexError -- if len(coordinate) < 2 (each coordinate must have at least 2 items)

pygame.gfxdraw.textured_polygon()

画一个有贴图的多边形 textured_polygon(surface, points, texture, tx, ty) -> None Draws a textured polygon on the given surface. For better performance, the surface and the texture should have the same format.

A per-pixel alpha texture blit to a per-pixel alpha surface will differ from a pygame.Surface.blit()draw one image onto another blit. Also, a per-pixel alpha texture cannot be used with an 8-bit per pixel destination.

The adjacent coordinates in the points argument, as well as the first and last points, will be connected by line segments. e.g. For the points [(x1, y1), (x2, y2), (x3, y3)] a line segment will be drawn from (x1, y1) to (x2, y2), from (x2, y2) to (x3, y3), and from (x3, y3) to (x1, y1).

Parameters: surface (Surface) -- surface to draw on points (tuple(coordinate) or list(coordinate)) -- a sequence of 3 or more (x, y) coordinates, where each coordinate in the sequence must be a tuple/list/pygame.math.Vector2a 2-Dimensional Vector of 2 ints/floats (float values will be truncated) texture (Surface) -- texture to draw on the polygon tx (int) -- x offset of the texture ty (int) -- y offset of the texture Returns: None


Raises: ValueError -- if len(points) < 3 (must have at least 3 points) IndexError -- if len(coordinate) < 2 (each coordinate must have at least 2 items)

pygame.gfxdraw.bezier()

画一条贝塞尔曲线 bezier(surface, points, steps, color颜色) → 无 Draws a Bézier curve on the given surface.

Parameters:

  • surface (Surface) -- surface to draw on
  • points (tuple(coordinate) or list(coordinate)) -- a sequence of 3 or more (x, y) coordinates used to form a curve, where each coordinate in the sequence must be a tuple/list/pygame.math.Vector2a 2-Dimensional Vector of 2 ints/floats (float values will be truncated)
  • color (Color 或元组(int, int, int, [int])):使用的颜色,如使用 RGBA 元组,不透明度是可选的。

Parms int steps:

number of steps for the interpolation, the minimum is 2

如果步数<2,或者 len(points)<3,则导致 ValueError;

Raises: ValueError -- if steps < 2 ValueError -- if len(points) < 3 (must have at least 3 points) IndexError -- if len(coordinate) < 2 (each coordinate must have at least 2 items)


pygame 文档中文翻译
常用ColordisplaydraweventfontimagekeylocalsmixermouseRectSurfacetimemusicpygame
高级cursorsjoystickmaskspritetransformBufferProxyfreetypegfxdrawmidiOverlayPixelArraypixelcopysndarraysurfarraymath
其它cameracdromscraptestssurfarrayversion
链接官网英文文档日文译文