diff --git a/alien.bmp b/alien.bmp new file mode 100644 index 0000000..e91ef54 Binary files /dev/null and b/alien.bmp differ diff --git a/alien.py b/alien.py new file mode 100644 index 0000000..865b4b8 --- /dev/null +++ b/alien.py @@ -0,0 +1,41 @@ +import pygame +from pygame.sprite import Sprite + + +class Alien(Sprite): + """A class to represent a single alien in the fleet.""" + + def __init__(self, ai_settings, screen): + """Initialize the alien, and set its starting position.""" + super(Alien, self).__init__() + self.screen = screen + self.ai_settings = ai_settings + + # Load the alien image, and set its rect attribute. + self.image = pygame.image.load(r'alien.bmp') + self.rect = self.image.get_rect() + + # Start each new alien near the top left of the screen. + self.rect.x = self.rect.width + self.rect.y = self.rect.height + + # Store the alien's exact position. + self.x = float(self.rect.x) + + def check_edges(self): + """Return True if alien is at edge of screen.""" + screen_rect = self.screen.get_rect() + if self.rect.right >= screen_rect.right: + return True + elif self.rect.left <= 0: + return True + + def update(self): + """Move the alien right or left.""" + self.x += (self.ai_settings.alien_speed_factor * + self.ai_settings.fleet_direction) + self.rect.x = self.x + + def blitme(self): + """Draw the alien at its current location.""" + self.screen.blit(self.image, self.rect) \ No newline at end of file diff --git a/alien_invasion.py b/alien_invasion.py new file mode 100644 index 0000000..a6e69bc --- /dev/null +++ b/alien_invasion.py @@ -0,0 +1,54 @@ +import pygame +from pygame.sprite import Group + +from settings import Settings +from game_stats import GameStats +from scoreboard import Scoreboard +from button import Button +from ship import Ship +import game_functions as gf + + +def run_game(): + # Initialize pygame, settings, and screen object. + pygame.init() + ai_settings = Settings() + screen = pygame.display.set_mode( + (ai_settings.screen_width, ai_settings.screen_height)) + pygame.display.set_caption("Alien Invasion") + + # Make the Play button. + play_button = Button(ai_settings, screen, "Play") + + # Create an instance to store game statistics, and a scoreboard. + stats = GameStats(ai_settings) + sb = Scoreboard(ai_settings, screen, stats) + + # Set the background color. + bg_color = (230, 230, 230) + + # Make a ship, a group of bullets, and a group of aliens. + ship = Ship(ai_settings, screen) + bullets = Group() + aliens = Group() + + # Create the fleet of aliens. + gf.create_fleet(ai_settings, screen, ship, aliens) + + # Start the main loop for the game. + while True: + gf.check_events(ai_settings, screen, stats, sb, play_button, ship, + aliens, bullets) + + if stats.game_active: + ship.update() + gf.update_bullets(ai_settings, screen, stats, sb, ship, aliens, + bullets) + gf.update_aliens(ai_settings, screen, stats, sb, ship, aliens, + bullets) + + gf.update_screen(ai_settings, screen, stats, sb, ship, aliens, + bullets, play_button) + + +run_game() \ No newline at end of file diff --git a/bullet.py b/bullet.py new file mode 100644 index 0000000..c26ab2c --- /dev/null +++ b/bullet.py @@ -0,0 +1,34 @@ +import pygame +from pygame.sprite import Sprite + + +class Bullet(Sprite): + """A class to manage bullets fired from the ship.""" + + def __init__(self, ai_settings, screen, ship): + """Create a bullet object, at the ship's current position.""" + super(Bullet, self).__init__() + self.screen = screen + + # Create bullet rect at (0, 0), then set correct position. + self.rect = pygame.Rect(0, 0, ai_settings.bullet_width, + ai_settings.bullet_height) + self.rect.centerx = ship.rect.centerx + self.rect.top = ship.rect.top + + # Store a decimal value for the bullet's position. + self.y = float(self.rect.y) + + self.color = ai_settings.bullet_color + self.speed_factor = ai_settings.bullet_speed_factor + + def update(self): + """Move the bullet up the screen.""" + # Update the decimal position of the bullet. + self.y -= self.speed_factor + # Update the rect position. + self.rect.y = self.y + + def draw_bullet(self): + """Draw the bullet to the screen.""" + pygame.draw.rect(self.screen, self.color, self.rect) \ No newline at end of file diff --git a/button.py b/button.py new file mode 100644 index 0000000..8250882 --- /dev/null +++ b/button.py @@ -0,0 +1,34 @@ +import pygame.font + + +class Button(): + + def __init__(self, ai_settings, screen, msg): + """Initialize button attributes.""" + self.screen = screen + self.screen_rect = screen.get_rect() + + # Set the dimensions and properties of the button. + self.width, self.height = 200, 50 + self.button_color = (0, 255, 0) + self.text_color = (255, 255, 255) + self.font = pygame.font.SysFont(None, 48) + + # Build the button's rect object, and center it. + self.rect = pygame.Rect(0, 0, self.width, self.height) + self.rect.center = self.screen_rect.center + + # The button message only needs to be prepped once. + self.prep_msg(msg) + + def prep_msg(self, msg): + """Turn msg into a rendered image, and center text on the button.""" + self.msg_image = self.font.render(msg, True, self.text_color, + self.button_color) + self.msg_image_rect = self.msg_image.get_rect() + self.msg_image_rect.center = self.rect.center + + def draw_button(self): + # Draw blank button, then draw message. + self.screen.fill(self.button_color, self.rect) + self.screen.blit(self.msg_image, self.msg_image_rect) \ No newline at end of file