Source

pygame / docs / ref / rect.html

<html>
<title>rect - Pygame Documentation</title>
<body bgcolor=#aaeebb text=#000000 link=#331111 vlink=#331111>


<table cellpadding=0 cellspacing=0 border=0 style='border: 3px solid black;' width='100%'>
<tr>
<td bgcolor='#c2fc20' style='padding: 6px;' align=center valign=center><a href='http://www.pygame.org/'><img src='../pygame_tiny.gif' border=0 width=200 height=60></a><br><b>pygame documentation</b></td>
<td bgcolor='#6aee28' style='border-left: 3px solid black; padding: 6px;' align=center valign=center>
	||&nbsp;
	<a href=http://www.pygame.org>Pygame Home</a> &nbsp;||&nbsp;
	<a href=../index.html>Help Contents</a> &nbsp;||
	<a href=index.html>Reference Index</a> &nbsp;||
	<br>&nbsp;<br>
	
<a href="camera.html">Camera</a>&nbsp;||&nbsp;
<a href="cdrom.html">Cdrom</a>&nbsp;||&nbsp;
<a href="color.html">Color</a>&nbsp;||&nbsp;
<a href="cursors.html">Cursors</a>&nbsp;||&nbsp;
<a href="display.html">Display</a>&nbsp;||&nbsp;
<a href="draw.html">Draw</a>&nbsp;||&nbsp;
<a href="event.html">Event</a>&nbsp;||&nbsp;
<a href="examples.html">Examples</a>&nbsp;||&nbsp;
<a href="font.html">Font</a>&nbsp;||&nbsp;
<a href="gfxdraw.html">Gfxdraw</a>&nbsp;||&nbsp;
<a href="image.html">Image</a>&nbsp;||&nbsp;
<a href="joystick.html">Joystick</a>&nbsp;||&nbsp;
<a href="key.html">Key</a>&nbsp;||&nbsp;
<a href="locals.html">Locals</a>&nbsp;||&nbsp;
<a href="mask.html">Mask</a>&nbsp;||&nbsp;
<a href="midi.html">Midi</a>&nbsp;||&nbsp;
<a href="mixer.html">Mixer</a>&nbsp;||&nbsp;
<a href="mouse.html">Mouse</a>&nbsp;||&nbsp;
<a href="movie.html">Movie</a>&nbsp;||&nbsp;
<a href="music.html">Music</a>&nbsp;||&nbsp;
<a href="overlay.html">Overlay</a>&nbsp;||&nbsp;
<a href="pixelarray.html">Pixelarray</a>&nbsp;||&nbsp;
<a href="pygame.html">Pygame</a>&nbsp;||&nbsp;
<a href="rect.html">Rect</a>&nbsp;||&nbsp;
<a href="scrap.html">Scrap</a>&nbsp;||&nbsp;
<a href="sndarray.html">Sndarray</a>&nbsp;||&nbsp;
<a href="sprite.html">Sprite</a>&nbsp;||&nbsp;
<a href="surface.html">Surface</a>&nbsp;||&nbsp;
<a href="surfarray.html">Surfarray</a>&nbsp;||&nbsp;
<a href="tests.html">Tests</a>&nbsp;||&nbsp;
<a href="time.html">Time</a>&nbsp;||&nbsp;
<a href="transform.html">Transform</a>
</td></tr></table>
<br>


<a name="pygame.Rect">
<big><b>pygame.Rect</big></b><br><ul>
  <i>pygame object for storing rectangular coordinates</i><br>
  <tt>pygame.Rect(left, top, width, height): return Rect</tt><br>
  <tt>pygame.Rect((left, top), (width, height)): return Rect</tt><br>
  <tt>pygame.Rect(object): return Rect</tt><br>
