Game Development Reference
In-Depth Information
17 - Graphics and Animation
myRect.centery
rectangle.
myRect.width
The int value of the width of the rectangle.
myRect.height
The int value of the height of the rectangle.
myRect.size
A tuple of two ints: (width, height)
myRect.topleft
A tuple of two ints: (left, top)
myRect.topright
A tuple of two ints: (right, top)
myRect.bottomleft
A tuple of two ints: (left, bottom)
myRect.bottomright
A tuple of two ints: (right, bottom)
myRect.midleft
A tuple of two ints: (left, centery)
myRect.midright
A tuple of two ints: (right, centery)
myRect.midtop
A tuple of two ints: (centerx, top)
myRect.midbottom
A tuple of two ints: (centerx, bottom)
The great thing about
Rect
objects is that if you modify any of these variables, all the
other variables will automatically modify themselves as well. For example, if you create a
Rect
object that is 20 pixels wide and 20 pixels high, and has the top left corner at the
coordinates (30, 40), then the X-coordinate of the right side will automatically be set to 50
(because 20 + 30 = 50). However, if you change the
left
attribute with the line
myRect.left = 100
, then Pygame will automatically change the
right
attribute to
120 (because 20 + 100 = 120). Every other attribute for that
Rect
object will also be
updated as well.
The
get_rect()
Methods for
pygame.font.Font
and
pygame.Surface
Objects
Notice that both the
Font
object (stored in the
text
variable) and the
Surface
object
(stored in
windowSurface
variable) both have a method called
get_rect()
.
Technically, these are two different methods. But the programmers of Pygame gave them
the same name because they both do the same thing and return
Rect
objects that represent
the size and position of the
Font
or
Surface
object.
Also, remember that
pygame
is a module that we import, and inside the
pygame
module are the
font
and
surface
modules. Inside
those
modules are the
Font
and
Surface
data types. The Pygame programmers made the modules begin with a lowercase
letter, and the data types begin with an uppercase letter. This makes it easier to distinguish
the data types and the modules that the data types can be found in.
Constructor Functions and the
type()
function.
We create a
pygame.Rect
object by calling a function named
pygame.Rect()
. The
pygame.Rect()
function has the same name as the
pygame.Rect
data type.