Added [Play] button

main
oabrivard 5 months ago
parent c79f8f7fd2
commit 4ccedc0473

@ -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__':

@ -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)

@ -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."""

Loading…
Cancel
Save