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 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
|
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
<title>pygame.Rect — pygame v2.1.2 documentation</title>
<link rel="stylesheet" type="text/css" href="../_static/pygments.css" />
<link rel="stylesheet" type="text/css" href="../_static/pygame.css" />
<script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
<script src="../_static/jquery.js"></script>
<script src="../_static/underscore.js"></script>
<script src="../_static/doctools.js"></script>
<link rel="shortcut icon" href="../_static/pygame.ico"/>
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="next" title="pygame.scrap" href="scrap.html" />
<link rel="prev" title="pygame" href="pygame.html" />
</head><body>
<div class="document">
<div class="header">
<table>
<tr>
<td class="logo">
<a href="https://www.pygame.org/">
<img src="../_static/pygame_tiny.png"/>
</a>
<h5>pygame documentation</h5>
</td>
<td class="pagelinks">
<div class="top">
<a href="https://www.pygame.org/">Pygame Home</a> ||
<a href="../index.html">Help Contents</a> ||
<a href="../genindex.html">Reference Index</a>
<form action="../search.html" method="get" style="display:inline;float:right;">
<input name="q" value="" type="text">
<input value="search" type="submit">
</form>
</div>
<hr style="color:black;border-bottom:none;border-style: dotted;border-bottom-style:none;">
<p class="bottom"><b>Most useful stuff</b>:
<a href="color.html">Color</a> |
<a href="display.html">display</a> |
<a href="draw.html">draw</a> |
<a href="event.html">event</a> |
<a href="font.html">font</a> |
<a href="image.html">image</a> |
<a href="key.html">key</a> |
<a href="locals.html">locals</a> |
<a href="mixer.html">mixer</a> |
<a href="mouse.html">mouse</a> |
<a href="rect.html">Rect</a> |
<a href="surface.html">Surface</a> |
<a href="time.html">time</a> |
<a href="music.html">music</a> |
<a href="pygame.html">pygame</a>
</p>
<p class="bottom"><b>Advanced stuff</b>:
<a href="cursors.html">cursors</a> |
<a href="joystick.html">joystick</a> |
<a href="mask.html">mask</a> |
<a href="sprite.html">sprite</a> |
<a href="transform.html">transform</a> |
<a href="bufferproxy.html">BufferProxy</a> |
<a href="freetype.html">freetype</a> |
<a href="gfxdraw.html">gfxdraw</a> |
<a href="midi.html">midi</a> |
<a href="pixelarray.html">PixelArray</a> |
<a href="pixelcopy.html">pixelcopy</a> |
<a href="sndarray.html">sndarray</a> |
<a href="surfarray.html">surfarray</a> |
<a href="math.html">math</a>
</p>
<p class="bottom"><b>Other</b>:
<a href="camera.html">camera</a> |
<a href="sdl2_controller.html#module-pygame._sdl2.controller">controller</a> |
<a href="examples.html">examples</a> |
<a href="fastevent.html">fastevent</a> |
<a href="scrap.html">scrap</a> |
<a href="tests.html">tests</a> |
<a href="touch.html">touch</a> |
<a href="pygame.html#module-pygame.version">version</a>
</p>
</td>
</tr>
</table>
</div>
<div class="documentwrapper">
<div class="body" role="main">
<section id="pygame-rect">
<dl class="py class definition">
<dt class="sig sig-object py title" id="pygame.Rect">
<span class="sig-prename descclassname"><span class="pre">pygame.</span></span><span class="sig-name descname"><span class="pre">Rect</span></span><a class="headerlink" href="#pygame.Rect" title="Permalink to this definition">¶</a></dt>
<dd><div class="line-block">
<div class="line"><span class="summaryline">pygame object for storing rectangular coordinates</span></div>
<div class="line"><span class="signature">Rect(left, top, width, height) -> Rect</span></div>
<div class="line"><span class="signature">Rect((left, top), (width, height)) -> Rect</span></div>
<div class="line"><span class="signature">Rect(object) -> Rect</span></div>
</div>
<table class="toc docutils align-default">
<colgroup>
<col style="width: 32%" />
<col style="width: 1%" />
<col style="width: 67%" />
</colgroup>
<tbody>
<tr class="row-odd"><td><div class="line"><a class="toc reference external" href="rect.html#pygame.Rect.copy">pygame.Rect.copy</a></div>
</td>
<td>—</td>
<td>copy the rectangle</td>
</tr>
<tr class="row-even"><td><div class="line"><a class="toc reference external" href="rect.html#pygame.Rect.move">pygame.Rect.move</a></div>
</td>
<td>—</td>
<td>moves the rectangle</td>
</tr>
<tr class="row-odd"><td><div class="line"><a class="toc reference external" href="rect.html#pygame.Rect.move_ip">pygame.Rect.move_ip</a></div>
</td>
<td>—</td>
<td>moves the rectangle, in place</td>
</tr>
<tr class="row-even"><td><div class="line"><a class="toc reference external" href="rect.html#pygame.Rect.inflate">pygame.Rect.inflate</a></div>
</td>
<td>—</td>
<td>grow or shrink the rectangle size</td>
</tr>
<tr class="row-odd"><td><div class="line"><a class="toc reference external" href="rect.html#pygame.Rect.inflate_ip">pygame.Rect.inflate_ip</a></div>
</td>
<td>—</td>
<td>grow or shrink the rectangle size, in place</td>
</tr>
<tr class="row-even"><td><div class="line"><a class="toc reference external" href="rect.html#pygame.Rect.update">pygame.Rect.update</a></div>
</td>
<td>—</td>
<td>sets the position and size of the rectangle</td>
</tr>
<tr class="row-odd"><td><div class="line"><a class="toc reference external" href="rect.html#pygame.Rect.clamp">pygame.Rect.clamp</a></div>
</td>
<td>—</td>
<td>moves the rectangle inside another</td>
</tr>
<tr class="row-even"><td><div class="line"><a class="toc reference external" href="rect.html#pygame.Rect.clamp_ip">pygame.Rect.clamp_ip</a></div>
</td>
<td>—</td>
<td>moves the rectangle inside another, in place</td>
</tr>
<tr class="row-odd"><td><div class="line"><a class="toc reference external" href="rect.html#pygame.Rect.clip">pygame.Rect.clip</a></div>
</td>
<td>—</td>
<td>crops a rectangle inside another</td>
</tr>
<tr class="row-even"><td><div class="line"><a class="toc reference external" href="rect.html#pygame.Rect.clipline">pygame.Rect.clipline</a></div>
</td>
<td>—</td>
<td>crops a line inside a rectangle</td>
</tr>
<tr class="row-odd"><td><div class="line"><a class="toc reference external" href="rect.html#pygame.Rect.union">pygame.Rect.union</a></div>
</td>
<td>—</td>
<td>joins two rectangles into one</td>
</tr>
<tr class="row-even"><td><div class="line"><a class="toc reference external" href="rect.html#pygame.Rect.union_ip">pygame.Rect.union_ip</a></div>
</td>
<td>—</td>
<td>joins two rectangles into one, in place</td>
</tr>
<tr class="row-odd"><td><div class="line"><a class="toc reference external" href="rect.html#pygame.Rect.unionall">pygame.Rect.unionall</a></div>
</td>
<td>—</td>
<td>the union of many rectangles</td>
</tr>
<tr class="row-even"><td><div class="line"><a class="toc reference external" href="rect.html#pygame.Rect.unionall_ip">pygame.Rect.unionall_ip</a></div>
</td>
<td>—</td>
<td>the union of many rectangles, in place</td>
</tr>
<tr class="row-odd"><td><div class="line"><a class="toc reference external" href="rect.html#pygame.Rect.fit">pygame.Rect.fit</a></div>
</td>
<td>—</td>
<td>resize and move a rectangle with aspect ratio</td>
</tr>
<tr class="row-even"><td><div class="line"><a class="toc reference external" href="rect.html#pygame.Rect.normalize">pygame.Rect.normalize</a></div>
</td>
<td>—</td>
<td>correct negative sizes</td>
</tr>
<tr class="row-odd"><td><div class="line"><a class="toc reference external" href="rect.html#pygame.Rect.contains">pygame.Rect.contains</a></div>
</td>
<td>—</td>
<td>test if one rectangle is inside another</td>
</tr>
<tr class="row-even"><td><div class="line"><a class="toc reference external" href="rect.html#pygame.Rect.collidepoint">pygame.Rect.collidepoint</a></div>
</td>
<td>—</td>
<td>test if a point is inside a rectangle</td>
</tr>
<tr class="row-odd"><td><div class="line"><a class="toc reference external" href="rect.html#pygame.Rect.colliderect">pygame.Rect.colliderect</a></div>
</td>
<td>—</td>
<td>test if two rectangles overlap</td>
</tr>
<tr class="row-even"><td><div class="line"><a class="toc reference external" href="rect.html#pygame.Rect.collidelist">pygame.Rect.collidelist</a></div>
</td>
<td>—</td>
<td>test if one rectangle in a list intersects</td>
</tr>
<tr class="row-odd"><td><div class="line"><a class="toc reference external" href="rect.html#pygame.Rect.collidelistall">pygame.Rect.collidelistall</a></div>
</td>
<td>—</td>
<td>test if all rectangles in a list intersect</td>
</tr>
<tr class="row-even"><td><div class="line"><a class="toc reference external" href="rect.html#pygame.Rect.collidedict">pygame.Rect.collidedict</a></div>
</td>
<td>—</td>
<td>test if one rectangle in a dictionary intersects</td>
</tr>
<tr class="row-odd"><td><div class="line"><a class="toc reference external" href="rect.html#pygame.Rect.collidedictall">pygame.Rect.collidedictall</a></div>
</td>
<td>—</td>
<td>test if all rectangles in a dictionary intersect</td>
</tr>
</tbody>
</table>
<p>Pygame uses Rect objects to store and manipulate rectangular areas. A Rect
can be created from a combination of left, top, width, and height values.
Rects can also be created from python objects that are already a Rect or
have an attribute named "rect".</p>
<p>Any pygame function that requires a Rect argument also accepts any of these
values to construct a Rect. This makes it easier to create Rects on the fly
as arguments to functions.</p>
<p>The Rect functions that change the position or size of a Rect return a new
copy of the Rect with the affected changes. The original Rect is not
modified. Some methods have an alternate "in-place" version that returns
None but affects the original Rect. These "in-place" methods are denoted
with the "ip" suffix.</p>
<p>The Rect object has several virtual attributes which can be used to move and
align the Rect:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">x</span><span class="p">,</span><span class="n">y</span>
<span class="n">top</span><span class="p">,</span> <span class="n">left</span><span class="p">,</span> <span class="n">bottom</span><span class="p">,</span> <span class="n">right</span>
<span class="n">topleft</span><span class="p">,</span> <span class="n">bottomleft</span><span class="p">,</span> <span class="n">topright</span><span class="p">,</span> <span class="n">bottomright</span>
<span class="n">midtop</span><span class="p">,</span> <span class="n">midleft</span><span class="p">,</span> <span class="n">midbottom</span><span class="p">,</span> <span class="n">midright</span>
<span class="n">center</span><span class="p">,</span> <span class="n">centerx</span><span class="p">,</span> <span class="n">centery</span>
<span class="n">size</span><span class="p">,</span> <span class="n">width</span><span class="p">,</span> <span class="n">height</span>
<span class="n">w</span><span class="p">,</span><span class="n">h</span>
</pre></div>
</div>
<p>All of these attributes can be assigned to:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">rect1</span><span class="o">.</span><span class="n">right</span> <span class="o">=</span> <span class="mi">10</span>
<span class="n">rect2</span><span class="o">.</span><span class="n">center</span> <span class="o">=</span> <span class="p">(</span><span class="mi">20</span><span class="p">,</span><span class="mi">30</span><span class="p">)</span>
</pre></div>
</div>
<p>Assigning to size, width or height changes the dimensions of the rectangle;
all other assignments move the rectangle without resizing it. Notice that
some attributes are integers and others are pairs of integers.</p>
<p>If a Rect has a nonzero width or height, it will return <code class="docutils literal notranslate"><span class="pre">True</span></code> for a
nonzero test. Some methods return a Rect with 0 size to represent an invalid
rectangle. A Rect with a 0 size will not collide when using collision
detection methods (e.g. <a class="reference internal" href="#pygame.Rect.collidepoint" title="pygame.Rect.collidepoint"><code class="xref py py-meth docutils literal notranslate"><span class="pre">collidepoint()</span></code></a>, <a class="reference internal" href="#pygame.Rect.colliderect" title="pygame.Rect.colliderect"><code class="xref py py-meth docutils literal notranslate"><span class="pre">colliderect()</span></code></a>, etc.).</p>
<p>The coordinates for Rect objects are all integers. The size values can be
programmed to have negative values, but these are considered illegal Rects
for most operations.</p>
<p>There are several collision tests between other rectangles. Most python
containers can be searched for collisions against a single Rect.</p>
<p>The area covered by a Rect does not include the right- and bottom-most edge
of pixels. If one Rect's bottom border is another Rect's top border (i.e.,
rect1.bottom=rect2.top), the two meet exactly on the screen but do not
overlap, and <code class="docutils literal notranslate"><span class="pre">rect1.colliderect(rect2)</span></code> returns false.</p>
<div class="versionadded">
<p><span class="versionmodified added">New in pygame 1.9.2: </span>The Rect class can be subclassed. Methods such as <code class="docutils literal notranslate"><span class="pre">copy()</span></code> and <code class="docutils literal notranslate"><span class="pre">move()</span></code>
will recognize this and return instances of the subclass.
However, the subclass's <code class="docutils literal notranslate"><span class="pre">__init__()</span></code> method is not called,
and <code class="docutils literal notranslate"><span class="pre">__new__()</span></code> is assumed to take no arguments. So these methods should be
overridden if any extra attributes need to be copied.</p>
</div>
<dl class="py method definition">
<dt class="sig sig-object py title" id="pygame.Rect.copy">
<span class="sig-name descname"><span class="pre">copy</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pygame.Rect.copy" title="Permalink to this definition">¶</a></dt>
<dd><div class="line-block">
<div class="line"><span class="summaryline">copy the rectangle</span></div>
<div class="line"><span class="signature">copy() -> Rect</span></div>
</div>
<p>Returns a new rectangle having the same position and size as the original.</p>
<p>New in pygame 1.9</p>
</dd></dl>
<dl class="py method definition">
<dt class="sig sig-object py title" id="pygame.Rect.move">
<span class="sig-name descname"><span class="pre">move</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pygame.Rect.move" title="Permalink to this definition">¶</a></dt>
<dd><div class="line-block">
<div class="line"><span class="summaryline">moves the rectangle</span></div>
<div class="line"><span class="signature">move(x, y) -> Rect</span></div>
</div>
<p>Returns a new rectangle that is moved by the given offset. The x and y
arguments can be any integer value, positive or negative.</p>
</dd></dl>
<dl class="py method definition">
<dt class="sig sig-object py title" id="pygame.Rect.move_ip">
<span class="sig-name descname"><span class="pre">move_ip</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pygame.Rect.move_ip" title="Permalink to this definition">¶</a></dt>
<dd><div class="line-block">
<div class="line"><span class="summaryline">moves the rectangle, in place</span></div>
<div class="line"><span class="signature">move_ip(x, y) -> None</span></div>
</div>
<p>Same as the <code class="docutils literal notranslate"><span class="pre">Rect.move()</span></code> method, but operates in place.</p>
</dd></dl>
<dl class="py method definition">
<dt class="sig sig-object py title" id="pygame.Rect.inflate">
<span class="sig-name descname"><span class="pre">inflate</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pygame.Rect.inflate" title="Permalink to this definition">¶</a></dt>
<dd><div class="line-block">
<div class="line"><span class="summaryline">grow or shrink the rectangle size</span></div>
<div class="line"><span class="signature">inflate(x, y) -> Rect</span></div>
</div>
<p>Returns a new rectangle with the size changed by the given offset. The
rectangle remains centered around its current center. Negative values
will shrink the rectangle. Note, uses integers, if the offset given is
too small(< 2 > -2), center will be off.</p>
</dd></dl>
<dl class="py method definition">
<dt class="sig sig-object py title" id="pygame.Rect.inflate_ip">
<span class="sig-name descname"><span class="pre">inflate_ip</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pygame.Rect.inflate_ip" title="Permalink to this definition">¶</a></dt>
<dd><div class="line-block">
<div class="line"><span class="summaryline">grow or shrink the rectangle size, in place</span></div>
<div class="line"><span class="signature">inflate_ip(x, y) -> None</span></div>
</div>
<p>Same as the <code class="docutils literal notranslate"><span class="pre">Rect.inflate()</span></code> method, but operates in place.</p>
</dd></dl>
<dl class="py method definition">
<dt class="sig sig-object py title" id="pygame.Rect.update">
<span class="sig-name descname"><span class="pre">update</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pygame.Rect.update" title="Permalink to this definition">¶</a></dt>
<dd><div class="line-block">
<div class="line"><span class="summaryline">sets the position and size of the rectangle</span></div>
<div class="line"><span class="signature">update(left, top, width, height) -> None</span></div>
<div class="line"><span class="signature">update((left, top), (width, height)) -> None</span></div>
<div class="line"><span class="signature">update(object) -> None</span></div>
</div>
<p>Sets the position and size of the rectangle, in place. See
parameters for <a class="tooltip reference internal" href="#pygame.Rect" title=""><code class="xref py py-meth docutils literal notranslate"><span class="pre">pygame.Rect()</span></code><span class="tooltip-content">pygame object for storing rectangular coordinates</span></a> for the parameters of this function.</p>
<div class="versionadded">
<p><span class="versionmodified added">New in pygame 2.0.1.</span></p>
</div>
</dd></dl>
<dl class="py method definition">
<dt class="sig sig-object py title" id="pygame.Rect.clamp">
<span class="sig-name descname"><span class="pre">clamp</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pygame.Rect.clamp" title="Permalink to this definition">¶</a></dt>
<dd><div class="line-block">
<div class="line"><span class="summaryline">moves the rectangle inside another</span></div>
<div class="line"><span class="signature">clamp(Rect) -> Rect</span></div>
</div>
<p>Returns a new rectangle that is moved to be completely inside the
argument Rect. If the rectangle is too large to fit inside, it is
centered inside the argument Rect, but its size is not changed.</p>
</dd></dl>
<dl class="py method definition">
<dt class="sig sig-object py title" id="pygame.Rect.clamp_ip">
<span class="sig-name descname"><span class="pre">clamp_ip</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pygame.Rect.clamp_ip" title="Permalink to this definition">¶</a></dt>
<dd><div class="line-block">
<div class="line"><span class="summaryline">moves the rectangle inside another, in place</span></div>
<div class="line"><span class="signature">clamp_ip(Rect) -> None</span></div>
</div>
<p>Same as the <code class="docutils literal notranslate"><span class="pre">Rect.clamp()</span></code> method, but operates in place.</p>
</dd></dl>
<dl class="py method definition">
<dt class="sig sig-object py title" id="pygame.Rect.clip">
<span class="sig-name descname"><span class="pre">clip</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pygame.Rect.clip" title="Permalink to this definition">¶</a></dt>
<dd><div class="line-block">
<div class="line"><span class="summaryline">crops a rectangle inside another</span></div>
<div class="line"><span class="signature">clip(Rect) -> Rect</span></div>
</div>
<p>Returns a new rectangle that is cropped to be completely inside the
argument Rect. If the two rectangles do not overlap to begin with, a Rect
with 0 size is returned.</p>
</dd></dl>
<dl class="py method definition">
<dt class="sig sig-object py title" id="pygame.Rect.clipline">
<span class="sig-name descname"><span class="pre">clipline</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pygame.Rect.clipline" title="Permalink to this definition">¶</a></dt>
<dd><div class="line-block">
<div class="line"><span class="summaryline">crops a line inside a rectangle</span></div>
<div class="line"><span class="signature">clipline(x1, y1, x2, y2) -> ((cx1, cy1), (cx2, cy2))</span></div>
<div class="line"><span class="signature">clipline(x1, y1, x2, y2) -> ()</span></div>
<div class="line"><span class="signature">clipline((x1, y1), (x2, y2)) -> ((cx1, cy1), (cx2, cy2))</span></div>
<div class="line"><span class="signature">clipline((x1, y1), (x2, y2)) -> ()</span></div>
<div class="line"><span class="signature">clipline((x1, y1, x2, y2)) -> ((cx1, cy1), (cx2, cy2))</span></div>
<div class="line"><span class="signature">clipline((x1, y1, x2, y2)) -> ()</span></div>
<div class="line"><span class="signature">clipline(((x1, y1), (x2, y2))) -> ((cx1, cy1), (cx2, cy2))</span></div>
<div class="line"><span class="signature">clipline(((x1, y1), (x2, y2))) -> ()</span></div>
</div>
<p>Returns the coordinates of a line that is cropped to be completely inside
the rectangle. If the line does not overlap the rectangle, then an empty
tuple is returned.</p>
<p>The line to crop can be any of the following formats (floats can be used
in place of ints, but they will be truncated):</p>
<blockquote>
<div><ul class="simple">
<li><p>four ints</p></li>
<li><p>2 lists/tuples/Vector2s of 2 ints</p></li>
<li><p>a list/tuple of four ints</p></li>
<li><p>a list/tuple of 2 lists/tuples/Vector2s of 2 ints</p></li>
</ul>
</div></blockquote>
<dl class="field-list simple">
<dt class="field-odd">Returns</dt>
<dd class="field-odd"><p>a tuple with the coordinates of the given line cropped to be
completely inside the rectangle is returned, if the given line does
not overlap the rectangle, an empty tuple is returned</p>
</dd>
<dt class="field-even">Return type</dt>
<dd class="field-even"><p>tuple(tuple(int, int), tuple(int, int)) or ()</p>
</dd>
<dt class="field-odd">Raises</dt>
<dd class="field-odd"><p><strong>TypeError</strong> -- if the line coordinates are not given as one of the
above described line formats</p>
</dd>
</dl>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>This method can be used for collision detection between a rect and a
line. See example code below.</p>
</div>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>The <code class="docutils literal notranslate"><span class="pre">rect.bottom</span></code> and <code class="docutils literal notranslate"><span class="pre">rect.right</span></code> attributes of a
<a class="tooltip reference internal" href="#pygame.Rect" title=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">pygame.Rect</span></code><span class="tooltip-content">pygame object for storing rectangular coordinates</span></a> always lie one pixel outside of its actual border.</p>
</div>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># Example using clipline().</span>
<span class="n">clipped_line</span> <span class="o">=</span> <span class="n">rect</span><span class="o">.</span><span class="n">clipline</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>
<span class="k">if</span> <span class="n">clipped_line</span><span class="p">:</span>
<span class="c1"># If clipped_line is not an empty tuple then the line</span>
<span class="c1"># collides/overlaps with the rect. The returned value contains</span>
<span class="c1"># the endpoints of the clipped line.</span>
<span class="n">start</span><span class="p">,</span> <span class="n">end</span> <span class="o">=</span> <span class="n">clipped_line</span>
<span class="n">x1</span><span class="p">,</span> <span class="n">y1</span> <span class="o">=</span> <span class="n">start</span>
<span class="n">x2</span><span class="p">,</span> <span class="n">y2</span> <span class="o">=</span> <span class="n">end</span>
<span class="k">else</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"No clipping. The line is fully outside the rect."</span><span class="p">)</span>
</pre></div>
</div>
<div class="versionadded">
<p><span class="versionmodified added">New in pygame 2.0.0.</span></p>
</div>
</dd></dl>
<dl class="py method definition">
<dt class="sig sig-object py title" id="pygame.Rect.union">
<span class="sig-name descname"><span class="pre">union</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pygame.Rect.union" title="Permalink to this definition">¶</a></dt>
<dd><div class="line-block">
<div class="line"><span class="summaryline">joins two rectangles into one</span></div>
<div class="line"><span class="signature">union(Rect) -> Rect</span></div>
</div>
<p>Returns a new rectangle that completely covers the area of the two
provided rectangles. There may be area inside the new Rect that is not
covered by the originals.</p>
</dd></dl>
<dl class="py method definition">
<dt class="sig sig-object py title" id="pygame.Rect.union_ip">
<span class="sig-name descname"><span class="pre">union_ip</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pygame.Rect.union_ip" title="Permalink to this definition">¶</a></dt>
<dd><div class="line-block">
<div class="line"><span class="summaryline">joins two rectangles into one, in place</span></div>
<div class="line"><span class="signature">union_ip(Rect) -> None</span></div>
</div>
<p>Same as the <code class="docutils literal notranslate"><span class="pre">Rect.union()</span></code> method, but operates in place.</p>
</dd></dl>
<dl class="py method definition">
<dt class="sig sig-object py title" id="pygame.Rect.unionall">
<span class="sig-name descname"><span class="pre">unionall</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pygame.Rect.unionall" title="Permalink to this definition">¶</a></dt>
<dd><div class="line-block">
<div class="line"><span class="summaryline">the union of many rectangles</span></div>
<div class="line"><span class="signature">unionall(Rect_sequence) -> Rect</span></div>
</div>
<p>Returns the union of one rectangle with a sequence of many rectangles.</p>
</dd></dl>
<dl class="py method definition">
<dt class="sig sig-object py title" id="pygame.Rect.unionall_ip">
<span class="sig-name descname"><span class="pre">unionall_ip</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pygame.Rect.unionall_ip" title="Permalink to this definition">¶</a></dt>
<dd><div class="line-block">
<div class="line"><span class="summaryline">the union of many rectangles, in place</span></div>
<div class="line"><span class="signature">unionall_ip(Rect_sequence) -> None</span></div>
</div>
<p>The same as the <code class="docutils literal notranslate"><span class="pre">Rect.unionall()</span></code> method, but operates in place.</p>
</dd></dl>
<dl class="py method definition">
<dt class="sig sig-object py title" id="pygame.Rect.fit">
<span class="sig-name descname"><span class="pre">fit</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pygame.Rect.fit" title="Permalink to this definition">¶</a></dt>
<dd><div class="line-block">
<div class="line"><span class="summaryline">resize and move a rectangle with aspect ratio</span></div>
<div class="line"><span class="signature">fit(Rect) -> Rect</span></div>
</div>
<p>Returns a new rectangle that is moved and resized to fit another. The
aspect ratio of the original Rect is preserved, so the new rectangle may
be smaller than the target in either width or height.</p>
</dd></dl>
<dl class="py method definition">
<dt class="sig sig-object py title" id="pygame.Rect.normalize">
<span class="sig-name descname"><span class="pre">normalize</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pygame.Rect.normalize" title="Permalink to this definition">¶</a></dt>
<dd><div class="line-block">
<div class="line"><span class="summaryline">correct negative sizes</span></div>
<div class="line"><span class="signature">normalize() -> None</span></div>
</div>
<p>This will flip the width or height of a rectangle if it has a negative
size. The rectangle will remain in the same place, with only the sides
swapped.</p>
</dd></dl>
<dl class="py method definition">
<dt class="sig sig-object py title" id="pygame.Rect.contains">
<span class="sig-name descname"><span class="pre">contains</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pygame.Rect.contains" title="Permalink to this definition">¶</a></dt>
<dd><div class="line-block">
<div class="line"><span class="summaryline">test if one rectangle is inside another</span></div>
<div class="line"><span class="signature">contains(Rect) -> bool</span></div>
</div>
<p>Returns true when the argument is completely inside the Rect.</p>
</dd></dl>
<dl class="py method definition">
<dt class="sig sig-object py title" id="pygame.Rect.collidepoint">
<span class="sig-name descname"><span class="pre">collidepoint</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pygame.Rect.collidepoint" title="Permalink to this definition">¶</a></dt>
<dd><div class="line-block">
<div class="line"><span class="summaryline">test if a point is inside a rectangle</span></div>
<div class="line"><span class="signature">collidepoint(x, y) -> bool</span></div>
<div class="line"><span class="signature">collidepoint((x,y)) -> bool</span></div>
</div>
<p>Returns true if the given point is inside the rectangle. A point along
the right or bottom edge is not considered to be inside the rectangle.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>For collision detection between a rect and a line the <a class="reference internal" href="#pygame.Rect.clipline" title="pygame.Rect.clipline"><code class="xref py py-meth docutils literal notranslate"><span class="pre">clipline()</span></code></a>
method can be used.</p>
</div>
</dd></dl>
<dl class="py method definition">
<dt class="sig sig-object py title" id="pygame.Rect.colliderect">
<span class="sig-name descname"><span class="pre">colliderect</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pygame.Rect.colliderect" title="Permalink to this definition">¶</a></dt>
<dd><div class="line-block">
<div class="line"><span class="summaryline">test if two rectangles overlap</span></div>
<div class="line"><span class="signature">colliderect(Rect) -> bool</span></div>
</div>
<p>Returns true if any portion of either rectangle overlap (except the
top+bottom or left+right edges).</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>For collision detection between a rect and a line the <a class="reference internal" href="#pygame.Rect.clipline" title="pygame.Rect.clipline"><code class="xref py py-meth docutils literal notranslate"><span class="pre">clipline()</span></code></a>
method can be used.</p>
</div>
</dd></dl>
<dl class="py method definition">
<dt class="sig sig-object py title" id="pygame.Rect.collidelist">
<span class="sig-name descname"><span class="pre">collidelist</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pygame.Rect.collidelist" title="Permalink to this definition">¶</a></dt>
<dd><div class="line-block">
<div class="line"><span class="summaryline">test if one rectangle in a list intersects</span></div>
<div class="line"><span class="signature">collidelist(list) -> index</span></div>
</div>
<p>Test whether the rectangle collides with any in a sequence of rectangles.
The index of the first collision found is returned. If no collisions are
found an index of -1 is returned.</p>
</dd></dl>
<dl class="py method definition">
<dt class="sig sig-object py title" id="pygame.Rect.collidelistall">
<span class="sig-name descname"><span class="pre">collidelistall</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pygame.Rect.collidelistall" title="Permalink to this definition">¶</a></dt>
<dd><div class="line-block">
<div class="line"><span class="summaryline">test if all rectangles in a list intersect</span></div>
<div class="line"><span class="signature">collidelistall(list) -> indices</span></div>
</div>
<p>Returns a list of all the indices that contain rectangles that collide
with the Rect. If no intersecting rectangles are found, an empty list is
returned.</p>
</dd></dl>
<dl class="py method definition">
<dt class="sig sig-object py title" id="pygame.Rect.collidedict">
<span class="sig-name descname"><span class="pre">collidedict</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pygame.Rect.collidedict" title="Permalink to this definition">¶</a></dt>
<dd><div class="line-block">
<div class="line"><span class="summaryline">test if one rectangle in a dictionary intersects</span></div>
<div class="line"><span class="signature">collidedict(dict) -> (key, value)</span></div>
<div class="line"><span class="signature">collidedict(dict) -> None</span></div>
<div class="line"><span class="signature">collidedict(dict, use_values=0) -> (key, value)</span></div>
<div class="line"><span class="signature">collidedict(dict, use_values=0) -> None</span></div>
</div>
<p>Returns the first key and value pair that intersects with the calling
Rect object. If no collisions are found, <code class="docutils literal notranslate"><span class="pre">None</span></code> is returned. If
<code class="docutils literal notranslate"><span class="pre">use_values</span></code> is 0 (default) then the dict's keys will be used in the
collision detection, otherwise the dict's values will be used.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>Rect objects cannot be used as keys in a dictionary (they are not
hashable), so they must be converted to a tuple.
e.g. <code class="docutils literal notranslate"><span class="pre">rect.collidedict({tuple(key_rect)</span> <span class="pre">:</span> <span class="pre">value})</span></code></p>
</div>
</dd></dl>
<dl class="py method definition">
<dt class="sig sig-object py title" id="pygame.Rect.collidedictall">
<span class="sig-name descname"><span class="pre">collidedictall</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pygame.Rect.collidedictall" title="Permalink to this definition">¶</a></dt>
<dd><div class="line-block">
<div class="line"><span class="summaryline">test if all rectangles in a dictionary intersect</span></div>
<div class="line"><span class="signature">collidedictall(dict) -> [(key, value), ...]</span></div>
<div class="line"><span class="signature">collidedictall(dict, use_values=0) -> [(key, value), ...]</span></div>
</div>
<p>Returns a list of all the key and value pairs that intersect with the
calling Rect object. If no collisions are found an empty list is returned.
If <code class="docutils literal notranslate"><span class="pre">use_values</span></code> is 0 (default) then the dict's keys will be used in the
collision detection, otherwise the dict's values will be used.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>Rect objects cannot be used as keys in a dictionary (they are not
hashable), so they must be converted to a tuple.
e.g. <code class="docutils literal notranslate"><span class="pre">rect.collidedictall({tuple(key_rect)</span> <span class="pre">:</span> <span class="pre">value})</span></code></p>
</div>
</dd></dl>
</dd></dl>
</section>
<br /><br />
<hr />
<a href="https://github.com/pygame/pygame/edit/main/docs/reST/ref/rect.rst" rel="nofollow">Edit on GitHub</a>
<div class="clearer"></div>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="scrap.html" title="pygame.scrap"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="pygame.html" title="pygame"
accesskey="P">previous</a> |</li>
<li class="nav-item nav-item-0"><a href="../index.html">pygame v2.1.2 documentation</a> »</li>
<li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">pygame.Rect</span></code></a></li>
<script type="text/javascript" src="https://www.pygame.org/comment/jquery.plugin.docscomments.js"></script>
</ul>
</div>
<div class="footer" role="contentinfo">
© Copyright 2000-2021, pygame developers.
</div>
</body>
</html>
|