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 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オブジェクトと同じ位置情報とサイズ情報を持つRectオブジェクトを新規に作成し、戻り値として返します。
Returns a new rectangle having the same position and size as the orginal.
指定した距離分移動させたRectオブジェクトを新規に作成し、戻り値として返します。x引数とy引数には正の値と負の値どちらも設定できます。
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と同じRectオブジェクトを移動させる命令ですが、この命令ではオブジェクト自身の位置を直接移動させます。
Same as the Rect.move - moves the rectangle method, but operates in place.
指定した値分拡大・縮小したRectオブジェクトを新規に作成し、戻り値として返します。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命令と同じRectオブジェクトの大きさを変更する命令ですが、この命令ではオブジェクト自身の大きさを直接変更させます。
Same as the Rect.inflate - grow or shrink the rectangle size method, but operates in place.
引数として設定したRectオブジェクトの描写枠内に移動させたRectオブジェクトを新規に作成し、戻り値として返します。もし指定した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命令と同じRectオブジェクトを別のRect内部へ移動させる命令ですが、この命令ではオブジェクト自身の位置を直接移動させます。
Same as the Rect.clamp - moves the rectangle inside another method, but operates in place.
引数として設定したRectオブジェクトと重なり合っている範囲をRectオブジェクトとして新規に作成し、戻り値として返します。二つのRectオブジェクトに重なる部分がない場合は、幅が0の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オブジェクトの描写範囲をカバーするRectオブジェクトを新規に作成し、戻り値として返します。新たに作成されたRectオブジェクトの描写範囲には、元となった各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命令と同じRectオブジェクト同士を一つにつなげる命令ですが、この命令では結合によってオブジェクト自身の大きさを変更させます。
Same as the Rect.union - joins two rectangles into one method, but operates in place.
リスト型で指定した複数のRectオブジェクトをつなげて新規にRectオブジェクトを作成し、戻り値として返します。
Returns the union of one rectangle with a sequence of many rectangles.
Rect.unionall命令と同じ複数のRectオブジェクト同士を一つにつなげる命令ですが、この命令では結合によってオブジェクト自身の大きさを変更させます。
The same as the Rect.unionall - the union of many rectangles method, but operates in place.
別のRectオブジェクトの描写範囲内に収まりきるように、位置とサイズを変更したRectオブジェクトを新規に作成し、戻り値として返します。元となるRectオブジェクトの縦横比はそのまま保持されるので、新規に作成されるRectオブジェクトは引数として設定した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オブジェクトの横幅もしくは縦幅にマイナスの値が設定されていた場合、その値をプラスの値に変更させます。Rectオブジェクトの位置は変わりませんが、反転によって各辺の位置取りが変わってしまいます。
※この命令でマイナス値を反転させたRectオブジェクトをpygame.draw.rect命令で描写すると、マイナス値のままで描写されてしまう場合があるようです。
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オブジェクトがそのRectオブジェクトの描写範囲内に完全に収まりきる場合は、trueが戻り値として返されます。
Returns true when the argument is completely inside the Rect.
引数として設定した座標がRectオブジェクトの描写範囲内にある場合はtrueが戻り値として返されます。指定した座標がRectオブジェクトの右辺上や下辺上にある場合、その座標はRectオブジェクト内部にあるとは見なされません。
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オブジェクトの一部分が重なっている場合はtrueが戻り値として返されます。(重なり合っているのが上辺と下辺、もしくは左辺と右辺の場合は、Rectオブジェクト同士が重なっているとは見なされません)。
Returns true if any portion of either rectangle overlap (except the top+bottom or left+right edges).
Rectオブジェクトが、複数のRectオブジェクトのうちのどれか一つと重なり合っているか調べます。引数として渡したRectオブジェクトリストの中で、最初に見つかった重なり合うRectオブジェクトのインデックス値が戻り値として返されます。見つからなかった場合は-1が戻り値として返されます。
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オブジェクトlistの中で、Rectオブジェクトと重なり合っているもののインデックス値のlistを戻り値として返します。見つからなかった場合は空のlist値が戻り値として返されます。
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オブジェクト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)}
というように設定してください。
|
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オブジェクトdictionaryの中で、Rectオブジェクトと重なり合っているもののキーワードと設定値のlistを戻り値として返します。 見つからなかった場合は空のlist値が戻り値として返されます。
Rectオブジェクトはハッシュ化できないのでdictionary型のキーワードに使用することができず、設定値に使用します。
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.