Skip to content

Commit

Permalink
Modified gitignore
Browse files Browse the repository at this point in the history
  • Loading branch information
0mppula committed Mar 23, 2021
0 parents commit f031cf5
Show file tree
Hide file tree
Showing 24 changed files with 818 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
__pycache__/
/resources/images/img_resources
57 changes: 57 additions & 0 deletions alien.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import pygame
from pygame.sprite import Sprite
from random import randint


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().__init__()
self.screen = screen
self.ai_settings = ai_settings
# Load the alien image and set its rect attribute.
self.image = pygame.image.load(self.get_color())
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 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 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 get_color(self):
""" Set color random color for alien. """
colors = ['./resources/images/alien_blue.bmp',
'./resources/images/alien_red.bmp', './resources/images/alien_green.bmp']
random_index = randint(0, len(colors)-1)
alien_color = colors[random_index]
self.set_alien_points(alien_color)
return alien_color

def set_alien_points(self, color):
""" Determines score of alien based on alien color. """
if color == './resources/images/alien_green.bmp':
self.points = 100
elif color == './resources/images/alien_blue.bmp':
self.points = 150
elif color == './resources/images/alien_red.bmp':
self.points = 200

def blitme(self):
"""Draw the alien at its current location."""
self.screen.blit(self.image, self.rect)
51 changes: 51 additions & 0 deletions alien_invasion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import pygame
from pygame.sprite import Group


from settings import Settings
import game_functions as gf
from game_stats import GameStats
from scoreboard import Scoreboard
from menu import Menu
from ship import Ship


def run_game():
# Initialize game and create a screen object
pygame.init()

ai_settings = Settings()
stats = GameStats(ai_settings)
screen = pygame.display.set_mode(
(ai_settings.screen_width, ai_settings.screen_height))
pygame.display.set_caption('Alien Invasion')
sb = Scoreboard(ai_settings, screen, stats)
# Make the Play button
menu = Menu(ai_settings, screen, stats)

# Make a ship, a group of bullets, and a group of aliens
ship = Ship(ai_settings, screen)
aliens = Group()
bullets = Group()

# Create the fleet of aliens
gf.create_fleet(ai_settings, screen, ship, aliens)

# Start the main loop of the game
while True:

gf.check_events(ai_settings, screen, stats, sb,
menu, ship, aliens, bullets)

if stats.game_active and not stats.game_paused:
ship.update()
gf.update_bullets(ai_settings, screen, stats, sb, menu,
ship, aliens, bullets)
gf.update_aliens(ai_settings, stats, sb, menu,
screen, ship, aliens, bullets)

gf.update_screen(ai_settings, screen, stats, sb, ship,
aliens, bullets, menu)


run_game()
34 changes: 34 additions & 0 deletions bullet.py
Original file line number Diff line number Diff line change
@@ -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 ccurrent position. """
super().__init__()
self.screen = screen

# Create a bullet rect at (0, 0) and 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 the bullet's position as a decimal value
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 value of the bullet
self.y -= self.speed_factor
# Update the rect position
self.rect.y = self.y

def draw_bullet(self):
""" Draw the bullet on the screen. """
pygame.draw.rect(self.screen, self.color, self.rect)
46 changes: 46 additions & 0 deletions button.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import pygame.font


class Button():

def __init__(self, ai_settings, screen, stats):
"""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 = (90, 200, 30)
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
self.rect.top = self.rect.center[1]
# The button message needs to be prepped only once
# self.msg = msg
self.prep_msg(stats)

def prep_text(self, stats):
""" Prepare the text for the menu button based on state of game. """
if not stats.game_active and not stats.game_paused and not stats.game_ended:
self.msg = 'Play!'
elif stats.game_active and stats.game_paused:
self.msg = 'Resume'
elif stats.game_ended:
self.msg = 'Try Again!'

def prep_msg(self, stats):
""" Turn msg into a rendered image and center text on the button. """

self.prep_text(stats)
self.msg_image = self.font.render(self.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, stats):
# Draw blank button and then draw message
self.prep_msg(stats)
self.screen.fill(self.button_color, self.rect)
self.screen.blit(self.msg_image, self.msg_image_rect)
Loading

0 comments on commit f031cf5

Please sign in to comment.