pygame documentation | || Home || Help Contents || || pygame || cdrom || constants || display || draw || event || || font || image || joystick || key || mixer || mixer_music || || mouse || movie || sndarray || surfarray || time || transform || || CD || Channel || Clock || Font || Joystick || Movie || || Rect || Sound || Surface || || color || cursors || sprite ||

## Rect

The rectangle object is a useful object representing a rectangle area. Rectangles are created from the pygame.Rect() function. This routine is also in the locals module, so importing the locals into your namespace allows you to just use Rect().
Rect contains helpful methods, as well as a list of modifiable members:

top bottom left right | topleft topright bottomleft bottomright | midleft midright midtop midbottom | center centerx centery | size width height

When changing these members, the rectangle will be moved to the given assignment. (except when changing the size, width, or height member, which will resize the rectangle from the topleft corner)
The rectstyle arguments used frequently with the Rect object (and elsewhere in pygame) is one of the following things.

| an actual Rect object. a sequence of [xpos, ypos, width, height]. a pair of sequences, representing the position and size [[xpos, ypos], [width,height]]. if a method takes a rectstyle argument as its only argument, you can simply pass four arguments representing xpos, ypos, width, height. and perhaps most importantly: A rectstyle argument can also be _any_ python object with an attribute named 'rect'.

clamp | - move rectangle inside another
clamp_ip | - moves the rectangle inside another
clip | - rectangle cropped inside another
collidedict | - find overlapping rectangle in a dictionary
collidedictall | - find all overlapping rectangles
collidelist | - find overlapping rectangle
collidelistall | - find all overlapping rectangles
collidepoint | - point inside rectangle
colliderect | - check overlapping rectangles
contains | - check if rectangle fully inside another
inflate | - new rectangle with size changed
inflate_ip | - changes the Rect size
move | - new rectangle with position changed
move_ip | - move the Rect by the given offset
normalize | - corrects negative sizes
union | - makes new rectangle covering both inputs
union_ip | - rectangle covering both input
unionall | - rectangle covering all inputs
unionall_ip | - rectangle covering all inputs

clamp
`Rect.clamp(rectstyle) -> Rect`

Returns a new rectangle that is moved to be completely inside the argument rectangle. If the base rectangle is too large for the argument rectangle in an axis, it will be centered on that axis.

clamp_ip
`Rect.clamp_ip(rectstyle) -> None`

Moves the Rect to be completely inside the argument rectangle. If the given rectangle is too large for the argument rectangle in an axis, it will be centered on that axis.

clip
`Rect.clip(rectstyle) -> Rect`

Returns a new rectangle that is the given rectangle cropped to the inside of the base rectangle. If the two rectangles do not overlap to begin with, you will get a rectangle with 0 size.

collidedict
`Rect.collidedict(dict if rectstyle keys) -> key/value pair`

Returns the key/value pair of the first rectangle key in the dict that overlaps the base rectangle. Once an overlap is found, this will stop checking the remaining list. If no overlap is found, it will return None.
Remember python dictionary keys must be immutable, Rects are not immutable, so they cannot directly be, dictionary keys. You can convert the Rect to a tuple with the tuple() builtin command.

collidedictall
`Rect.collidedictall(rectstyle list) -> key/val list`

Returns a list of the indexes that contain rectangles overlapping the base rectangle. If no overlap is found, it will return an empty sequence.
Remember python dictionary keys must be immutable, Rects are not immutable, so they cannot directly be, dictionary keys. You can convert the Rect to a tuple with the tuple() builtin command.

collidelist
`Rect.collidelist(rectstyle list) -> int index`

Returns the index of the first rectangle in the list to overlap the base rectangle. Once an overlap is found, this will stop checking the remaining list. If no overlap is found, it will return -1.

collidelistall
`Rect.collidelistall(rectstyle list) -> index list`

Returns a list of the indexes that contain rectangles overlapping the base rectangle. If no overlap is found, it will return an empty sequence.

collidepoint
`Rect.collidepoint(x, y) -> bool`

Returns true if the given point position is inside the rectangle. If a point is on the border, it is counted as inside.

colliderect
`Rect.colliderect(rectstyle) -> bool`

Returns true if any area of the two rectangles overlaps.

contains
`Rect.contains(rectstyle) -> bool`

Returns true when the given rectangle is entirely inside the base rectangle.

inflate
`Rect.inflate(x, y) -> Rect`

Returns a new rectangle which has the sizes changed by the given amounts. The rectangle shrinks and expands around the rectangle's center. Negative values will shrink the rectangle.

inflate_ip
`Rect.inflate_ip(x, y) -> None`

Changes the Rect by the given amounts. The rectangle shrinks and expands around the rectangle's center. Negative values will shrink the rectangle.

move
`Rect.move(x, y) -> Rect`

Returns a new rectangle which is the base rectangle moved by the given amount.

move_ip
`Rect.move_ip(x, y) -> None`

Moves the rectangle which by the given amount.

normalize
`Rect.normalize() -> None`

If the rectangle has a a negative size in width or height, this will flip that axis so the sizes are positive, and the rectangle remains in the same place.

union
`Rect.union(rectstyle) -> Rect`

Returns a new Rect to completely cover the given input. There may be area inside the new Rect that is not covered by either input.

union_ip
`Rect.union_ip(rectstyle) -> None`

Resizes the Rect to completely cover the given input. There may be area inside the new dimensions that is not covered by either input.

unionall
`Rect.unionall(sequence_of_rectstyles) -> Rect`

Returns a new rectangle that completely covers all the given inputs. There may be area inside the new rectangle that is not covered by the inputs.

unionall_ip
`Rect.unionall_ip(sequence_of_rectstyles) -> None`

Resizes the rectangle to completely cover all the given inputs. There may be area inside the new rectangle that is not covered by the inputs.