<ul><small><table>
  <tr><td><a href="rect.html#Rect.copy">Rect.copy</a> - <font size=-1>copy the rectangle</font></td><td>copy the rectangle</td></tr>
  <tr><td><a href="rect.html#Rect.move">Rect.move</a> - <font size=-1>moves the rectangle</font></td><td>moves the rectangle</td></tr>
  <tr><td><a href="rect.html#Rect.move_ip">Rect.move_ip</a> - <font size=-1>moves the rectangle, in place</font></td><td>moves the rectangle, in place</td></tr>
  <tr><td><a href="rect.html#Rect.inflate">Rect.inflate</a> - <font size=-1>grow or shrink the rectangle size</font></td><td>grow or shrink the rectangle size</td></tr>
  <tr><td><a href="rect.html#Rect.inflate_ip">Rect.inflate_ip</a> - <font size=-1>grow or shrink the rectangle size, in place</font></td><td>grow or shrink the rectangle size, in place</td></tr>
  <tr><td><a href="rect.html#Rect.clamp">Rect.clamp</a> - <font size=-1>moves the rectangle inside another</font></td><td>moves the rectangle inside another</td></tr>
  <tr><td><a href="rect.html#Rect.clamp_ip">Rect.clamp_ip</a> - <font size=-1>moves the rectangle inside another, in place</font></td><td>moves the rectangle inside another, in place</td></tr>
  <tr><td><a href="rect.html#Rect.clip">Rect.clip</a> - <font size=-1>crops a rectangle inside another</font></td><td>crops a rectangle inside another</td></tr>
  <tr><td><a href="rect.html#Rect.union">Rect.union</a> - <font size=-1>joins two rectangles into one</font></td><td>joins two rectangles into one</td></tr>
  <tr><td><a href="rect.html#Rect.union_ip">Rect.union_ip</a> - <font size=-1>joins two rectangles into one, in place</font></td><td>joins two rectangles into one, in place</td></tr>
  <tr><td><a href="rect.html#Rect.unionall">Rect.unionall</a> - <font size=-1>the union of many rectangles</font></td><td>the union of many rectangles</td></tr>
  <tr><td><a href="rect.html#Rect.unionall_ip">Rect.unionall_ip</a> - <font size=-1>the union of many rectangles, in place</font></td><td>the union of many rectangles, in place</td></tr>
  <tr><td><a href="rect.html#Rect.fit">Rect.fit</a> - <font size=-1>resize and move a rectangle with aspect ratio</font></td><td>resize and move a rectangle with aspect ratio</td></tr>
  <tr><td><a href="rect.html#Rect.normalize">Rect.normalize</a> - <font size=-1>correct negative sizes</font></td><td>correct negative sizes</td></tr>
  <tr><td><a href="rect.html#Rect.contains">Rect.contains</a> - <font size=-1>test if one rectangle is inside another</font></td><td>test if one rectangle is inside another</td></tr>
  <tr><td><a href="rect.html#Rect.collidepoint">Rect.collidepoint</a> - <font size=-1>test if a point is inside a rectangle</font></td><td>test if a point is inside a rectangle</td></tr>
  <tr><td><a href="rect.html#Rect.colliderect">Rect.colliderect</a> - <font size=-1>test if two rectangles overlap</font></td><td>test if two rectangles overlap</td></tr>
  <tr><td><a href="rect.html#Rect.collidelist">Rect.collidelist</a> - <font size=-1>test if one rectangle in a list intersects</font></td><td>test if one rectangle in a list intersects</td></tr>
  <tr><td><a href="rect.html#Rect.collidelistall">Rect.collidelistall</a> - <font size=-1>test if all rectangles in a list intersect</font></td><td>test if all rectangles in a list intersect</td></tr>
  <tr><td><a href="rect.html#Rect.collidedict">Rect.collidedict</a> - <font size=-1>test if one rectangle in a dictionary intersects</font></td><td>test if one rectangle in a dictionary intersects</td></tr>
  <tr><td><a href="rect.html#Rect.collidedictall">Rect.collidedictall</a> - <font size=-1>test if all rectangles in a dictionary intersect</font></td><td>test if all rectangles in a dictionary intersect</td></tr>
</table></small></ul>
<p>Pygame uses Rect objects to store and manipulate rectangular areas. <tt>A</tt> 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 effects 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>
<pre>    top, left, bottom, right
    topleft, bottomleft, topright, bottomright
    midtop, midleft, midbottom, midright
    center, centerx, centery
    size, width, height
    w,h
</pre><p>All of these attributes can be assigned to: </p>
<pre>    rect1.right = 10
    rect2.center = (20,30)
</pre><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 True for a nonzero test. Some methods return a Rect with 0 size to represent an invalid rectangle. </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 <tt>(i.e.</tt>, <tt>rect1.bottom=rect2.top)</tt>, the two meet exactly on the screen but do not overlap, and <tt>rect1.colliderect(rect2)</tt> returns false. </p>
<p>Though Rect can be subclassed, methods that return new rectangles are not subclass aware. That is, move or copy with return a new <tt>pygame.Rect</tt> instance, not an instance of the subclass. This may change. To make subclass awareness work though, subclasses may have to maintain the same constructor signature as Rect. </p>
<!--COMMENTS:pygame.Rect--> &nbsp;<br> 


