1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
|
.. include:: common.txt
:mod:`pygame.gfxdraw`
=====================
.. module:: pygame.gfxdraw
:synopsis: pygame module for drawing shapes
| :sl:`pygame module for drawing shapes`
**EXPERIMENTAL!**: This API may change or disappear in later pygame releases. If
you use this, your code may break with the next pygame release.
The pygame package does not import gfxdraw automatically when loaded, so it
must imported explicitly to be used.
::
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:
- a :mod:`pygame.Color` object
- an ``(RGB)`` triplet (tuple/list)
- an ``(RGBA)`` quadruplet (tuple/list)
The functions :meth:`rectangle` and :meth:`box` will accept any ``(x, y, w, h)``
sequence for their ``rect`` argument, though :mod:`pygame.Rect` instances are
preferred.
To draw a filled antialiased shape, first use the antialiased (aa*) version
of the function, and then use the filled (filled_*) version.
For example:
::
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)
.. note::
For threading, each of the functions releases the GIL during the C part of
the call.
.. note::
See the :mod:`pygame.draw` module for alternative draw methods.
The ``pygame.gfxdraw`` module differs from the :mod:`pygame.draw` module in
the API it uses and the different draw functions available.
``pygame.gfxdraw`` wraps the primitives from the library called SDL_gfx,
rather than using modified versions.
.. versionadded:: 1.9.0
.. function:: pixel
| :sl:`draw a pixel`
| :sg:`pixel(surface, x, y, color) -> None`
Draws a single pixel, at position (x ,y), on the given surface.
:param Surface surface: surface to draw on
:param int x: x coordinate of the pixel
:param int y: y coordinate of the pixel
:param color: color to draw with, the alpha value is optional if using a
tuple ``(RGB[A])``
:type color: Color or tuple(int, int, int, [int])
:returns: ``None``
:rtype: NoneType
.. ## pygame.gfxdraw.pixel ##
.. function:: hline
| :sl:`draw a horizontal line`
| :sg:`hline(surface, x1, x2, y, color) -> None`
Draws a straight horizontal line (``(x1, y)`` to ``(x2, y)``) on the given
surface. There are no endcaps.
:param Surface surface: surface to draw on
:param int x1: x coordinate of one end of the line
:param int x2: x coordinate of the other end of the line
:param int y: y coordinate of the line
:param color: color to draw with, the alpha value is optional if using a
tuple ``(RGB[A])``
:type color: Color or tuple(int, int, int, [int])
:returns: ``None``
:rtype: NoneType
.. ## pygame.gfxdraw.hline ##
.. function:: vline
| :sl:`draw a vertical line`
| :sg:`vline(surface, x, y1, y2, color) -> None`
Draws a straight vertical line (``(x, y1)`` to ``(x, y2)``) on the given
surface. There are no endcaps.
:param Surface surface: surface to draw on
:param int x: x coordinate of the line
:param int y1: y coordinate of one end of the line
:param int y2: y coordinate of the other end of the line
:param color: color to draw with, the alpha value is optional if using a
tuple ``(RGB[A])``
:type color: Color or tuple(int, int, int, [int])
:returns: ``None``
:rtype: NoneType
.. ## pygame.gfxdraw.vline ##
.. function:: line
| :sl:`draw a line`
| :sg:`line(surface, x1, y1, x2, y2, color) -> None`
Draws a straight line (``(x1, y1)`` to ``(x2, y2)``) on the given surface.
There are no endcaps.
:param Surface surface: surface to draw on
:param int x1: x coordinate of one end of the line
:param int y1: y coordinate of one end of the line
:param int x2: x coordinate of the other end of the line
:param int y2: y coordinate of the other end of the line
:param color: color to draw with, the alpha value is optional if using a
tuple ``(RGB[A])``
:type color: Color or tuple(int, int, int, [int])
:returns: ``None``
:rtype: NoneType
.. ## pygame.gfxdraw.line ##
.. function:: rectangle
| :sl:`draw a rectangle`
| :sg:`rectangle(surface, rect, color) -> None`
Draws an unfilled rectangle on the given surface. For a filled rectangle use
:meth:`box`.
:param Surface surface: surface to draw on
:param Rect rect: rectangle to draw, position and dimensions
:param color: color to draw with, the alpha value is optional if using a
tuple ``(RGB[A])``
:type color: Color or tuple(int, int, int, [int])
:returns: ``None``
:rtype: NoneType
.. note::
The ``rect.bottom`` and ``rect.right`` attributes of a :mod:`pygame.Rect`
always lie one pixel outside of its actual border. Therefore, these
values will not be included as part of the drawing.
.. ## pygame.gfxdraw.rectangle ##
.. function:: box
| :sl:`draw a filled rectangle`
| :sg:`box(surface, rect, color) -> None`
Draws a filled rectangle on the given surface. For an unfilled rectangle use
:meth:`rectangle`.
:param Surface surface: surface to draw on
:param Rect rect: rectangle to draw, position and dimensions
:param color: color to draw with, the alpha value is optional if using a
tuple ``(RGB[A])``
:type color: Color or tuple(int, int, int, [int])
:returns: ``None``
:rtype: NoneType
.. note::
The ``rect.bottom`` and ``rect.right`` attributes of a :mod:`pygame.Rect`
always lie one pixel outside of its actual border. Therefore, these
values will not be included as part of the drawing.
.. note::
The :func:`pygame.Surface.fill` method works just as well for drawing
filled rectangles. In fact :func:`pygame.Surface.fill` can be hardware
accelerated on some platforms with both software and hardware display
modes.
.. ## pygame.gfxdraw.box ##
.. function:: circle
| :sl:`draw a circle`
| :sg:`circle(surface, x, y, r, color) -> None`
Draws an unfilled circle on the given surface. For a filled circle use
:meth:`filled_circle`.
:param Surface surface: surface to draw on
:param int x: x coordinate of the center of the circle
:param int y: y coordinate of the center of the circle
:param int r: radius of the circle
:param color: color to draw with, the alpha value is optional if using a
tuple ``(RGB[A])``
:type color: Color or tuple(int, int, int, [int])
:returns: ``None``
:rtype: NoneType
.. ## pygame.gfxdraw.circle ##
.. function:: aacircle
| :sl:`draw an antialiased circle`
| :sg:`aacircle(surface, x, y, r, color) -> None`
Draws an unfilled antialiased circle on the given surface.
:param Surface surface: surface to draw on
:param int x: x coordinate of the center of the circle
:param int y: y coordinate of the center of the circle
:param int r: radius of the circle
:param color: color to draw with, the alpha value is optional if using a
tuple ``(RGB[A])``
:type color: Color or tuple(int, int, int, [int])
:returns: ``None``
:rtype: NoneType
.. ## pygame.gfxdraw.aacircle ##
.. function:: filled_circle
| :sl:`draw a filled circle`
| :sg:`filled_circle(surface, x, y, r, color) -> None`
Draws a filled circle on the given surface. For an unfilled circle use
:meth:`circle`.
:param Surface surface: surface to draw on
:param int x: x coordinate of the center of the circle
:param int y: y coordinate of the center of the circle
:param int r: radius of the circle
:param color: color to draw with, the alpha value is optional if using a
tuple ``(RGB[A])``
:type color: Color or tuple(int, int, int, [int])
:returns: ``None``
:rtype: NoneType
.. ## pygame.gfxdraw.filled_circle ##
.. function:: ellipse
| :sl:`draw an ellipse`
| :sg:`ellipse(surface, x, y, rx, ry, color) -> None`
Draws an unfilled ellipse on the given surface. For a filled ellipse use
:meth:`filled_ellipse`.
:param Surface surface: surface to draw on
:param int x: x coordinate of the center of the ellipse
:param int y: y coordinate of the center of the ellipse
:param int rx: horizontal radius of the ellipse
:param int ry: vertical radius of the ellipse
:param color: color to draw with, the alpha value is optional if using a
tuple ``(RGB[A])``
:type color: Color or tuple(int, int, int, [int])
:returns: ``None``
:rtype: NoneType
.. ## pygame.gfxdraw.ellipse ##
.. function:: aaellipse
| :sl:`draw an antialiased ellipse`
| :sg:`aaellipse(surface, x, y, rx, ry, color) -> None`
Draws an unfilled antialiased ellipse on the given surface.
:param Surface surface: surface to draw on
:param int x: x coordinate of the center of the ellipse
:param int y: y coordinate of the center of the ellipse
:param int rx: horizontal radius of the ellipse
:param int ry: vertical radius of the ellipse
:param color: color to draw with, the alpha value is optional if using a
tuple ``(RGB[A])``
:type color: Color or tuple(int, int, int, [int])
:returns: ``None``
:rtype: NoneType
.. ## pygame.gfxdraw.aaellipse ##
.. function:: filled_ellipse
| :sl:`draw a filled ellipse`
| :sg:`filled_ellipse(surface, x, y, rx, ry, color) -> None`
Draws a filled ellipse on the given surface. For an unfilled ellipse use
:meth:`ellipse`.
:param Surface surface: surface to draw on
:param int x: x coordinate of the center of the ellipse
:param int y: y coordinate of the center of the ellipse
:param int rx: horizontal radius of the ellipse
:param int ry: vertical radius of the ellipse
:param color: color to draw with, the alpha value is optional if using a
tuple ``(RGB[A])``
:type color: Color or tuple(int, int, int, [int])
:returns: ``None``
:rtype: NoneType
.. ## pygame.gfxdraw.filled_ellipse ##
.. function:: arc
| :sl:`draw an arc`
| :sg:`arc(surface, x, y, r, start_angle, stop_angle, color) -> None`
Draws an arc on the given surface. For an arc with its endpoints connected
to its center use :meth:`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
:param Surface surface: surface to draw on
:param int x: x coordinate of the center of the arc
:param int y: y coordinate of the center of the arc
:param int r: radius of the arc
:param int start_angle: start angle in degrees
:param int stop_angle: stop angle in degrees
:param color: color to draw with, the alpha value is optional if using a
tuple ``(RGB[A])``
:type color: Color or tuple(int, int, int, [int])
:returns: ``None``
:rtype: NoneType
.. note::
This function uses *degrees* while the :func:`pygame.draw.arc` function
uses *radians*.
.. ## pygame.gfxdraw.arc ##
.. function:: pie
| :sl:`draw a pie`
| :sg:`pie(surface, x, y, r, start_angle, stop_angle, color) -> None`
Draws an unfilled pie on the given surface. A pie is an :meth:`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.
:param Surface surface: surface to draw on
:param int x: x coordinate of the center of the pie
:param int y: y coordinate of the center of the pie
:param int r: radius of the pie
:param int start_angle: start angle in degrees
:param int stop_angle: stop angle in degrees
:param color: color to draw with, the alpha value is optional if using a
tuple ``(RGB[A])``
:type color: Color or tuple(int, int, int, [int])
:returns: ``None``
:rtype: NoneType
.. ## pygame.gfxdraw.pie ##
.. function:: trigon
| :sl:`draw a trigon/triangle`
| :sg:`trigon(surface, x1, y1, x2, y2, x3, y3, color) -> None`
Draws an unfilled trigon (triangle) on the given surface. For a filled
trigon use :meth:`filled_trigon`.
A trigon can also be drawn using :meth:`polygon` e.g.
``polygon(surface, ((x1, y1), (x2, y2), (x3, y3)), color)``
:param Surface surface: surface to draw on
:param int x1: x coordinate of the first corner of the trigon
:param int y1: y coordinate of the first corner of the trigon
:param int x2: x coordinate of the second corner of the trigon
:param int y2: y coordinate of the second corner of the trigon
:param int x3: x coordinate of the third corner of the trigon
:param int y3: y coordinate of the third corner of the trigon
:param color: color to draw with, the alpha value is optional if using a
tuple ``(RGB[A])``
:type color: Color or tuple(int, int, int, [int])
:returns: ``None``
:rtype: NoneType
.. ## pygame.gfxdraw.trigon ##
.. function:: aatrigon
| :sl:`draw an antialiased trigon/triangle`
| :sg:`aatrigon(surface, x1, y1, x2, y2, x3, y3, color) -> None`
Draws an unfilled antialiased trigon (triangle) on the given surface.
An aatrigon can also be drawn using :meth:`aapolygon` e.g.
``aapolygon(surface, ((x1, y1), (x2, y2), (x3, y3)), color)``
:param Surface surface: surface to draw on
:param int x1: x coordinate of the first corner of the trigon
:param int y1: y coordinate of the first corner of the trigon
:param int x2: x coordinate of the second corner of the trigon
:param int y2: y coordinate of the second corner of the trigon
:param int x3: x coordinate of the third corner of the trigon
:param int y3: y coordinate of the third corner of the trigon
:param color: color to draw with, the alpha value is optional if using a
tuple ``(RGB[A])``
:type color: Color or tuple(int, int, int, [int])
:returns: ``None``
:rtype: NoneType
.. ## pygame.gfxdraw.aatrigon ##
.. function:: filled_trigon
| :sl:`draw a filled trigon/triangle`
| :sg:`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 :meth:`trigon`.
A filled_trigon can also be drawn using :meth:`filled_polygon` e.g.
``filled_polygon(surface, ((x1, y1), (x2, y2), (x3, y3)), color)``
:param Surface surface: surface to draw on
:param int x1: x coordinate of the first corner of the trigon
:param int y1: y coordinate of the first corner of the trigon
:param int x2: x coordinate of the second corner of the trigon
:param int y2: y coordinate of the second corner of the trigon
:param int x3: x coordinate of the third corner of the trigon
:param int y3: y coordinate of the third corner of the trigon
:param color: color to draw with, the alpha value is optional if using a
tuple ``(RGB[A])``
:type color: Color or tuple(int, int, int, [int])
:returns: ``None``
:rtype: NoneType
.. ## pygame.gfxdraw.filled_trigon ##
.. function:: polygon
| :sl:`draw a polygon`
| :sg:`polygon(surface, points, color) -> None`
Draws an unfilled polygon on the given surface. For a filled polygon use
:meth:`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)``.
:param Surface surface: surface to draw on
:param points: a sequence of 3 or more (x, y) coordinates, where each
*coordinate* in the sequence must be a
tuple/list/:class:`pygame.math.Vector2` of 2 ints/floats (float values
will be truncated)
:type points: tuple(coordinate) or list(coordinate)
:param color: color to draw with, the alpha value is optional if using a
tuple ``(RGB[A])``
:type color: Color or tuple(int, int, int, [int])
:returns: ``None``
:rtype: NoneType
:raises ValueError: if ``len(points) < 3`` (must have at least 3 points)
:raises IndexError: if ``len(coordinate) < 2`` (each coordinate must have
at least 2 items)
.. ## pygame.gfxdraw.polygon ##
.. function:: aapolygon
| :sl:`draw an antialiased polygon`
| :sg:`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)``.
:param Surface surface: surface to draw on
:param points: a sequence of 3 or more (x, y) coordinates, where each
*coordinate* in the sequence must be a
tuple/list/:class:`pygame.math.Vector2` of 2 ints/floats (float values
will be truncated)
:type points: tuple(coordinate) or list(coordinate)
:param color: color to draw with, the alpha value is optional if using a
tuple ``(RGB[A])``
:type color: Color or tuple(int, int, int, [int])
:returns: ``None``
:rtype: NoneType
:raises ValueError: if ``len(points) < 3`` (must have at least 3 points)
:raises IndexError: if ``len(coordinate) < 2`` (each coordinate must have
at least 2 items)
.. ## pygame.gfxdraw.aapolygon ##
.. function:: filled_polygon
| :sl:`draw a filled polygon`
| :sg:`filled_polygon(surface, points, color) -> None`
Draws a filled polygon on the given surface. For an unfilled polygon use
:meth:`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)``.
:param Surface surface: surface to draw on
:param points: a sequence of 3 or more (x, y) coordinates, where each
*coordinate* in the sequence must be a
tuple/list/:class:`pygame.math.Vector2` of 2 ints/floats (float values
will be truncated)`
:type points: tuple(coordinate) or list(coordinate)
:param color: color to draw with, the alpha value is optional if using a
tuple ``(RGB[A])``
:type color: Color or tuple(int, int, int, [int])
:returns: ``None``
:rtype: NoneType
:raises ValueError: if ``len(points) < 3`` (must have at least 3 points)
:raises IndexError: if ``len(coordinate) < 2`` (each coordinate must have
at least 2 items)
.. ## pygame.gfxdraw.filled_polygon ##
.. function:: textured_polygon
| :sl:`draw a textured polygon`
| :sg:`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 :func:`pygame.Surface.blit` 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)``.
:param Surface surface: surface to draw on
:param points: a sequence of 3 or more (x, y) coordinates, where each
*coordinate* in the sequence must be a
tuple/list/:class:`pygame.math.Vector2` of 2 ints/floats (float values
will be truncated)
:type points: tuple(coordinate) or list(coordinate)
:param Surface texture: texture to draw on the polygon
:param int tx: x offset of the texture
:param int ty: y offset of the texture
:returns: ``None``
:rtype: NoneType
:raises ValueError: if ``len(points) < 3`` (must have at least 3 points)
:raises IndexError: if ``len(coordinate) < 2`` (each coordinate must have
at least 2 items)
.. ## pygame.gfxdraw.textured_polygon ##
.. function:: bezier
| :sl:`draw a Bezier curve`
| :sg:`bezier(surface, points, steps, color) -> None`
Draws a Bézier curve on the given surface.
:param Surface surface: surface to draw on
:param points: 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/:class:`pygame.math.Vector2` of 2 ints/floats (float values
will be truncated)
:type points: tuple(coordinate) or list(coordinate)
:param int steps: number of steps for the interpolation, the minimum is 2
:param color: color to draw with, the alpha value is optional if using a
tuple ``(RGB[A])``
:type color: Color or tuple(int, int, int, [int])
:returns: ``None``
:rtype: NoneType
:raises ValueError: if ``steps < 2``
:raises ValueError: if ``len(points) < 3`` (must have at least 3 points)
:raises IndexError: if ``len(coordinate) < 2`` (each coordinate must have
at least 2 items)
.. ## pygame.gfxdraw.bezier ##
.. ## pygame.gfxdraw ##
|