You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
41 lines
1.3 KiB
Python
41 lines
1.3 KiB
Python
from __future__ import annotations
|
|
import pygame
|
|
from pygame.sprite import Sprite
|
|
from typing import TYPE_CHECKING
|
|
|
|
if TYPE_CHECKING:
|
|
from alien_invasion import AlienInvasion
|
|
|
|
class Ship(Sprite):
|
|
"""A class to manage the ship."""
|
|
|
|
def __init__(self, ai_game: 'AlienInvasion') -> None:
|
|
"""Initialize the ship and set its starting position."""
|
|
super().__init__()
|
|
self.screen = ai_game.screen
|
|
self.settings = ai_game.settings
|
|
self.image = pygame.image.load('images/ship.bmp')
|
|
self.rect = self.image.get_rect()
|
|
self.rect.midbottom = self.screen.get_rect().midbottom
|
|
self.x = float(self.rect.x)
|
|
self.moving_right = False
|
|
self.moving_left = False
|
|
|
|
def update(self) -> None:
|
|
"""Update the ship's position."""
|
|
# Update the ship's x value, not the rect.
|
|
if self.moving_right and self.rect.right < self.screen.get_rect().right:
|
|
self.x += self.settings.ship_speed
|
|
if self.moving_left and self.rect.left > 0:
|
|
self.x -= self.settings.ship_speed
|
|
self.rect.x = int(self.x)
|
|
|
|
def blitme(self) -> None:
|
|
"""Draw the ship at its current location."""
|
|
self.screen.blit(self.image, self.rect)
|
|
|
|
def center_ship(self) -> None:
|
|
"""Center the ship on the screen."""
|
|
self.rect.midbottom = self.screen.get_rect().midbottom
|
|
self.x = float(self.rect.x)
|