Skip to content

Commit

Permalink
reset game after game over
Browse files Browse the repository at this point in the history
  • Loading branch information
ntkfi committed Mar 12, 2024
1 parent 3cd68cd commit 67ada1c
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
20 changes: 18 additions & 2 deletions alien_invasion.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

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
Expand All @@ -31,8 +32,11 @@ def __init__(self):

self._create_fleet()

# Start Alien Invasion in an active state.
self.game_active = True
# Start Alien Invasion in an inactive state.
self.game_active = False

# Make the play button.
self.play_button = Button(self, "草泥马")

def run_game(self):
"""Start the main loop for the game."""
Expand All @@ -56,6 +60,9 @@ def _check_events(self):
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 keypresses."""
Expand All @@ -75,6 +82,11 @@ def _check_keyup_events(self, event):
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."""
if self.play_button.rect.collidepoint(mouse_pos):
self.game_active = True

def _fire_bullet(self):
"""Create a new bullet and add it to the bullets group."""
if len(self.bullets) < self.settings.bullets_allowed:
Expand Down Expand Up @@ -183,6 +195,10 @@ def _update_screen(self):
self.ship.blitme()
self.aliens.draw(self.screen)

# Draw the play button if the game is not active.
if not self.game_active:
self.play_button.draw_button()

pygame.display.flip()

if __name__ == '__main__':
Expand Down
35 changes: 35 additions & 0 deletions button.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import pygame

class Button:
"""A class to build buttons for the game."""

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, 100
self.button_image = pygame.image.load('images/gradient.png')
self.button_image = pygame.transform.scale(self.button_image,
(self.width, self.height))
self.text_color = (255, 255, 255)
self.font = pygame.font.SysFont('microsoftyahei', 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 prepared only 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.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.blit(self.button_image, self.rect)
self.screen.blit(self.msg_image, self.msg_image_rect)
Binary file added images/gradient.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 67ada1c

Please sign in to comment.