Class Rubygame::Rect
In: lib/rubygame/rect.rb
lib/rubygame/rect.rb
Parent: Array

A Rect is a representation of a rectangle, with four core attributes (x offset, y offset, width, and height) and a variety of functions for manipulating and accessing these attributes.

Like all coordinates in Rubygame (and its base library, SDL), x and y offsets are measured from the top-left corner of the screen, with greater y offsets being lower. Thus, specifying the x and y offsets of the Rect is equivalent to setting the location of its top-left corner.

In Rubygame, Rects are used for collision detection and describing the area of a Surface to operate on.

Methods

b   b   b=   b=   bl   bl   bl=   bl=   bottom   bottom   bottom=   bottom=   bottomleft   bottomleft   bottomleft=   bottomleft=   bottomright   bottomright   bottomright=   bottomright=   br   br   br=   br=   c   c   c=   c=   center   center   center=   center=   centerx   centerx   centerx=   centerx=   centery   centery   centery=   centery=   clamp   clamp   clamp!   clamp!   clip   clip   clip!   clip!   collide_array   collide_array   collide_array_all   collide_array_all   collide_hash   collide_hash   collide_hash_all   collide_hash_all   collide_point?   collide_point?   collide_rect?   collide_rect?   contain?   contain?   cx   cx   cx=   cx=   cy   cy   cy=   cy=   h   h   h=   h=   height   height   height=   height=   inflate   inflate   inflate!   inflate!   inspect   inspect   l   l   l=   l=   left   left   left=   left=   mb   mb   mb=   mb=   midbottom   midbottom   midbottom=   midbottom=   midleft   midleft   midleft=   midleft=   midright   midright   midright=   midright=   midtop   midtop   midtop=   midtop=   ml   ml   ml=   ml=   move   move   move!   move!   mr   mr   mr=   mr=   mt   mt   mt=   mt=   new   new   new_from_object   new_from_object   normalize   normalize   normalize!   normalize!   r   r   r=   r=   right   right   right=   right=   size   size   size=   size=   t   t   t=   t=   tl   tl   tl=   tl=   to_s   to_s   top   top   top=   top=   topleft   topleft   topleft=   topleft=   topright   topright   topright=   topright=   tr   tr   tr=   tr=   union   union   union!   union!   union_all   union_all   union_all!   union_all!   w   w   w=   w=   width   width   width=   width=   x   x   x=   x=   y   y   y=   y=  

Public Class methods

Create a new Rect, attempting to extract its own information from the given arguments. The arguments must fall into one of these cases:

  - 4 integers +(x, y, w, h)+.
  - 1 Rect or Array containing 4 integers +([x, y, w, h])+.
  - 2 Arrays containing 2 integers each +([x,y], [w,h])+.
  - 1 object with a +rect+ attribute which is a valid Rect object.

All rect core attributes (x,y,w,h) must be integers.

Create a new Rect, attempting to extract its own information from the given arguments. The arguments must fall into one of these cases:

  - 4 integers +(x, y, w, h)+.
  - 1 Rect or Array containing 4 integers +([x, y, w, h])+.
  - 2 Arrays containing 2 integers each +([x,y], [w,h])+.
  - 1 object with a +rect+ attribute which is a valid Rect object.

All rect core attributes (x,y,w,h) must be integers.

Extract or generate a Rect from the given object, if possible, using the following process:

 1. If it's a Rect already, return a duplicate Rect.
 2. Elsif it's an Array with at least 4 values, make a Rect from it.
 3. Elsif it has a +rect+ attribute., perform (1) and (2) on that.
 4. Otherwise, raise TypeError.

See also Surface#make_rect()

Extract or generate a Rect from the given object, if possible, using the following process:

 1. If it's a Rect already, return a duplicate Rect.
 2. Elsif it's an Array with at least 4 values, make a Rect from it.
 3. Elsif it has a +rect+ attribute., perform (1) and (2) on that.
 4. Otherwise, raise TypeError.

