Skip to content

Commit

Permalink
up to pg 259
Browse files Browse the repository at this point in the history
  • Loading branch information
mistwire committed Apr 19, 2024
1 parent 8e8f1c3 commit c8ebcc7
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 2 deletions.
22 changes: 22 additions & 0 deletions alien.py
Original file line number Diff line number Diff line change
@@ -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)
37 changes: 36 additions & 1 deletion alien_invasion.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import pygame
from settings import Settings
from ship import Ship
from bullet import Bullet
from alien import Alien


class AlienInvasion:
Expand All @@ -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)
Expand All @@ -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)

Expand All @@ -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"""
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion bullet.py
Original file line number Diff line number Diff line change
@@ -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"""
Expand Down
Binary file added images/alien.bmp
Binary file not shown.
1 change: 1 addition & 0 deletions settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit c8ebcc7

Please sign in to comment.