const player = document.getElementById("player");
const block = document.getElementById("block");
const scoreText = document.getElementById("scoreText");

let score = 0;
let playerX = 180;

// KECEPATAN (DIUBAH)
let speed = 1;             
let speedIncrease = 0.6;   
let maxSpeed = 12;         

document.addEventListener("keydown", (e) => {
    if (e.key === "ArrowLeft" && playerX > 0) {
        playerX -= 20;
    }
    if (e.key === "ArrowRight" && playerX < 360) {
        playerX += 20;
    }
    player.style.left = playerX + "px";
});

function moveBlock() {
    let blockY = -40;
    let blockX = Math.floor(Math.random() * 360);
    block.style.left = blockX + "px";

    let fall = setInterval(() => {
        blockY += speed;
        block.style.top = blockY + "px";

        // TABRAKAN
        if (
            blockY > 450 &&
            blockX < playerX + 40 &&
            blockX + 40 > playerX
        ) {
            alert("Game Over! Score: " + score);
            score = 0;
            speed = 1; // reset ke pelan (DIUBAH)
            scoreText.textContent = "Score: 0";
            clearInterval(fall);
            moveBlock();
        }

        // BLOCK LOLOS
        if (blockY > 500) {
            score++;
            scoreText.textContent = "Score: " + score;

            // NAIK BERTAHAP (TERASA)
            if (speed < maxSpeed) {
                speed += speedIncrease;
            }

            blockY = -40;
            block.style.top = "-40px";
            clearInterval(fall);
            moveBlock();
        }

    }, 16); // DIUBAH: lebih smooth (60 FPS)
}

moveBlock();
