diff --git a/alien.py b/alien.py new file mode 100644 index 0000000..a6d4f0e --- /dev/null +++ b/alien.py @@ -0,0 +1,22 @@ +import pygame +from pygame.sprite import Sprite + +class Alien(Sprite): + """A class to represent a single alien in the fleet""" + + def __init__(self, ai_game): + """Init the alien & set it's starting position""" + super().__init__() + self.screen = ai_game.screen + self.screen = ai_game.screen + + # Load the alien image and set it's rect attrib + self.image = pygame.image.load("images/alien.bmp") + self.rect = self.image.get_rect() + + # Start each new alien near the top left of screen + self.rect.x = self.rect.width + self.rect.y = self.rect.height + + # Store the alien's exact horizontal position + self.x = float(self.rect.x) diff --git a/alien_invasion.py b/alien_invasion.py index 01f88e7..9eba583 100644 --- a/alien_invasion.py +++ b/alien_invasion.py @@ -2,6 +2,8 @@ import pygame from settings import Settings from ship import Ship +from bullet import Bullet +from alien import Alien class AlienInvasion: @@ -19,6 +21,10 @@ def __init__(self) -> None: pygame.display.set_caption("Alien Invasion") self.ship = Ship(self) + self.bullets = pygame.sprite.Group() + self.aliens = pygame.sprite.Group() + + self._create_fleet() # Set the background color. self.bg_color = (230, 230, 230) @@ -28,6 +34,7 @@ def run_game(self): while True: self._check_events() self.ship.update() + self._update_bullets() self._update_screen() self.clock.tick(60) @@ -51,6 +58,8 @@ def _check_keydown_events(self, event): self.ship.moving_left = True elif event.key == pygame.K_q: sys.exit() + elif event.key == pygame.K_SPACE: + self._fire_bullet() def _check_keyup_events(self, event): """Respond to key releases""" @@ -59,14 +68,40 @@ def _check_keyup_events(self, event): elif event.key == pygame.K_LEFT: self.ship.moving_left = False + def _fire_bullet(self): + """Create a new bullet and add it to the bullets group""" + if len(self.bullets) < self.settings.bullets_allowed: + new_bullet = Bullet(self) + self.bullets.add(new_bullet) + + def _update_bullets(self): + """Update position of bullets & delete old bullets.""" + # Update bullet positions. + self.bullets.update() + + # Delete bullets at top of screen + for bullet in self.bullets.copy(): + if bullet.rect.bottom <= 0: + self.bullets.remove(bullet) + # print(len(self.bullets)) + + def _create_fleet(self): + """Create the fleet of aliens""" + # Make an alien + alien = Alien(self) + self.aliens.add(alien) + + def _update_screen(self): """Update images on the screen, and flip to the new screen.""" self.screen.fill(self.settings.bg_color) + for bullet in self.bullets.sprites(): + bullet.draw_bullet() self.ship.blitme() + self.aliens.draw(self.screen) pygame.display.flip() - if __name__ == "__main__": # Make a game instance & run the game. ai = AlienInvasion() diff --git a/bullet.py b/bullet.py index 70965ce..69335b3 100644 --- a/bullet.py +++ b/bullet.py @@ -1,5 +1,5 @@ import pygame -from pygame.sprite import _Group, Sprite +from pygame.sprite import Sprite class Bullet(Sprite): """A class to manage bullets fired from the ship""" diff --git a/images/alien.bmp b/images/alien.bmp new file mode 100644 index 0000000..dbe01a0 Binary files /dev/null and b/images/alien.bmp differ diff --git a/settings.py b/settings.py index 541560b..bc8f07e 100644 --- a/settings.py +++ b/settings.py @@ -16,3 +16,4 @@ def __init__(self) -> None: self.bullet_width = 3 self.bullet_height = 15 self.bullet_color = (60, 60, 60) + self.bullets_allowed = 5