rect - Pygame Documentation

pygame documentation | || Pygame Home || Help Contents || Reference Index || Camera || Cdrom || Color || Cursors || Display || Draw || Event || Examples || Font || Gfxdraw || Image || Joystick || Key || Locals || Mask || Midi || Mixer || Mouse || Movie || Music || Overlay || Pixelarray || Pygame || Rect || Scrap || Sndarray || Sprite || Surface || Surfarray || Tests || Time || Transform

pygame.Rect

pygame object for storing rectangular coordinates
`pygame.Rect(left, top, width, height): return Rect`
`pygame.Rect((left, top), (width, height)): return Rect`
`pygame.Rect(object): return Rect`

Rect.copy - copy the rectangle | copy the rectangle
Rect.move - moves the rectangle | moves the rectangle
Rect.move_ip - moves the rectangle, in place | moves the rectangle, in place
Rect.inflate - grow or shrink the rectangle size | grow or shrink the rectangle size
Rect.inflate_ip - grow or shrink the rectangle size, in place | grow or shrink the rectangle size, in place
Rect.clamp - moves the rectangle inside another | moves the rectangle inside another
Rect.clamp_ip - moves the rectangle inside another, in place | moves the rectangle inside another, in place
Rect.clip - crops a rectangle inside another | crops a rectangle inside another
Rect.union - joins two rectangles into one | joins two rectangles into one
Rect.union_ip - joins two rectangles into one, in place | joins two rectangles into one, in place
Rect.unionall - the union of many rectangles | the union of many rectangles
Rect.unionall_ip - the union of many rectangles, in place | the union of many rectangles, in place
Rect.fit - resize and move a rectangle with aspect ratio | resize and move a rectangle with aspect ratio
Rect.normalize - correct negative sizes | correct negative sizes
Rect.contains - test if one rectangle is inside another | test if one rectangle is inside another
Rect.collidepoint - test if a point is inside a rectangle | test if a point is inside a rectangle
Rect.colliderect - test if two rectangles overlap | test if two rectangles overlap
Rect.collidelist - test if one rectangle in a list intersects | test if one rectangle in a list intersects
Rect.collidelistall - test if all rectangles in a list intersect | test if all rectangles in a list intersect
Rect.collidedict - test if one rectangle in a dictionary intersects | test if one rectangle in a dictionary intersects
Rect.collidedictall - test if all rectangles in a dictionary intersect | test if all rectangles in a dictionary intersect

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".

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.

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.

The Rect object has several virtual attributes which can be used to move and align the Rect:

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

All of these attributes can be assigned to:

        rect1.right = 10
        rect2.center = (20,30)

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.

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.

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.

There are several collision tests between other rectangles. Most python containers can be searched for collisions against a single Rect.

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 `rect1.colliderect(rect2)` returns false.

Though Rect can be subclassed, methods that return new rectangles are not subclass aware. That is, move or copy return a new `pygame.Rect` 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.

Rect.copy

copy the rectangle
`Rect.copy(): return Rect`

Returns a new rectangle having the same position and size as the orginal.

Rect.move

moves the rectangle
`Rect.move(x, y): return Rect`

Returns a new rectangle that is moved by the given offset. The x and y arguments can be any integer value, positive or negative.

Rect.move_ip

moves the rectangle, in place
`Rect.move_ip(x, y): return None`

Same as the `Rect.move - moves the rectangle` method, but operates in place.

Rect.inflate

grow or shrink the rectangle size
`Rect.inflate(x, y): return Rect`

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.

Rect.inflate_ip

grow or shrink the rectangle size, in place
`Rect.inflate_ip(x, y): return None`

Same as the `Rect.inflate - grow or shrink the rectangle size` method, but operates in place.

Rect.clamp

moves the rectangle inside another
`Rect.clamp(Rect): return Rect`

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.

Rect.clamp_ip

moves the rectangle inside another, in place
`Rect.clamp_ip(Rect): return None`

Same as the `Rect.clamp - moves the rectangle inside another` method, but operates in place.

Rect.clip

crops a rectangle inside another
`Rect.clip(Rect): return Rect`

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.

Rect.union

joins two rectangles into one
`Rect.union(Rect): return Rect`

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.

Rect.union_ip

joins two rectangles into one, in place
`Rect.union_ip(Rect): return None`

Same as the `Rect.union - joins two rectangles into one` method, but operates in place.

Rect.unionall

the union of many rectangles
`Rect.unionall(Rect_sequence): return Rect`

Returns the union of one rectangle with a sequence of many rectangles.

Rect.unionall_ip

the union of many rectangles, in place
`Rect.unionall_ip(Rect_sequence): return None`

The same as the `Rect.unionall - the union of many rectangles` method, but operates in place.

Rect.fit

resize and move a rectangle with aspect ratio
`Rect.fit(Rect): return Rect`

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.

Rect.normalize

correct negative sizes
`Rect.normalize(): return None`

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.

Rect.contains

test if one rectangle is inside another
`Rect.contains(Rect): return bool`

Returns true when the argument is completely inside the Rect.

Rect.collidepoint

test if a point is inside a rectangle
`Rect.collidepoint(x, y): return bool`
`Rect.collidepoint((x,y)): return bool`

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.

Rect.colliderect

test if two rectangles overlap
`Rect.colliderect(Rect): return bool`

Returns true if any portion of either rectangle overlap (except the top+bottom or left+right edges).

Rect.collidelist

test if one rectangle in a list intersects
`Rect.collidelist(list): return index`

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.

Rect.collidelistall

test if all rectangles in a list intersect
`Rect.collidelistall(list): return indices`

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.

Rect.collidedict

test if one rectangle in a dictionary intersects
`Rect.collidedict(dict): return (key, value)`

Returns the key and value of the first dictionary value that collides with the Rect. If no collisions are found, None is returned.

Rect objects are not hashable and cannot be used as keys in a dictionary, only as values.

Rect.collidedictall

test if all rectangles in a dictionary intersect
`Rect.collidedictall(dict): return [(key, value), ...]`

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.

Rect objects are not hashable and cannot be used as keys in a dictionary, only as values.