Source

pygame / docs / ref / rect.html

Full commit

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


<table cellspacing=3 width=100%><tr><td bgcolor=#00000>
<table width=100%><tr><td bgcolor=c2fc20 align=center>
    <a href=http://www.pygame.org>
    <img src=../pygame_tiny.gif border=0 width=200 height=60></a><br>
    <b>pygame&nbsp;&nbsp;&nbsp;documentation</b>
</td><td bgcolor=6aee28 align=center valign=top width=100%>

	||&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=cdrom.html>Cdrom</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=font.html>Font</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=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=pygame.html>Pygame</a>&nbsp;||&nbsp;
<a href=rect.html>Rect</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=time.html>Time</a>&nbsp;||&nbsp;
<a href=transform.html>Transform</a>
</p>


</td></tr></table></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.move">Rect.move</a></td><td>moves the rectangle</td></tr>
  <tr><td><a href="rect.html#Rect.move_ip">Rect.move_ip</a></td><td>moves the rectangle, in place</td></tr>
  <tr><td><a href="rect.html#Rect.inflate">Rect.inflate</a></td><td>grow or shrink the rectangle size</td></tr>
  <tr><td><a href="rect.html#Rect.inflate_ip">Rect.inflate_ip</a></td><td>grow or shrink the rectangle size, in place</td></tr>
  <tr><td><a href="rect.html#Rect.clamp">Rect.clamp</a></td><td>moves the rectangle inside another</td></tr>
  <tr><td><a href="rect.html#Rect.clamp_ip">Rect.clamp_ip</a></td><td>moves the rectangle inside another, in place</td></tr>
  <tr><td><a href="rect.html#Rect.clip">Rect.clip</a></td><td>crops a rectangle inside another</td></tr>
  <tr><td><a href="rect.html#Rect.clip_ip">Rect.clip_ip</a></td><td>crops a rectangle inside another, in place</td></tr>
  <tr><td><a href="rect.html#Rect.union">Rect.union</a></td><td>joins two rectangles into one</td></tr>
  <tr><td><a href="rect.html#Rect.union_ip">Rect.union_ip</a></td><td>joins two rectangles into one, in place</td></tr>
  <tr><td><a href="rect.html#Rect.unionall">Rect.unionall</a></td><td>the union of many rectangles</td></tr>
  <tr><td><a href="rect.html#Rect.unionall_ip">Rect.unionall_ip</a></td><td>the union of many rectangles, in place</td></tr>
  <tr><td><a href="rect.html#Rect.fit">Rect.fit</a></td><td>resize and move a rectangle with aspect ratio</td></tr>
  <tr><td><a href="rect.html#Rect.normalize">Rect.normalize</a></td><td>correct negative sizes</td></tr>
  <tr><td><a href="rect.html#Rect.contains">Rect.contains</a></td><td>test if one rectangle is inside another</td></tr>
  <tr><td><a href="rect.html#Rect.collidepoint">Rect.collidepoint</a></td><td>test if a point is inside a rectangle</td></tr>
  <tr><td><a href="rect.html#Rect.colliderect">Rect.colliderect</a></td><td>test if two rectangles overlap</td></tr>
  <tr><td><a href="rect.html#Rect.collidelist">Rect.collidelist</a></td><td>test if one rectangle in a list intersects</td></tr>
  <tr><td><a href="rect.html#Rect.collidelistall">Rect.collidelistall</a></td><td>test if all rectangles in a list intersect</td></tr>
  <tr><td><a href="rect.html#Rect.collidedict">Rect.collidedict</a></td><td>test if one rectangle in a dictionary intersects</td></tr>
  <tr><td><a href="rect.html#Rect.collidedictall">Rect.collidedictall</a></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 effected 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
</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>
 &nbsp;<br> 


<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>
 &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></tt> method, but operates in place. </p>
 &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>
 &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></tt> method, but operates in place. </p>
 &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>
 &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></tt> method, but operates in place. </p>
 &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>
 &nbsp;<br> 
<br></ul>


<a name="Rect.clip_ip">
<big><b>Rect.clip_ip</big></b><br><ul>
  <i>crops a rectangle inside another, in place</i><br>
  <tt>Rect.clip_ip(Rect): return None</tt><br>
<p>Same as the <tt><a href="rect.html#Rect.clip">Rect.clip</a></tt> method, but operates in place. </p>
 &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>
 &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></tt> method, but operates in place. </p>
 &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>
 &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></tt> method, but operates in place. </p>
 &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>
 &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>
 &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>
 &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>
<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>
 &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>
 &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>
 &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>
 &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>
 &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>
 &nbsp;<br> 
<br></ul>
<br></ul>

</body></html>