See also Surface#make_rect()

Public Instance methods

b()

Alias for bottom

b()

Alias for bottom

b=(b)

Alias for bottom=

b=(b)

Alias for bottom=

bl()

Alias for bottomleft

bl()

Alias for bottomleft

bl=(bottomleft)

Alias for bottomleft=

bl=(bottomleft)

Alias for bottomleft=

Return the y coordinate of the bottom side of the Rect.

Return the y coordinate of the bottom side of the Rect.

Set the y coordinate of the bottom side of the Rect by translating the Rect (adjusting the y offset).

Set the y coordinate of the bottom side of the Rect by translating the Rect (adjusting the y offset).

Return the x and y coordinates of the bottom-left corner of the Rect

Return the x and y coordinates of the bottom-left corner of the Rect

Set the x and y coordinates of the bottom-left corner of the Rect by translating the Rect (adjusting the x and y offsets).

Set the x and y coordinates of the bottom-left corner of the Rect by translating the Rect (adjusting the x and y offsets).

Return the x and y coordinates of the bottom-right corner of the Rect

Return the x and y coordinates of the bottom-right corner of the Rect

Set the x and y coordinates of the bottom-right corner of the Rect by translating the Rect (adjusting the x and y offsets).

Set the x and y coordinates of the bottom-right corner of the Rect by translating the Rect (adjusting the x and y offsets).

br()

Alias for bottomright

br()

Alias for bottomright

br=(bottomright)

Alias for bottomright=

br=(bottomright)

Alias for bottomright=

c()

Alias for center

c()

Alias for center

c=(center)

Alias for center=

c=(center)

Alias for center=

Return the x and y coordinates of the center of the Rect.

Return the x and y coordinates of the center of the Rect.

Set the x and y coordinates of the center of the Rect by translating the Rect (adjusting the x and y offsets).

Set the x and y coordinates of the center of the Rect by translating the Rect (adjusting the x and y offsets).

Return the x coordinate of the center of the Rect

Return the x coordinate of the center of the Rect

Set the x coordinate of the center of the Rect by translating the Rect (adjusting the x offset).

Set the x coordinate of the center of the Rect by translating the Rect (adjusting the x offset).

Return the y coordinate of the center of the Rect

Return the y coordinate of the center of the Rect

Set the y coordinate of the center of the Rect by translating the Rect (adjusting the y offset).

Set the y coordinate of the center of the Rect by translating the Rect (adjusting the y offset).

As clamp!, but the original caller is not changed.

As clamp!, but the original caller is not changed.

Translate the calling Rect to be entirely inside the given Rect. If the caller is too large along either axis to fit in the given rect, it is centered with respect to the given rect, along that axis.

Translate the calling Rect to be entirely inside the given Rect. If the caller is too large along either axis to fit in the given rect, it is centered with respect to the given rect, along that axis.

As clip!, but the original caller is not changed.

As clip!, but the original caller is not changed.

Crop the calling Rect to be entirely inside the given Rect. If the caller does not intersect the given Rect at all, its width and height are set to zero, but its x and y offsets are not changed.

As a side effect, the Rect is normalized.

Crop the calling Rect to be entirely inside the given Rect. If the caller does not intersect the given Rect at all, its width and height are set to zero, but its x and y offsets are not changed.

As a side effect, the Rect is normalized.

Iterate through all elements in the given Array, and return the index of the first element which is a Rect that collides with the caller.

Iterate through all elements in the given Array, and return the index of the first element which is a Rect that collides with the caller.

Iterate through all elements in the given Array, and return an Array containing the indices of every element that is a Rect that collides with the caller.

Iterate through all elements in the given Array, and return an Array containing the indices of every element that is a Rect that collides with the caller.

Iterate through all key/value pairs in the given hash table, and return the first pair whose value is a Rect that collides with the caller.

Because a hash table is unordered, you should not expect any particular Rect to be returned first.