<a name="Rect.copy">
<big><b>Rect.copy</big></b><br><ul>
  <i>copy the rectangle</i><br>
  <tt>Rect.copy(): return Rect</tt><br>
<p>Returns a new rectangle having the same position and size as the orginal. </p>
<!--COMMENTS:Rect.copy--> &nbsp;<br> 
<br></ul>


<a name="Rect.move">
<big><b>Rect.move</big></b><br><ul>
  <i>moves the rectangle</i><br>
  <tt>Rect.move(x, y): return Rect</tt><br>
<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>
<!--COMMENTS:Rect.move--> &nbsp;<br> 
<br></ul>


<a name="Rect.move_ip">
<big><b>Rect.move_ip</big></b><br><ul>
  <i>moves the rectangle, in place</i><br>
  <tt>Rect.move_ip(x, y): return None</tt><br>
<p>Same as the <tt><a href="rect.html#Rect.move">Rect.move</a> - <font size=-1>moves the rectangle</font></tt> method, but operates in place. </p>
<!--COMMENTS:Rect.move_ip--> &nbsp;<br> 
<br></ul>


<a name="Rect.inflate">
<big><b>Rect.inflate</big></b><br><ul>
  <i>grow or shrink the rectangle size</i><br>
  <tt>Rect.inflate(x, y): return Rect</tt><br>
<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. </p>
<!--COMMENTS:Rect.inflate--> &nbsp;<br> 
<br></ul>


<a name="Rect.inflate_ip">
<big><b>Rect.inflate_ip</big></b><br><ul>
  <i>grow or shrink the rectangle size, in place</i><br>
  <tt>Rect.inflate_ip(x, y): return None</tt><br>
<p>Same as the <tt><a href="rect.html#Rect.inflate">Rect.inflate</a> - <font size=-1>grow or shrink the rectangle size</font></tt> method, but operates in place. </p>
<!--COMMENTS:Rect.inflate_ip--> &nbsp;<br> 
<br></ul>


<a name="Rect.clamp">
<big><b>Rect.clamp</big></b><br><ul>
  <i>moves the rectangle inside another</i><br>
  <tt>Rect.clamp(Rect): return Rect</tt><br>
<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>
<!--COMMENTS:Rect.clamp--> &nbsp;<br> 
<br></ul>


<a name="Rect.clamp_ip">
<big><b>Rect.clamp_ip</big></b><br><ul>
  <i>moves the rectangle inside another, in place</i><br>
  <tt>Rect.clamp_ip(Rect): return None</tt><br>
<p>Same as the <tt><a href="rect.html#Rect.clamp">Rect.clamp</a> - <font size=-1>moves the rectangle inside another</font></tt> method, but operates in place. </p>
<!--COMMENTS:Rect.clamp_ip--> &nbsp;<br> 
<br></ul>


<a name="Rect.clip">
<big><b>Rect.clip</big></b><br><ul>
  <i>crops a rectangle inside another</i><br>
  <tt>Rect.clip(Rect): return Rect</tt><br>
<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>
<!--COMMENTS:Rect.clip--> &nbsp;<br> 
<br></ul>


<a name="Rect.union">
<big><b>Rect.union</big></b><br><ul>
  <i>joins two rectangles into one</i><br>
  <tt>Rect.union(Rect): return Rect</tt><br>
<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>
<!--COMMENTS:Rect.union--> &nbsp;<br> 
<br></ul>


<a name="Rect.union_ip">
<big><b>Rect.union_ip</big></b><br><ul>
  <i>joins two rectangles into one, in place</i><br>
  <tt>Rect.union_ip(Rect): return None</tt><br>
<p>Same as the <tt><a href="rect.html#Rect.union">Rect.union</a> - <font size=-1>joins two rectangles into one</font></tt> method, but operates in place. </p>
<!--COMMENTS:Rect.union_ip--> &nbsp;<br> 
<br></ul>


<a name="Rect.unionall">
<big><b>Rect.unionall</big></b><br><ul>
  <i>the union of many rectangles</i><br>
  <tt>Rect.unionall(Rect_sequence): return Rect</tt><br>
