Class | Rubygame::Rect
In: | lib/rubygame/rect.rb lib/rubygame/old_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

new(*argv)

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.

new(*argv)

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.

new_from_object(object)

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()

new_from_object(object)

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=

bottom()

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

bottom()

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

bottom=(b)

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

bottom=(b)

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

bottomleft()

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

bottomleft()

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

bottomleft=(bottomleft)

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

bottomleft=(bottomleft)

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

bottomright()

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

bottomright()

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

bottomright=(bottomright)

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

bottomright=(bottomright)

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=

center()

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

center()

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

center=(center)

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

center=(center)

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

centerx()

Return the x coordinate of the center of the Rect

centerx()

Return the x coordinate of the center of the Rect

centerx=(x)

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

centerx=(x)

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

centery()

Return the y coordinate of the center of the Rect

centery()

Return the y coordinate of the center of the Rect

centery=(y)

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

centery=(y)

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

clamp(rect)

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

clamp(rect)

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

clamp!(rect)

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.

clamp!(rect)

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.

clip(rect)

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

clip(rect)

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

clip!(rect)

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.

clip!(rect)

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.

collide_array(array_rects)

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.

collide_array(array_rects)

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.

collide_array_all(array_rects)

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.

collide_array_all(array_rects)

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.

collide_hash(hash_rects)

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.

collide_hash(hash_rects)

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.

collide_hash_all(hash_rects)

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.

collide_hash_all(hash_rects)

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.

collide_point?(x,y)

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

collide_point?(x,y)

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

collide_rect?(rect)

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

collide_rect?(rect)

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

contain?(rect)

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

contain?(rect)

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=

h()

Returns self.at(3)

h()

Returns self.at(3)

h=(val)

Sets self[3] to `val`

h=(val)

Sets self[3] to `val`

height()

Alias for h

height()

Alias for h

height=(val)

Alias for h=

height=(val)

Alias for h=

inflate(x,y)

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

inflate(x,y)

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

inflate!(x,y)

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.

inflate!(x,y)

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.

inspect()

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

inspect()

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=

midbottom()

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

midbottom()

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

midbottom=(midbottom)

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

midbottom=(midbottom)

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

midleft()

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

midleft()

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

midleft=(midleft)

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

midleft=(midleft)

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

midright()

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

midright()

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

midright=(midright)

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

midright=(midright)

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

midtop()

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

midtop()

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

midtop=(midtop)

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

midtop=(midtop)

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=

move(x,y)

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

move(x,y)

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

move!(x,y)

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.

move!(x,y)

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=

normalize()

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

normalize()

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

normalize!()

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.

normalize!()

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=

right()

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

right()

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

right=(r)

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

right=(r)

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

size()

Return the width and height of the Rect.

size()

Return the width and height of the Rect.

size=(size)

Set the width and height of the Rect.

size=(size)

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=

to_s()

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

to_s()

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=

topleft()

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

topleft()

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

topleft=(topleft)

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

topleft=(topleft)

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

topright()

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

topright()

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

topright=(topright)

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

topright=(topright)

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=

union(rect)

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

union(rect)

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

union!(rect)

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.

union!(rect)

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.

union_all(array_rects)

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

union_all(array_rects)

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

union_all!(array_rects)

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

union_all!(array_rects)

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

w()

Returns self.at(2)

w()

Returns self.at(2)

w=(val)

Sets self[2] to `val`

w=(val)

Sets self[2] to `val`

width()

Alias for w

width()

Alias for w

width=(val)

Alias for w=

width=(val)

Alias for w=

x()

Returns self.at(0)

x()

Returns self.at(0)

x=(val)

Sets self[0] to `val`

x=(val)

Sets self[0] to `val`

y()

Returns self.at(1)

y()

Returns self.at(1)

y=(val)

Sets self[1] to `val`

y=(val)

Sets self[1] to `val`

[Validate]