メニュー

pygame.Rect

四角形の描写情報を保持したpygameのオブジェクトです。
pygame.Rect(left, top, width, height): return Rect
pygame.Rect((left, top), (width, height)): return Rect
pygame.Rect(object): return Rect
  • Rect.copy - Rectオブジェクトのコピーを作成します。
  • Rect.move - 位置を移動させたRectオブジェクトを新規に作成します。
  • Rect.move_ip - Rectオブジェクトの描写位置を移動させます。
  • Rect.inflate - 拡大・縮小したRectオブジェクトを新規に作成します。
  • Rect.inflate_ip - Rectオブジェクトの大きさを拡大・縮小します。
  • Rect.clamp - 別のRectオブジェクトの中に移動させたRectオブジェクトを新規に作成します。
  • Rect.clamp_ip - Rectオブジェクトを、別のRectオブジェクトの中に移動させます。
  • Rect.clip - Rectオブジェクト同士が重なり合っている範囲をRectオブジェクトとして新規に作成し、戻り値として返します。
  • Rect.union - 二つのRectオブジェクトをつなげて新規にRectオブジェクトを作成し、戻り値として返します。
  • Rect.union_ip - 二つのRectオブジェクトをつなげて一つにします。
  • Rect.unionall - 複数のRectオブジェクトをつなげて新規にRectオブジェクトを作成し、戻り値として返します。
  • Rect.unionall_ip - 複数のRectオブジェクトをつなげて一つにします。
  • Rect.fit - Rectオブジェクトの縦横比を保ったままで、サイズの変更や位置の移動を行います。
  • Rect.normalize - サイズにマイナスの値が設定されていた場合に修正を行います。
  • Rect.contains - Rectオブジェクトが別のRectオブジェクトの描写範囲内に収まるか調べます。
  • Rect.collidepoint - 点座標がRectオブジェクトの描写範囲内にあるか調べます。
  • Rect.colliderect - 二つのRectオブジェクトが重なっているか調べます。
  • Rect.collidelist - Rectオブジェクトが、list型に格納された複数のRectオブジェクトのうちのどれか一つと重なり合っているか調べます。
  • Rect.collidelistall - Rectオブジェクトが、list型に格納された複数のRectオブジェクトと重なり合っているか調べます。
  • Rect.collidedict - Rectオブジェクトが、dictionary型に格納された複数のRectオブジェクトのうちのどれか一つと重なり合っているか調べます。
  • Rect.collidedictall - Rectオブジェクトが、複数のRectオブジェクトと重なり合っているか調べます。

PygameではRectオブジェクトを使用して描写する四角範囲を設定したり変更したりできます。Rectオブジェクトはleft、top、width、heightといった引数を組み合わせて設定し、作成することができます。 他にも、RectオブジェクトやRectオブジェクトを持つオブジェクトなどからも作成することができます。

Rect型の値を引数として設定するPygameの関数では、この命令で作成されたRectオブジェクトを引数として使用することができます。この命令を使えば、様々な関数で引数として使用するRectオブジェクトをより早く簡単に作成することができます。

通常はこの項目の命令を使用してRectオブジェクトの位置やサイズを変更しても、情報が変更されたRectオブジェクトを新規に作成するだけで元のRectオブジェクト自体は変更されません。そのためいつくかの命令には"in-place"というバージョンがあり、それを使用すれば元のRectオブジェクト自体を変更することができます。これらの "in-place"命令は語尾に"ip"と付いています。

Rectオブジェクトは複数の属性値を保持しており、それらの値を変更することによって描写する四角形の移動や配置をすることができます。:

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

上記の属性値は全て変更をすることができます。:

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

size値や width値、 height値を変更することで描写する四角形の大きさを変更することができます。;左記以外の属性値を変更した場合は、四角形の大きさは変わらず描写位置が変わります。整数を設定する属性値もありますが、整数のタプル値を設定する属性値もあるので注意してください。

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.

Rectオブジェクトの持つ各座標は全て整数値となります。size値にはマイナスの数を設定することもできますが、マイナスを設定したRectオブジェクトは大抵の処理に置いて不正な値と見なされてしまいます。

PygameのRectオブジェクトには、他の複数のRectオブジェクトとの当たり判定を調べるための処理があります。pythonで使用される大抵のオブジェクトでは、単一範囲の当たり判定しか調べることができません。

Rectオブジェクトで描写される範囲には、右辺と下辺の部分は含まれません。あるRectオブジェクトのbottom値と別のRectオブジェクトのtop値を同じに設定した場合(例えばrect1.bottom=rect2.topといった具合に)、二つのRectオブジェクトは画面で見るとぴったりとくっついている様ですが、実際には重なってはいません。rect1.colliderect(rect2)命令を実行して当たり判定を調べると、falseが戻り値として返されます。

