import pygame
import random
import sys

pygame.init()

# =========================
# WINDOW
# =========================
WIDTH = 500
HEIGHT = 600

window = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Space Defender")
clock = pygame.time.Clock()

# =========================
# WARNA
# =========================
BLACK = (20, 20, 30)
WHITE = (255, 255, 255)
BLUE = (0, 150, 255)
RED = (255, 60, 60)
YELLOW = (255, 230, 0)

# =========================
# PLAYER
# =========================
player = pygame.Rect(225, 530, 50, 30)
player_speed = 6

# =========================
# LIST PROYEKTIL
# =========================
bullets = []

bullet_speed = 8

# Cooldown tembakan
shoot_cooldown = 400  # milidetik
last_shot = 0

# =========================
# LIST MUSUH
# =========================
enemies = []

enemy_speed = 3

# Membuat 5 musuh
for i in range(5):
    enemy = pygame.Rect(
        random.randint(0, WIDTH - 40),
        random.randint(-400, -50),
        40,
        40
    )

    enemies.append(enemy)

# =========================
# SCORE
# =========================
score = 0

font = pygame.font.SysFont(None, 36)

# =========================
# GAME LOOP
# =========================
running = True

while running:

    # =========================
    # EVENT
    # =========================
    for event in pygame.event.get():

        if event.type == pygame.QUIT:
            running = False

    # =========================
    # INPUT PLAYER
    # =========================
    keys = pygame.key.get_pressed()

    if keys[pygame.K_LEFT] and player.left > 0:
        player.x -= player_speed

    if keys[pygame.K_RIGHT] and player.right < WIDTH:
        player.x += player_speed

    # =========================
    # MENEMBAK
    # =========================
    current_time = pygame.time.get_ticks()

    if keys[pygame.K_SPACE]:

        # Cek cooldown
        if current_time - last_shot >= shoot_cooldown:

            bullet = pygame.Rect(
                player.centerx - 3,
                player.top,
                6,
                15
            )

            # Masukkan peluru ke List
            bullets.append(bullet)

            last_shot = current_time

    # =========================
    # GERAKKAN PELURU
    # =========================
    for bullet in bullets[:]:

        bullet.y -= bullet_speed

        # Hapus jika keluar layar
        if bullet.bottom < 0:
            bullets.remove(bullet)

    # =========================
    # GERAKKAN BANYAK MUSUH
    # =========================
    for enemy in enemies:

        enemy.y += enemy_speed

        # Jika musuh keluar layar
        if enemy.top > HEIGHT:

            enemy.x = random.randint(
                0,
                WIDTH - enemy.width
            )

            enemy.y = random.randint(
                -300,
                -50
            )

    # =========================
    # COLLISION
    # PELURU VS MUSUH
    # =========================
    for bullet in bullets[:]:

        for enemy in enemies:

            if bullet.colliderect(enemy):

                # Hapus peluru
                if bullet in bullets:
                    bullets.remove(bullet)

                # Tambah skor
                score += 10

                # Respawn musuh
                enemy.x = random.randint(
                    0,
                    WIDTH - enemy.width
                )

                enemy.y = random.randint(
                    -300,
                    -50
                )

                break

    # =========================
    # COLLISION
    # PLAYER VS MUSUH
    # =========================
    for enemy in enemies:

        if player.colliderect(enemy):

            print("GAME OVER")
            running = False

    # =========================
    # RENDER
    # =========================
    window.fill(BLACK)

    # Player
    pygame.draw.rect(
        window,
        BLUE,
        player
    )

    # Peluru
    for bullet in bullets:

        pygame.draw.rect(
            window,
            YELLOW,
            bullet
        )

    # Musuh
    for enemy in enemies:

        pygame.draw.rect(
            window,
            RED,
            enemy
        )

    # =========================
    # SCORE
    # =========================
    score_text = font.render(
        f"Score: {score}",
        True,
        WHITE
    )

    window.blit(
        score_text,
        (10, 10)
    )

    # =========================
    # UPDATE
    # =========================
    pygame.display.update()
    clock.tick(60)

pygame.quit()
sys.exit()