Skip to content

Commit

Permalink
scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
konstanzer committed Mar 13, 2022
1 parent 6f75632 commit 468c81f
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 6 deletions.
Binary file added img/alien.bmp
Binary file not shown.
22 changes: 22 additions & 0 deletions src/alien.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import pygame
from pygame.sprite import Sprite
from os.path import dirname, abspath, join
path = dirname(dirname(abspath(__file__)))

class Alien(Sprite):
"""A single alien"""

def __init__(self, ai_game):

super().__init__()
self.screen = ai_game.screen

self.image = pygame.image.load(join(path, "img", "alien.bmp"))
self.rect = self.image.get_rect()

#Start position
self.rect.x = self.rect.width
self.rect.y = self.rect.height

#Decimal value for horizontal position
self.x = float(self.rect.x)
61 changes: 61 additions & 0 deletions src/alieninvasion.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import pygame
from settings import Settings
from ship import Ship
from bullet import Bullet
from alien import Alien


class AlienInvasion:
Expand All @@ -19,14 +21,46 @@ def __init__(self):

self.bg_color = (230, 230, 230)
pygame.display.set_caption("Alien Invasion")

self.ship = Ship(self)
self.bullets = pygame.sprite.Group()
self.aliens = pygame.sprite.Group()

self._create_fleet()


def _create_fleet(self):

alien = Alien(self)
alien_width, alien_height = alien.rect.size
available_space_x = self.settings.screen_width - (2*alien_width)
n_aliens_x = available_space_x // (2*alien_width)

ship_height = self.ship.rect.height
available_space_y = self.settings.screen_height - 3*alien_height - ship_height
n_rows = available_space_y // (2*alien_height)

for row in range(n_rows):
for col in range(n_aliens_x):
self._create_alien(col, row)


def _create_alien(self, col, row):

alien = Alien(self)
alien_width, alien_height = alien.rect.size
alien.x = alien_width + 2*alien_width*col
alien.rect.x = alien.x
alien.rect.y = alien_height + 2*alien_height*row
self.aliens.add(alien)


def run_game(self):

while True:
self._check_events()
self.ship.update()
self._update_bullets()
self._update_screen()


Expand All @@ -42,24 +76,51 @@ def _check_events(self):


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()
elif event.key == pygame.K_SPACE:
self._fire_bullet()


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 _fire_bullet(self):

if len(self.bullets) < self.settings.bullet_limit:
new_bullet = Bullet(self)
self.bullets.add(new_bullet) #add bullet to to bullets group


def _update_bullets(self):

self.bullets.update()

#Delete bullets off screen
for bullet in self.bullets.copy():
if bullet.rect.bottom <= 0:
self.bullets.remove(bullet)


def _update_screen(self):

self.screen.fill(self.settings.bg_color)
self.ship.blitme()
for bullet in self.bullets.sprites():
bullet.draw_bullet()
#Draw each alien in the group
self.aliens.draw(self.screen)

pygame.display.flip()


Expand Down
15 changes: 13 additions & 2 deletions src/bullet.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,18 @@ def __init__(self, ai_game):
self.color = self.settings.bullet_color

self.rect = pygame.Rect(0, 0,
self.settings.bullet_width, self.settings.bullet_height)
self.settings.bullet_width,
self.settings.bullet_height)
self.rect.midtop = ai_game.ship.rect.midtop

self.y = float(self.rect.y)
self.y = float(self.rect.y)


def update(self):
"""Move bullet up"""
self.y -= self.settings.bullet_speed
self.rect.y = self.y


def draw_bullet(self):
pygame.draw.rect(self.screen, self.color, self.rect)
5 changes: 3 additions & 2 deletions src/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ def __init__(self):
#Bullets
self.bullet_speed = 1.
self.bullet_width = 3
self.bullet_height = 15
self.bullet_color = (60, 60, 60)
self.bullet_height = 12
self.bullet_color = (60, 60, 60)
self.bullet_limit = 3
3 changes: 1 addition & 2 deletions src/ship.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import pygame
from os.path import dirname, abspath, join

#Parent directory
path = dirname(dirname(abspath(__file__)))

Expand Down Expand Up @@ -36,5 +35,5 @@ def update(self):


def blitme(self):
"""Draw ship at new loaction"""
"""Draw ship at new location"""
self.screen.blit(self.image, self.rect)

0 comments on commit 468c81f

Please sign in to comment.