import pygame import random # Initialize pygame pygame.init() pygame.mixer.init() # Screen settings WIDTH, HEIGHT = 1024, 768 screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Jack Invaders") # Load assets player_img = pygame.transform.scale(pygame.image.load("player.png"), (40, 40)) alien_img = pygame.transform.scale(pygame.image.load("alien.png"), (40, 40)) bullet_img = pygame.transform.scale(pygame.image.load("bullet.png"), (10, 20)) alien_bullet_img = pygame.transform.scale(pygame.image.load("alien_bullet.png"), (10, 20)) boss_img = pygame.transform.scale(pygame.image.load("boss.png"), (80, 80)) minion_img = pygame.transform.scale(pygame.image.load("minion.png"), (40, 40)) boss_bullet_img = pygame.transform.scale(pygame.image.load("boss_bullet.png"), (15, 25)) # Load sounds boss_shoot_sound = pygame.mixer.Sound("boss_shoot.mp3") boss_speech_sound = pygame.mixer.Sound("boss_speech.mp3") boss_phase2_sound = pygame.mixer.Sound("boss_phase2.mp3") background_music = pygame.mixer.Sound("boss_phase2_music.mp4") background_music.play(-1) # Loop background music # Font font = pygame.font.Font(None, 50) # Game Variables player_x, player_y = WIDTH // 2, HEIGHT - 100 player_speed = 5 bullet_speed = 7 alien_speed = 3 alien_drop_speed = 30 alien_bullet_speed = 4 boss_bullet_speed = 6 aliens = [[j * 60 + 50, i * 50 + 50] for i in range(5) for j in range(10)] player_bullets = [] alien_bullets = [] boss_bullets = [] minion_bullets = [] score = 0 game_over = False boss = None boss_hp = 20 boss_phase_2 = False minions = [] # Alien shooting cooldowns alien_shoot_delay = 1000 last_shot_time = pygame.time.get_ticks() # Game loop running = True while running: pygame.time.delay(30) screen.fill((0, 0, 0)) # Event handling for event in pygame.event.get(): if event.type == pygame.QUIT: running = False if game_over and event.type == pygame.KEYDOWN and event.key == pygame.K_r: aliens = [[j * 60 + 50, i * 50 + 50] for i in range(5) for j in range(10)] player_x, player_y = WIDTH // 2, HEIGHT - 100 player_bullets.clear() alien_bullets.clear() boss_bullets.clear() minion_bullets.clear() score = 0 game_over = False boss = None boss_hp = 20 boss_phase_2 = False minions.clear() elif event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE: player_bullets.append([player_x + 22, player_y]) boss_shoot_sound.play() if not game_over: keys = pygame.key.get_pressed() if keys[pygame.K_a] and player_x > 0: player_x -= player_speed if keys[pygame.K_d] and player_x < WIDTH - 50: player_x += player_speed if keys[pygame.K_w] and player_y > HEIGHT - 300: player_y -= player_speed if keys[pygame.K_s] and player_y < HEIGHT - 50: player_y += player_speed for bullet in player_bullets[:]: bullet[1] -= bullet_speed if bullet[1] < 0: player_bullets.remove(bullet) for alien in aliens: alien[0] += alien_speed if aliens and (aliens[0][0] <= 0 or aliens[-1][0] >= WIDTH - 40): alien_speed *= -1 for alien in aliens: alien[1] += alien_drop_speed for bullet in player_bullets[:]: for alien in aliens[:]: if alien[0] < bullet[0] < alien[0] + 40 and alien[1] < bullet[1] < alien[1] + 40: aliens.remove(alien) player_bullets.remove(bullet) score += 10 break current_time = pygame.time.get_ticks() if current_time - last_shot_time > alien_shoot_delay: last_shot_time = current_time shooting_aliens = random.sample(aliens, min(len(aliens), 3)) for alien in shooting_aliens: alien_bullets.append([alien[0] + 20, alien[1] + 40]) if not aliens and score >= 200 and boss is None: boss = [WIDTH // 2 - 40, 50] boss_speech_sound.play() minions = [[WIDTH // 2 - 100, 100], [WIDTH // 2 + 100, 100]] if boss: screen.blit(boss_img, (boss[0], boss[1])) for minion in minions: screen.blit(minion_img, (minion[0], minion[1])) minion_bullets.append([minion[0] + 20, minion[1] + 40]) boss_bullets.append([boss[0] + 40, boss[1] + 80]) for bullet in boss_bullets: bullet[1] += boss_bullet_speed screen.blit(player_img, (player_x, player_y)) for alien in aliens: screen.blit(alien_img, (alien[0], alien[1])) for bullet in player_bullets: screen.blit(bullet_img, (bullet[0], bullet[1])) for bullet in alien_bullets + minion_bullets: screen.blit(alien_bullet_img, (bullet[0], bullet[1])) for bullet in boss_bullets: screen.blit(boss_bullet_img, (bullet[0], bullet[1])) screen.blit(font.render(f"Score: {score}", True, (255, 255, 255)), (10, 10)) pygame.display.update() pygame.quit()