game will notice that we’re not working with the exact shape of each game
element. We’ll treat the ship and the screen as rectangles in this class.
We import the pygame module before defining the class. The __init__()
method of Ship takes two parameters: the self reference and a reference to
the current instance of the AlienInvasion class. This will give Ship access to
all the game resources defined in AlienInvasion. We then assign the screen
to an attribute of Ship 1, so we can access it easily in all the methods in this
class. We access the screen’s rect attribute using the get_rect() method and
assign it to self.screen_rect 2. Doing so allows us to place the ship in the
correct location on the screen.
To load the image, we call pygame.image.load() 3 and give it the location
of our ship image. This function returns a surface representing the ship,
which we assign to self.image. When the image is loaded, we call get_rect() to
access the ship surface’s rect attribute so we can later use it to place the ship.
When you’re working with a rect object, you can use the x- and
y-coordinates of the top, bottom, left, and right edges of the rectangle, as
well as the center, to place the object. You can set any of these values to
establish the current position of the rect. When you’re centering a game
element, work with the center, centerx, or centery attributes of a rect. When
you’re working at an edge of the screen, work with the top, bottom, left, or
right attributes. There are also attributes that combine these properties,
such as midbottom, midtop, midleft, and midright. When you’re adjusting the
horizontal or vertical placement of the rect, you can just use the x and y
attributes, which are the x- and y-coordinates of its top-left corner. These
attributes spare you from having to do calculations that game developers
formerly had to do manually, and you’ll use them often.
NOTE In Pygame, the origin (0, 0) is at the top-left corner of the screen, and coordinates
increase as you go down and to the right. On a 1200×800 screen, the origin is at the
top-left corner, and the bottom-right corner has the coordinates (1200, 800). These
coordinates refer to the game window, not the physical screen.
We’ll position the ship at the bottom center of the screen. To do so,
make the value of self.rect.midbottom match the midbottom attribute of the
screen’s rect 4. Pygame uses these rect attributes to position the ship image
so it’s centered horizontally and aligned with the bottom of the screen.
Finally, we define the blitme() method 5, which draws the image to the
screen at the position specified by self.rect.
Drawing the Ship to the Screen
Now let’s update alien_invasion.py so it creates a ship and calls the ship’s
blitme() method:
alien --snip--
_invasion.py from settings import Settings
from ship import Ship
A Ship That Fires Bullets 235