Iterate through all key/value pairs in the given hash table, and return the first pair whose value is a Rect that collides with the caller.

Because a hash table is unordered, you should not expect any particular Rect to be returned first.

Iterate through all key/value pairs in the given hash table, and return an Array of every pair whose value is a Rect that collides the caller.

Because a hash table is unordered, you should not expect the returned pairs to be in any particular order.

Iterate through all key/value pairs in the given hash table, and return an Array of every pair whose value is a Rect that collides the caller.

Because a hash table is unordered, you should not expect the returned pairs to be in any particular order.

True if the point is inside (including on the border) of the caller. If you have Array of coordinates, you can use collide_point?(*coords).

True if the point is inside (including on the border) of the caller. If you have Array of coordinates, you can use collide_point?(*coords).

True if the caller and the given Rect overlap (or touch) at all.

True if the caller and the given Rect overlap (or touch) at all.

True if the given Rect is totally within the caller. Borders may overlap.

True if the given Rect is totally within the caller. Borders may overlap.

cx()

Alias for centerx

cx()

Alias for centerx

cx=(x)

Alias for centerx=

cx=(x)

Alias for centerx=

cy()

Alias for centery

cy()

Alias for centery

cy=(y)

Alias for centery=

cy=(y)

Alias for centery=

Returns self.at(3)

Returns self.at(3)

Sets self[3] to val

Sets self[3] to val

height()

Alias for h

height()

Alias for h

height=(val)

Alias for h=

height=(val)

Alias for h=

As inflate!, but the original caller is not changed.

As inflate!, but the original caller is not changed.

Increase the Rect‘s size is the x and y directions, while keeping the same center point. For best results, expand by an even number. X and y inflation can be given as an Array or as separate values.

Increase the Rect‘s size is the x and y directions, while keeping the same center point. For best results, expand by an even number. X and y inflation can be given as an Array or as separate values.

Print the Rect in the form "+#<Rect:id [x,y,w,h]>+"

Print the Rect in the form "+#<Rect:id [x,y,w,h]>+"

l()

Alias for x

l()

Alias for x

l=(val)

Alias for x=

l=(val)

Alias for x=

left()

Alias for x

left()

Alias for x

left=(val)

Alias for x=

left=(val)

Alias for x=

mb()

Alias for midbottom

mb()

Alias for midbottom

mb=(midbottom)

Alias for midbottom=

mb=(midbottom)

Alias for midbottom=

Return the x and y coordinates of the midpoint on the left side of the Rect.

Return the x and y coordinates of the midpoint on the left side of the Rect.

Set the x and y coordinates of the midpoint on the bottom side of the Rect by translating the Rect (adjusting the x and y offsets).

Set the x and y coordinates of the midpoint on the bottom side of the Rect by translating the Rect (adjusting the x and y offsets).

Return the x and y coordinates of the midpoint on the left side of the Rect.

Return the x and y coordinates of the midpoint on the left side of the Rect.

Set the x and y coordinates of the midpoint on the left side of the Rect by translating the Rect (adjusting the x and y offsets).

Set the x and y coordinates of the midpoint on the left side of the Rect by translating the Rect (adjusting the x and y offsets).

Return the x and y coordinates of the midpoint on the left side of the Rect.

Return the x and y coordinates of the midpoint on the left side of the Rect.

Set the x and y coordinates of the midpoint on the right side of the Rect by translating the Rect (adjusting the x and y offsets).

Set the x and y coordinates of the midpoint on the right side of the Rect by translating the Rect (adjusting the x and y offsets).

Return the x and y coordinates of the midpoint on the left side of the Rect.

Return the x and y coordinates of the midpoint on the left side of the Rect.

Set the x and y coordinates of the midpoint on the top side of the Rect by translating the Rect (adjusting the x and y offsets).

Set the x and y coordinates of the midpoint on the top side of the Rect by translating the Rect (adjusting the x and y offsets).

ml()

Alias for midleft

ml()

Alias for midleft

ml=(midleft)

