Skip to content

Commit

Permalink
Added Play button
Browse files Browse the repository at this point in the history
  • Loading branch information
melkyah committed Sep 12, 2019
1 parent f336972 commit b93748a
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 4 deletions.
31 changes: 31 additions & 0 deletions alien_invasion.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,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 Down Expand Up @@ -36,6 +37,9 @@ def __init__(self):

self._create_fleet()

# Make the Play button
self.play_button = Button(self, "Play")

def run_game(self):
"""This function runs the game."""

Expand Down Expand Up @@ -63,6 +67,28 @@ def _check_events(self):
self.ship.moving_right = False
elif event.key == pygame.K_LEFT:
self.ship.moving_left = False
elif event.type == pygame.MOUSEBUTTONDOWN:
mouse_pos = pygame.mouse.get_pos()
self._check_play_button(mouse_pos)

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.stats.game_active:
# Reset the game statistics.
self.stats.reset_stats()
self.stats.game_active = True

# Get rid of any remaining aliens and bullets.
self.aliens.empty()
self.bullets.empty()

# Create a new fleet and center the ship.
self._create_fleet()
self.ship.center_ship()

# Hide the mouse cursor.
pygame.mouse.set_visible(False)

def _check_keydown_events(self, event):
"""Respond to keypresses."""
Expand Down Expand Up @@ -153,6 +179,7 @@ def _ship_hit(self):
sleep(0.5)
else:
self.stats.game_active = False
pygame.mouse.set_visible(True)

def _create_fleet(self):
"""Create the fleet of aliens."""
Expand Down Expand Up @@ -204,6 +231,10 @@ def _update_screen(self):
bullet.draw_bullet()
self.aliens.draw(self.screen)

# Draw the Play button if the game is inactive.
if not self.stats.game_active:
self.play_button.draw_button()

pygame.display.flip()


Expand Down
40 changes: 40 additions & 0 deletions button.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""
Base class for creating buttons in the game.
"""

import pygame.font


class Button():
"""This class handles the 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 sproperties 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 needs to be prepped 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.button_color)
self.msg_image_rect = self.msg_image.get_rect()
self.msg_image_rect.center = self.rect.center

def draw_button(self):
"""This method draws the button"""
# 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 changes: 3 additions & 2 deletions game_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ def __init__(self, ai_game):
"""Initialize statistics"""
self.settings = ai_game.settings
self.reset_stats()
# Start Alien Invasion in an active state.
self.game_active = True

# Start game in an inactive state.
self.game_active = False

def reset_stats(self):
"""Initialize statistics that can change during the game."""
Expand Down
4 changes: 2 additions & 2 deletions settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def __init__(self):
self.bullets_allowed = 5

# Alien settings
self.alien_speed = 1.0
self.fleet_drop_speed = 10
self.alien_speed = 3.0
self.fleet_drop_speed = 30
# fleet_direction of 1 represents right; -1 represents left
self.fleet_direction = 1

0 comments on commit b93748a

Please sign in to comment.