ship 对象如何知道在主屏幕表面的底部居中显示?

How does the ship object know to get displayed centred at the bottom of the main screen surface?

这些是相关的源文件:

1。 ship.py

#!/usr/bin/python3

import pygame

class Ship(object):
    """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)

2。 alien_invasion.py

#!/usr/bin/python3

import pygame
from sys import exit
from settings import Settings
from ship import Ship

class AlienInvasion(object):
    """Overall class to manage game assets and behavior."""

    def __init__(self):
        """Initialize the game, and create game resources."""
        pygame.init()
        self.settings = Settings()

        self.screen = pygame.display.set_mode((self.settings.screen_width, self.settings.screen_height))
        pygame.display.set_caption("Alien Invasion")

        self.ship = Ship(self)

    def run_game(self):
        """Start the main loop for the game"""
        while True:
            # Watch for keyboard and mouse events. ** The Event Loop **
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    sys.exit()

            # Redraw the screen during each pass through the loop
            self.screen.fill(self.settings.bg_color)
            self.ship.blitme()

            # Make the most recently drawn screen visible.
            pygame.display.flip()

if __name__ == '__main__':
    # Make a game instance, and run the game.
    ai = AlienInvasion()
    ai.run_game()

运行 alien_invasion.py 产生以下输出... ...这是所需的输出。我只是不明白当 Ship.blitme() 方法采用参数 imagerect(而不是 rect.midbottom).据我了解,这不会在任何地方使用 self.rect.midbottom 属性在定义图像后在屏幕上绘制图像。

TIA!

当您创建飞船 class 的实例时,您使用 self.rect.midbottom = self.screen_rect.midbottom 将其矩形移动到屏幕的底部中间。这也会移动 rect.xrect.y,所以当你用矩形 blit 船时,它已经定位在右边