<p>Returns the union of one rectangle with a sequence of many rectangles. </p>
<!--COMMENTS:Rect.unionall--> &nbsp;<br> 
<br></ul>


<a name="Rect.unionall_ip">
<big><b>Rect.unionall_ip</big></b><br><ul>
  <i>the union of many rectangles, in place</i><br>
  <tt>Rect.unionall_ip(Rect_sequence): return None</tt><br>
<p>The same as the <tt><a href="rect.html#Rect.unionall">Rect.unionall</a> - <font size=-1>the union of many rectangles</font></tt> method, but operates in place. </p>
<!--COMMENTS:Rect.unionall_ip--> &nbsp;<br> 
<br></ul>


<a name="Rect.fit">
<big><b>Rect.fit</big></b><br><ul>
  <i>resize and move a rectangle with aspect ratio</i><br>
  <tt>Rect.fit(Rect): return Rect</tt><br>
<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>
<!--COMMENTS:Rect.fit--> &nbsp;<br> 
<br></ul>


<a name="Rect.normalize">
<big><b>Rect.normalize</big></b><br><ul>
  <i>correct negative sizes</i><br>
  <tt>Rect.normalize(): return None</tt><br>
<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>
<!--COMMENTS:Rect.normalize--> &nbsp;<br> 
<br></ul>


<a name="Rect.contains">
<big><b>Rect.contains</big></b><br><ul>
  <i>test if one rectangle is inside another</i><br>
  <tt>Rect.contains(Rect): return bool</tt><br>
<p>Returns true when the argument is completely inside the Rect. </p>
<!--COMMENTS:Rect.contains--> &nbsp;<br> 
<br></ul>


<a name="Rect.collidepoint">
<big><b>Rect.collidepoint</big></b><br><ul>
  <i>test if a point is inside a rectangle</i><br>
  <tt>Rect.collidepoint(x, y): return bool</tt><br>
  <tt>Rect.collidepoint((x,y)): return bool</tt><br>
<p>Returns true if the given point is inside the rectangle. <tt>A</tt> point along the right or bottom edge is not considered to be inside the rectangle. </p>
<!--COMMENTS:Rect.collidepoint--> &nbsp;<br> 
<br></ul>


<a name="Rect.colliderect">
<big><b>Rect.colliderect</big></b><br><ul>
  <i>test if two rectangles overlap</i><br>
  <tt>Rect.colliderect(Rect): return bool</tt><br>
<p>Returns true if any portion of either rectangle overlap (except the top+bottom or left+right edges). </p>
<!--COMMENTS:Rect.colliderect--> &nbsp;<br> 
<br></ul>


<a name="Rect.collidelist">
<big><b>Rect.collidelist</big></b><br><ul>
  <i>test if one rectangle in a list intersects</i><br>
  <tt>Rect.collidelist(list): return index</tt><br>
<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>
<!--COMMENTS:Rect.collidelist--> &nbsp;<br> 
<br></ul>


<a name="Rect.collidelistall">
<big><b>Rect.collidelistall</big></b><br><ul>
  <i>test if all rectangles in a list intersect</i><br>
  <tt>Rect.collidelistall(list): return indices</tt><br>
<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>
<!--COMMENTS:Rect.collidelistall--> &nbsp;<br> 
<br></ul>


<a name="Rect.collidedict">
<big><b>Rect.collidedict</big></b><br><ul>
  <i>test if one rectangle in a dictionary intersects</i><br>
  <tt>Rect.collidedict(dict): return (key, value)</tt><br>
<p>Returns the key and value of the first dictionary value that collides with the Rect. If no collisions are found, None is returned. </p>
<p>Rect objects are not hashable and cannot be used as keys in a dictionary, only as values. </p>
<!--COMMENTS:Rect.collidedict--> &nbsp;<br> 
<br></ul>


<a name="Rect.collidedictall">
<big><b>Rect.collidedictall</big></b><br><ul>
  <i>test if all rectangles in a dictionary intersect</i><br>
  <tt>Rect.collidedictall(dict): return [(key, value), ...]</tt><br>
<p>Returns a list of all the key and value pairs that intersect with the Rect. If no collisions are found an empty dictionary is returned. </p>
<p>Rect objects are not hashable and cannot be used as keys in a dictionary, only as values. </p>
<!--COMMENTS:Rect.collidedictall--> &nbsp;<br> 
<br></ul>
<br></ul>

</body></html>
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.