added alien shooting

This commit is contained in:
Stephen Deaton 2024-07-17 17:59:28 -04:00
parent 7bd7057bf9
commit d93b26e2be
10 changed files with 38 additions and 13 deletions

View File

@ -12,9 +12,16 @@ HEIGHT = 800
WINDOW_SIZE = (WIDTH, HEIGHT)
FPS = 60
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
ROWS = 5
COLS = 5
alien_cooldown = 1000
last_alien_shot = pygame.time.get_ticks()
# load background image
bg = pygame.image.load("img/bg.png") # FUCK THIS IN THE ASS FIXXED IT FUCK YEA
@ -72,7 +79,6 @@ class Aliens(pygame.sprite.Sprite):
self.rect.center = [x, y]
self.move_counter = 0
self.move_direction = 1
# WONT MOVE TO THE RIGHT ONLY TO THE LEFT AND STOP
def update(self):
self.rect.x += self.move_direction
self.move_counter += 1
@ -80,14 +86,28 @@ class Aliens(pygame.sprite.Sprite):
self.move_direction *= -1
self.move_counter *= self.move_direction
class Alien_Bullets(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("img/alien_bullet.png")
self.rect = self.image.get_rect()
self.rect.center = [x, y]
def update(self):
self.rect.y += 2
if self.rect.top > HEIGHT:
self.kill()
# create sprite groups
spaceship_group = pygame.sprite.Group()
bullet_group = pygame.sprite.Group()
alien_group = pygame.sprite.Group()
alien_bullet_group = pygame.sprite.Group()
# create player
spaceship = Spaceship(int(WIDTH / 2), HEIGHT - 100, 3)
spaceship_group.add(spaceship)
alien_group = pygame.sprite.Group()
def create_aliens():
for row in range(ROWS):
@ -98,14 +118,6 @@ def create_aliens():
create_aliens()
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode(WINDOW_SIZE)
@ -120,20 +132,33 @@ while running:
draw_bg()
# event handlers
# create randomw alien bullets
#record current time
time_now = pygame.time.get_ticks()
#shoot
if time_now - last_alien_shot > alien_cooldown and len(alien_bullet_group) < 5 and len(alien_group) > 0:
attacking_alien = random.choice(alien_group.sprites())
alien_bullet = Alien_Bullets(attacking_alien.rect.centerx, attacking_alien.rect.bottom)
alien_bullet_group.add(alien_bullet)
last_alien_shot = time_now
# event handlers
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
spaceship.update()
bullet_group.update()
alien_group.update()
alien_bullet_group.update()
# draw sprite groups on screen
spaceship_group.draw(screen)
bullet_group.draw(screen)
alien_group.draw(screen)
pygame.display.update() # Go over it again FLIP vs UPDATE Which to use When
alien_bullet_group.draw(screen)
pygame.display.update()

Binary file not shown.

After

Width:  |  Height:  |  Size: 932 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.4 KiB

Binary file not shown.

Binary file not shown.

Binary file not shown.