-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9da3a8b
commit 6f75632
Showing
6 changed files
with
126 additions
and
10 deletions.
There are no files selected for viewing
Empty file.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,69 @@ | ||
import sys | ||
import pygame | ||
from settings import Settings | ||
from ship import Ship | ||
|
||
|
||
class AlienInvasion: | ||
|
||
"""Parent class to manage game behavior""" | ||
|
||
def __init__(self): | ||
|
||
pygame.init() | ||
|
||
self.screen = pygame.display.set_mode((1200, 800)) | ||
pygame.display.set_caption("Alien Invasion") | ||
self.settings = Settings() | ||
|
||
self.screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN) | ||
self.settings.screen_width = self.screen.get_rect().width | ||
self.settings.screen_height = self.screen.get_rect().height | ||
|
||
self.bg_color = (230, 230, 230) | ||
pygame.display.set_caption("Alien Invasion") | ||
self.ship = Ship(self) | ||
|
||
|
||
def run_game(self): | ||
|
||
while True: | ||
for event in pygame.event.get(): | ||
if event.type == pygame.QUIT: | ||
sys.exit() | ||
|
||
#Update screen | ||
self.screen.fill(self.bg_color) | ||
pygame.display.flip() | ||
self._check_events() | ||
self.ship.update() | ||
self._update_screen() | ||
|
||
|
||
def _check_events(self): | ||
|
||
for event in pygame.event.get(): | ||
if event.type == pygame.QUIT: | ||
sys.exit() | ||
elif event.type == pygame.KEYDOWN: | ||
self._check_keydown_events(event) | ||
elif event.type == pygame.KEYUP: | ||
self._check_keyup_events(event) | ||
|
||
|
||
def _check_keydown_events(self, event): | ||
if event.key == pygame.K_RIGHT: | ||
self.ship.moving_right = True | ||
elif event.key == pygame.K_LEFT: | ||
self.ship.moving_left = True | ||
elif event.key == pygame.K_q: | ||
sys.exit() | ||
|
||
|
||
def _check_keyup_events(self, event): | ||
if event.key == pygame.K_RIGHT: | ||
self.ship.moving_right = False | ||
elif event.key == pygame.K_LEFT: | ||
self.ship.moving_left = False | ||
|
||
|
||
def _update_screen(self): | ||
self.screen.fill(self.settings.bg_color) | ||
self.ship.blitme() | ||
pygame.display.flip() | ||
|
||
|
||
if __name__ == "__main__": | ||
|
||
ai = AlienInvasion() | ||
ai.run_game() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import pygame | ||
from pygame.sprite import Sprite | ||
|
||
|
||
class Bullet(Sprite): | ||
"""Projectiles fired from ship. The Bullet class inherits from Sprite, which we import from the pygame .sprite module. When you use sprites, you can group related elements in your game and act on all the grouped elements at once. To create a bullet instance, __init__() needs the current instance of AlienInvasion, and we call super() to inherit properly from Sprite.""" | ||
|
||
def __init__(self, ai_game): | ||
|
||
super().__init__() | ||
self.screen = ai_game.screen | ||
self.settings = ai_game.settings | ||
self.color = self.settings.bullet_color | ||
|
||
self.rect = pygame.Rect(0, 0, | ||
self.settings.bullet_width, self.settings.bullet_height) | ||
self.rect.midtop = ai_game.ship.rect.midtop | ||
|
||
self.y = float(self.rect.y) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class Settings: | ||
"""Settings for Alien Invasion""" | ||
|
||
def __init__(self): | ||
|
||
self.screen_width = 1200 | ||
self.screen_height = 800 | ||
self.bg_color = (230, 230, 230) | ||
#Ship | ||
self.ship_speed = 1.5 | ||
#Bullets | ||
self.bullet_speed = 1. | ||
self.bullet_width = 3 | ||
self.bullet_height = 15 | ||
self.bullet_color = (60, 60, 60) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import pygame | ||
from os.path import dirname, abspath, join | ||
|
||
#Parent directory | ||
path = dirname(dirname(abspath(__file__))) | ||
|
||
class Ship: | ||
"""The player's ship""" | ||
|
||
def __init__(self, ai_game): | ||
|
||
self.screen = ai_game.screen | ||
self.settings = ai_game.settings | ||
self.screen_rect = ai_game.screen.get_rect() | ||
|
||
self.image = pygame.image.load(join(path, "img", "ship.bmp")) | ||
self.rect = self.image.get_rect() | ||
self.rect.midbottom = self.screen_rect.midbottom | ||
|
||
#Decimal value for ship's horizontal position | ||
self.x = float(self.rect.x) | ||
|
||
# Movement flags | ||
self.moving_right = False | ||
self.moving_left = False | ||
|
||
|
||
def update(self): | ||
|
||
if self.moving_right and self.rect.right < self.screen_rect.right: | ||
self.x += self.settings.ship_speed | ||
if self.moving_left and self.rect.left > 0: | ||
self.x -= self.settings.ship_speed | ||
|
||
self.rect.x = self.x | ||
|
||
|
||
def blitme(self): | ||
"""Draw ship at new loaction""" | ||
self.screen.blit(self.image, self.rect) |