Alias for midleft=

ml=(midleft)

Alias for midleft=

As move!, but the original caller is not changed.

As move!, but the original caller is not changed.

Translate the Rect by the given amounts in the x and y directions. Positive values are rightward for x and downward for y. X and y movement can be given as an Array or as separate values.

Translate the Rect by the given amounts in the x and y directions. Positive values are rightward for x and downward for y. X and y movement can be given as an Array or as separate values.

mr()

Alias for midright

mr()

Alias for midright

mr=(midright)

Alias for midright=

mr=(midright)

Alias for midright=

mt()

Alias for midtop

mt()

Alias for midtop

mt=(midtop)

Alias for midtop=

mt=(midtop)

Alias for midtop=

As normalize!, but the original caller is not changed.

As normalize!, but the original caller is not changed.

Fix Rects that have negative width or height, without changing the area it represents. Has no effect on Rects with non-negative width and height. Some Rect methods will automatically normalize the Rect.

Fix Rects that have negative width or height, without changing the area it represents. Has no effect on Rects with non-negative width and height. Some Rect methods will automatically normalize the Rect.

r()

Alias for right

r()

Alias for right

r=(r)

Alias for right=

r=(r)

Alias for right=

Return the x coordinate of the right side of the Rect.

Return the x coordinate of the right side of the Rect.

Set the x coordinate of the right side of the Rect by translating the Rect (adjusting the x offset).

Set the x coordinate of the right side of the Rect by translating the Rect (adjusting the x offset).

Return the width and height of the Rect.

Return the width and height of the Rect.

Set the width and height of the Rect.

Set the width and height of the Rect.

t()

Alias for y

t()

Alias for y

t=(val)

Alias for y=

t=(val)

Alias for y=

tl()

Alias for topleft

tl()

Alias for topleft

tl=(topleft)

Alias for topleft=

tl=(topleft)

Alias for topleft=

Print the Rect in the form "+#<Rect [x,y,w,h]>+"

Print the Rect in the form "+#<Rect [x,y,w,h]>+"

top()

Alias for y

top()

Alias for y

top=(val)

Alias for y=

top=(val)

Alias for y=

Return the x and y coordinates of the top-left corner of the Rect

Return the x and y coordinates of the top-left corner of the Rect

Set the x and y coordinates of the top-left corner of the Rect by translating the Rect (adjusting the x and y offsets).

Set the x and y coordinates of the top-left corner of the Rect by translating the Rect (adjusting the x and y offsets).

Return the x and y coordinates of the top-right corner of the Rect

Return the x and y coordinates of the top-right corner of the Rect

Set the x and y coordinates of the top-right corner of the Rect by translating the Rect (adjusting the x and y offsets).

Set the x and y coordinates of the top-right corner of the Rect by translating the Rect (adjusting the x and y offsets).

tr()

Alias for topright

tr()

Alias for topright

tr=(topright)

Alias for topright=

tr=(topright)

Alias for topright=

As union!, but the original caller is not changed.

As union!, but the original caller is not changed.

Expand the caller to also cover the given Rect. The Rect is still a rectangle, so it may also cover areas that neither of the original Rects did, for example areas between the two Rects.

Expand the caller to also cover the given Rect. The Rect is still a rectangle, so it may also cover areas that neither of the original Rects did, for example areas between the two Rects.

As union_all!, but the original caller is not changed.

As union_all!, but the original caller is not changed.

Expand the caller to cover all of the given Rects. See also union!

Expand the caller to cover all of the given Rects. See also union!

Returns self.at(2)

Returns self.at(2)

Sets self[2] to val

Sets self[2] to val

width()

Alias for w

width()

Alias for w

width=(val)

Alias for w=

width=(val)

Alias for w=

Returns self.at(0)

Returns self.at(0)

Sets self[0] to val

Sets self[0] to val

Returns self.at(1)

Returns self.at(1)

Sets self[1] to val

Sets self[1] to val

[Validate]