Rectクラスを継承してサブクラスを作成することはできますが、オブジェクトの新規作成を行うような命令では、サブクラスを正確に処理することができません。Rect.copy命令やRect.move命令を使用してRectクラスのサブクラスオブジェクトの移動やコピーなどを行うと、サブクラスではなくRectクラスのオブジェクトが新規に作成されて戻り値として返されます。 This may change. To make subclass awareness work though, subclasses may have to maintain the same constructor signature as 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

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

Rectオブジェクトのコピーを作成します。
Rect.copy(): return Rect

元のRectオブジェクトと同じ位置情報とサイズ情報を持つRectオブジェクトを新規に作成し、戻り値として返します。

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

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


Rect.move

位置を移動させたRectオブジェクトを新規に作成します。
Rect.move(x, y): return Rect

指定した距離分移動させたRectオブジェクトを新規に作成し、戻り値として返します。x引数とy引数には正の値と負の値どちらも設定できます。

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

Rectオブジェクトの描写位置を移動させます。
Rect.move_ip(x, y): return None

Rect.moveと同じRectオブジェクトを移動させる命令ですが、この命令ではオブジェクト自身の位置を直接移動させます。

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

拡大・縮小したRectオブジェクトを新規に作成します。
Rect.inflate(x, y): return Rect

指定した値分拡大・縮小したRectオブジェクトを新規に作成し、戻り値として返します。Rectオブジェクトは図形の中心位置を保った状態で拡大・縮小が行われます。マイナスの値を設定するとその分描写する四角形の大きさは小さくなります。

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

Rectオブジェクトの大きさを拡大・縮小します。
Rect.inflate_ip(x, y): return None

Rect.inflate命令と同じRectオブジェクトの大きさを変更する命令ですが、この命令ではオブジェクト自身の大きさを直接変更させます。

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

別のRectオブジェクトの中に移動させたRectオブジェクトを新規に作成します。
Rect.clamp(Rect): return Rect

引数として設定したRectオブジェクトの描写枠内に移動させたRectオブジェクトを新規に作成し、戻り値として返します。もし指定したRectオブジェクトの枠内に収まりきれない大きさの場合は、枠内の中央に来るよう設置はされますが大きさは変更されません。

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

Rectオブジェクトを、別のRectオブジェクトの中に移動させます。
Rect.clamp_ip(Rect): return None

Rect.clamp命令と同じRectオブジェクトを別のRect内部へ移動させる命令ですが、この命令ではオブジェクト自身の位置を直接移動させます。

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

Rectオブジェクト同士が重なり合っている範囲をRectオブジェクトとして新規に作成し、戻り値として返します。
Rect.clip(Rect): return Rect

引数として設定したRectオブジェクトと重なり合っている範囲をRectオブジェクトとして新規に作成し、戻り値として返します。二つのRectオブジェクトに重なる部分がない場合は、幅が0のRectオブジェクトが戻り値として返ります。

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

二つのRectオブジェクトをつなげて新規にRectオブジェクトを作成し、戻り値として返します。
Rect.union(Rect): return Rect

二つのRectオブジェクトの描写範囲をカバーするRectオブジェクトを新規に作成し、戻り値として返します。新たに作成されたRectオブジェクトの描写範囲には、元となった各Rectオブジェクトではカバーしていない範囲が含まれることもあります。

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

二つのRectオブジェクトをつなげて一つにします。
Rect.union_ip(Rect): return None

Rect.union命令と同じRectオブジェクト同士を一つにつなげる命令ですが、この命令では結合によってオブジェクト自身の大きさを変更させます。

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

複数のRectオブジェクトをつなげて新規にRectオブジェクトを作成し、戻り値として返します。
Rect.unionall(Rect_sequence): return Rect

リスト型で指定した複数のRectオブジェクトをつなげて新規にRectオブジェクトを作成し、戻り値として返します。

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

複数のRectオブジェクトをつなげて一つにします。
Rect.unionall_ip(Rect_sequence): return None

Rect.unionall命令と同じ複数のRectオブジェクト同士を一つにつなげる命令ですが、この命令では結合によってオブジェクト自身の大きさを変更させます。

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

Rectオブジェクトの縦横比を保ったままで、サイズの変更や位置の移動を行います。
Rect.fit(Rect): return Rect

別のRectオブジェクトの描写範囲内に収まりきるように、位置とサイズを変更したRectオブジェクトを新規に作成し、戻り値として返します。元となるRectオブジェクトの縦横比はそのまま保持されるので、新規に作成されるRectオブジェクトは引数として設定したRectオブジェクトよりも横幅か縦幅のどちらかが小さくなります。

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

サイズにマイナスの値が設定されていた場合に修正を行います。
Rect.normalize(): return None

