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.
30 lines
1.0 KiB
Python
30 lines
1.0 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 Alien(Sprite):
|
|
"""A class to represent a single alien in the fleet."""
|
|
def __init__(self, ai_game: 'AlienInvasion') -> None:
|
|
super().__init__()
|
|
self.screen = ai_game.screen
|
|
self.settings = ai_game.settings
|
|
self.image = pygame.image.load("images/alien.bmp")
|
|
self.rect = self.image.get_rect()
|
|
self.rect.x = self.rect.width
|
|
self.rect.y = self.rect.height
|
|
self.x = float(self.rect.x)
|
|
|
|
def check_edges(self) -> bool:
|
|
"""Return True if alien is at edge of screen."""
|
|
screen_rect = self.screen.get_rect()
|
|
return self.rect.right >= screen_rect.right or self.rect.left <= 0
|
|
|
|
def update(self) -> None:
|
|
"""Move the alien right or left."""
|
|
self.x += self.settings.alien_speed * self.settings.fleet_direction
|
|
self.rect.x = int(self.x)
|