Reputation: 11
So I am working through No Starch Presses' Python Crash Course 2e. I have finally made it to the part where you create the Alien Invasion game. I am understanding everything up until these specific two lines of code.
There is a class called alien Invasion that holds a screen rect that will be passed to this class in ai_game.
\\
import pygame
class Ship: """A class to manage the ship"""
def __init__(self, ai_game):
"""Initialize the ship and set its starting position"""
self.screen = ai_game.screen
self.screen_rect = ai_game.screen.get_rect()
#load the ship image and get its rect.
self.image = pygame.image.load('images/ship.bmp')
self.rect = self.image.get_rect()
#Start each new ship at the bottom center of the screen
self.rect.midbottom = self.screen_rect.midbottom
def blitme(self):
"""Draw the ship at its current location"""
self.screen.blit(self.image, self.rect)
\
The two lines I am not understanding are below. What is happening under the surface? Of course I know what the author says they are doing but I'm trying to understand how its doing this and not just what it is doing, so I may understand the rules and logic and not just accept the rules and logic on face value.
\\
self.rect.midbottom = self.screen_rect.midbottom
def blitme(self):
"""Draw the ship at its current location"""
self.screen.blit(self.image, self.rect)
\\
self.rect
represents an object in memory that stores data about a rectangle object on the screen, correct? My main question is the .midbottom
following self.rect a attribute of the rect object? So in the first statement is the .midbottom
attribute being set to the .midbottom
attribute of the screen rectangle? While I was learning python as well it says be careful about setting values equal to one another in this way because changing one might change the other as well after you see them equal. Moving on, so okay so if that's true, the ship objects .midbottom
attrribute is being set to the screens .midbottom
attribute, when the blit() method is called how does blit() know where to position the ship? I never told it where to postion the ship. Does the blitmethod have default behavior that set the ship to the only known postition? I have read that by default everything is postioned at (0,0) so if you supply the ship object with data it will automatically place the ship where the user lasts entered data.
Upvotes: 1
Views: 373
Reputation: 210889
A Rect object has exactly 4 attributes x
, y
, width
, height
. However there are many virtual attributes. If you set a virtual attribute under the hood, the attributes x
, y
, width
, height
are changed.
See pygame.Rect
:
The Rect object has several virtual attributes which can be used to move and align the Rect:
x,y 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)
The position is stored in the rect
attribute. See blit
:
Draws a source Surface onto this Surface. The draw can be positioned with the dest argument. The dest argument can either be a pair of coordinates representing the position of the upper left corner of the blit or a Rect, where the upper left corner of the rectangle will be used as the position for the blit.
First a pygame.Rect
object is created with the size of the Surface:
self.rect = self.image.get_rect()
Then the (middle bottom) position of the rectangle is set.
self.rect.midbottom = self.screen_rect.midbottom
This instruction changes the x and y coordinates so that the middle bottom point of the rectangle is at the middle bottom point of the window. This instruction will perfectly align the rectangle at the bottom center of the window.
Finally, the Surface is drawn at the position which is stored in the Rect:
self.screen.blit(self.image, self.rect)
Upvotes: 2