Rectオブジェクトの横幅もしくは縦幅にマイナスの値が設定されていた場合、その値をプラスの値に変更させます。Rectオブジェクトの位置は変わりませんが、反転によって各辺の位置取りが変わってしまいます。

※この命令でマイナス値を反転させたRectオブジェクトをpygame.draw.rect命令で描写すると、マイナス値のままで描写されてしまう場合があるようです。

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

Rectオブジェクトが別のRectオブジェクトの描写範囲内に収まるか調べます。
Rect.contains(Rect): return bool

引数として設定したRectオブジェクトがそのRectオブジェクトの描写範囲内に完全に収まりきる場合は、trueが戻り値として返されます。

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

Returns true when the argument is completely inside the Rect.


Rect.collidepoint

点座標がRectオブジェクトの描写範囲内にあるか調べます。
Rect.collidepoint(x, y): return boolRect.collidepoint((x,y)): return bool

引数として設定した座標がRectオブジェクトの描写範囲内にある場合はtrueが戻り値として返されます。指定した座標がRectオブジェクトの右辺上や下辺上にある場合、その座標はRectオブジェクト内部にあるとは見なされません。

test if a point is inside a rectangle
Rect.collidepoint(x, y): return boolRect.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

二つのRectオブジェクトが重なっているか調べます。
Rect.colliderect(Rect): return bool

二つのRectオブジェクトの一部分が重なっている場合はtrueが戻り値として返されます。(重なり合っているのが上辺と下辺、もしくは左辺と右辺の場合は、Rectオブジェクト同士が重なっているとは見なされません)。

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

Rectオブジェクトが、list型に格納された複数のRectオブジェクトのうちのどれか一つと重なり合っているか調べます。
Rect.collidelist(list): return index

Rectオブジェクトが、複数のRectオブジェクトのうちのどれか一つと重なり合っているか調べます。引数として渡したRectオブジェクトリストの中で、最初に見つかった重なり合うRectオブジェクトのインデックス値が戻り値として返されます。見つからなかった場合は-1が戻り値として返されます。

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

Rectオブジェクトが、list型に格納された複数のRectオブジェクトと重なり合っているか調べます。
Rect.collidelistall(list): return indices

引数として渡したRectオブジェクトlistの中で、Rectオブジェクトと重なり合っているもののインデックス値のlistを戻り値として返します。見つからなかった場合は空のlist値が戻り値として返されます。

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

Rectオブジェクトが、dictionary型に格納された複数のRectオブジェクトのうちのどれか一つと重なり合っているか調べます。
Rect.collidedict(dict): return (key, value)

引数として渡したRectオブジェクトdictionaryの中で、最初に見つかった重なり合うRectオブジェクトのキーワードと設定値を戻り値として返します。見つからなかった場合はNone値が戻り値として返されます。

Rectオブジェクトはハッシュ化できないのでdictionary型のキーワードに使用することができず、設定値に使用します。

補足
list型とdictionary型

Rect.collidelistおよびRect.collidelistallで使用するlist型とは、複数の値を数字で管理する配列です。
rectList=[Rect(0,0,100,100) , Rect(0,100,100,100) , Rect(100,0,100,100) , Rect(100,100,100,100)]
と設定すると
rectList[0]の値はRect(0,0,100,100)、rectList[1]の値はRect(0,100,100,100)、rectList[2]の値はRect(100,0,100,100)、rectList[3]の値はRect(100,100,100,100)となります。

Rect.collidedictおよびRect.collidedictallで使用するdictionary型とは、複数の値をキーワードで管理する配列です。{キーワード:設定値}という形式で記述します。
rectDict={'a':Rect(0,0,100,100) , 'b':Rect(0,100,100,100) , 'c':Rect(100,0,100,100) , 'd':Rect(100,100,100,100)}
と設定すると
rectList['a']の値はRect(0,0,100,100)、rectList['b']の値はRect(0,100,100,100)、rectList['c']の値はRect(100,0,100,100)、rectList['d']の値はRect(100,100,100,100)となります。


またRect.collidedict、Rect.collidedictall命令でdictionary型の値を設定する場合は、キーワードと設定値を同じ値にしないと機能しないようです。
rectDict={(0,0,100,100):Rect(0,0,100,100) , (0,100,100,100):Rect(0,100,100,100) , (100,0,100,100):Rect(100,0,100,100) , (100,100,100,100):Rect(100,100,100,100)}
というように設定してください。
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

Rectオブジェクトが、複数のRectオブジェクトと重なり合っているか調べます。
Rect.collidedictall(dict): return [(key, value), ...]

引数として渡したRectオブジェクトdictionaryの中で、Rectオブジェクトと重なり合っているもののキーワードと設定値のlistを戻り値として返します。 見つからなかった場合は空のlist値が戻り値として返されます。

Rectオブジェクトはハッシュ化できないのでdictionary型のキーワードに使用することができず、設定値に使用します。

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.