From 4ccedc0473698ae74627a938abfd0881a3efd735 Mon Sep 17 00:00:00 2001 From: oabrivard Date: Sat, 9 Aug 2025 20:03:05 +0200 Subject: [PATCH] Added [Play] button --- alien_invasion.py | 24 ++++++++++++++++++++++-- button.py | 31 +++++++++++++++++++++++++++++++ game_stats.py | 2 -- 3 files changed, 53 insertions(+), 4 deletions(-) create mode 100644 button.py diff --git a/alien_invasion.py b/alien_invasion.py index b4e9392..0486891 100644 --- a/alien_invasion.py +++ b/alien_invasion.py @@ -3,6 +3,7 @@ from time import sleep import pygame from settings import Settings from game_stats import GameStats +from button import Button from ship import Ship from bullet import Bullet from alien import Alien @@ -26,13 +27,15 @@ class AlienInvasion: self.bullets = Group() self.aliens = Group() self._create_fleet() + self.game_active = False + self.play_button = Button(self, "Play") def run_game(self): """Start the main loop for the game.""" while True: # Check for events and update the game state. self._check_events() - if self.stats.game_active: + if self.game_active: # Update the ship, bullets, and aliens. self.ship.update() self._update_bullets() @@ -83,6 +86,9 @@ class AlienInvasion: self._check_keydown_events(event) elif event.type == pygame.KEYUP: self._check_keyup_events(event) + elif event.type == pygame.MOUSEBUTTONDOWN: + mouse_pos = pygame.mouse.get_pos() + self._check_play_button(mouse_pos) def _check_keydown_events(self, event): """Respond to key presses.""" @@ -102,6 +108,18 @@ class AlienInvasion: elif event.key == pygame.K_LEFT: self.ship.moving_left = False + def _check_play_button(self, mouse_pos): + """Start a new game when the player clicks Play.""" + button_clicked = self.play_button.rect.collidepoint(mouse_pos) + if button_clicked and not self.game_active: + self.stats.reset_stats() + self.game_active = True + self.bullets.empty() + self.aliens.empty() + self._create_fleet() + self.ship.center_ship() + pygame.mouse.set_visible(False) + def _fire_bullet(self): """Fire a bullet if the limit is not reached.""" if len(self.bullets) < self.settings.bullets_allowed: @@ -143,7 +161,7 @@ class AlienInvasion: self.ship.center_ship() sleep(0.5) else: - self.stats.game_active = False + self.game_active = False pygame.mouse.set_visible(True) def _update_aliens(self): @@ -162,6 +180,8 @@ class AlienInvasion: bullet.draw_bullet() self.ship.blitme() self.aliens.draw(self.screen) + if not self.game_active: + self.play_button.draw_button() pygame.display.flip() if __name__ == '__main__': diff --git a/button.py b/button.py new file mode 100644 index 0000000..d8d868c --- /dev/null +++ b/button.py @@ -0,0 +1,31 @@ +import pygame.font + +class Button: + def __init__(self, ai_game, msg): + """Initialize button attributes.""" + self.screen = ai_game.screen + self.screen_rect = self.screen.get_rect() + + # Set the dimensions and properties of the button. + self.width, self.height = 200, 50 + self.button_color = (0, 135, 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 needs to be prepped only once. + self._prep_msg(msg) + + def _prep_msg(self, msg): + """Turn the message 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 and then draw message.""" + self.screen.fill(self.button_color, self.rect) + self.screen.blit(self.msg_image, self.msg_image_rect) diff --git a/game_stats.py b/game_stats.py index 2c361ac..bb65119 100644 --- a/game_stats.py +++ b/game_stats.py @@ -5,8 +5,6 @@ class GameStats: """Initialize statistics.""" self.settings = ai_game.settings self.reset_stats() - # Start Alien Invasion in an active state. - self.game_active = True def reset_stats(self): """Initialize statistics that can change during the game."""