pygame object for storing rectangular coordinatesRect(left, top, width, height) -> Rect
Rect((left, top), (width, height)) -> Rect
Rect(object) -> Rect
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:
x,y 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.
New in pygame 1.9.2: The Rect class can be subclassed. Methods such as copy()
and move()
will recognize this and return instances of the subclass. However, the subclass's __init__()
method is not called, and __new__()
is assumed to take no arguments. So these methods should be overridden if any extra attributes need to be copied.
copy() -> Rect
copy the rectangle
Returns a new rectangle having the same position and size as the original.
New in pygame 1.9
move(x, y) -> Rect
moves the rectangle
Returns a new rectangle that is moved by the given offset. The x and y arguments can be any integer value, positive or negative.
move_ip(x, y) -> None
moves the rectangle, in place
Same as the Rect.move()
method, but operates in place.
inflate(x, y) -> Rect
grow or shrink the rectangle size
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.
inflate_ip(x, y) -> None
grow or shrink the rectangle size, in place
Same as the Rect.inflate()
method, but operates in place.
clamp(Rect) -> Rect
moves the rectangle inside another
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.
clamp_ip(Rect) -> None
moves the rectangle inside another, in place
Same as the Rect.clamp()
method, but operates in place.
clip(Rect) -> Rect
crops a rectangle inside another
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.
union(Rect) -> Rect
joins two rectangles into one
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.
union_ip(Rect) -> None
joins two rectangles into one, in place
Same as the Rect.union()
method, but operates in place.
unionall(Rect_sequence) -> Rect
the union of many rectangles
Returns the union of one rectangle with a sequence of many rectangles.
unionall_ip(Rect_sequence) -> None
the union of many rectangles, in place
The same as the Rect.unionall()
method, but operates in place.
fit(Rect) -> Rect
resize and move a rectangle with aspect ratio
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.
normalize() -> None
correct negative sizes
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.
contains(Rect) -> bool
test if one rectangle is inside another
Returns true when the argument is completely inside the Rect.
collidepoint(x, y) -> bool
collidepoint((x,y)) -> bool
test if a point is inside a rectangle
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.
colliderect(Rect) -> bool
test if two rectangles overlap
Returns true if any portion of either rectangle overlap (except the top+bottom or left+right edges).
collidelist(list) -> index
test if one rectangle in a list intersects
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.
collidelistall(list) -> indices
test if all rectangles in a list intersect
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.
collidedict(dict) -> (key, value)
collidedict(dict) -> None
collidedict(dict, use_values=0) -> (key, value)
collidedict(dict, use_values=0) -> None
test if one rectangle in a dictionary intersects
Returns the first key and value pair that intersects with the calling Rect object. If no collisions are found, None
is returned. If use_values
is 0 (default) then the dict's keys will be used in the collision detection, otherwise the dict's values will be used.
Note
Rect objects cannot be used as keys in a dictionary (they are not hashable), so they must be converted to a tuple/list. e.g. rect.collidedict({tuple(key_rect) : value})
collidedictall(dict) -> [(key, value), ...]
collidedictall(dict, use_values=0) -> [(key, value), ...]
test if all rectangles in a dictionary intersect
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 use_values
is 0 (default) then the dict's keys will be used in the collision detection, otherwise the dict's values will be used.
Note
Rect objects cannot be used as keys in a dictionary (they are not hashable), so they must be converted to a tuple/list. e.g. rect.collidedictall({tuple(key_rect) : value})
© Pygame Developers.
Licensed under the GNU LGPL License version 2.1.
https://www.pygame.org/docs/ref/rect.html