File size: 6,228 Bytes
0460853
 
 
 
 
38e2aa1
a7c28a1
0460853
 
 
d93db1d
 
 
 
 
 
 
 
 
03d9c2e
 
d93db1d
 
 
 
 
 
 
 
 
 
 
 
cd2bfee
0460853
38e2aa1
c530330
38e2aa1
 
 
 
 
d93db1d
 
38e2aa1
 
daef768
d93db1d
38e2aa1
 
dd4ebb4
d93db1d
38e2aa1
 
d93db1d
38e2aa1
dd4ebb4
38e2aa1
 
 
 
 
 
 
 
 
c530330
38e2aa1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53e2177
38e2aa1
 
 
53e2177
38e2aa1
 
d93db1d
 
 
 
 
 
 
38e2aa1
 
 
 
 
 
d93db1d
38e2aa1
 
 
 
 
 
 
 
 
 
 
 
 
d93db1d
 
38e2aa1
a7c28a1
38e2aa1
 
 
 
d93db1d
 
38e2aa1
d93db1d
38e2aa1
d93db1d
38e2aa1
 
 
 
d93db1d
 
 
 
 
 
38e2aa1
 
 
 
 
 
d93db1d
 
 
 
 
 
 
38e2aa1
 
d93db1d
 
38e2aa1
 
 
d93db1d
38e2aa1
d93db1d
38e2aa1
 
 
 
 
 
 
 
53e2177
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Futuristic Falling Ball Game</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="game-container">
        <!-- Nyawa (♥️) ditempatkan di bagian atas layar -->
        <div id="lives">
            <span class="life">❤️</span>
            <span class="life">❤️</span>
            <span class="life">❤️</span>
            <span class="life">❤️</span>
        </div>

        <!-- Skor dan Level di posisi kiri dan kanan atas -->
        <div id="score">Score: 0</div>
        <div id="level">Level: 1</div>

        <!-- Platform yang dikendalikan oleh pemain -->
        <div id="platform" class="platform"></div>

        <!-- Bola yang akan jatuh -->
        <div id="ball" class="ball"></div>
        
        <!-- Game Over -->
        <div id="game-over" class="game-over">
            <h2>Game Over!</h2>
            <p>Your Score: <span id="final-score">0</span></p>
            <button id="restart-btn" onclick="restartGame()">Restart</button>
        </div>
    </div>

    <script>
        let ball = document.getElementById("ball");
        let platform = document.getElementById("platform");
        let score = document.getElementById("score");
        let levelDisplay = document.getElementById("level");
        let livesDisplay = document.getElementById("lives");
        let gameOverDisplay = document.getElementById("game-over");
        let finalScoreDisplay = document.getElementById("final-score");

        let currentScore = 0;
        let level = 1;
        let fallSpeed = 3;  // Initial fall speed (seconds)
        let platformX = window.innerWidth / 2 - 50;
        platform.style.left = platformX + "px";

        let remainingLives = 4;
        let ballY = -30;
        let ballX = Math.floor(Math.random() * (window.innerWidth - 30));
        let ballSpeed = 2;  // Ball fall speed per frame
        let isTouching = false;

        // Kontrol menggunakan Mouse
        document.addEventListener("mousemove", (e) => {
            platformX = e.clientX - platform.offsetWidth / 2;
            if (platformX < 0) platformX = 0;
            if (platformX > window.innerWidth - platform.offsetWidth) {
                platformX = window.innerWidth - platform.offsetWidth;
            }
            platform.style.left = platformX + "px";
        });

        // Kontrol menggunakan Sentuhan
        document.addEventListener("touchmove", (e) => {
            if (isTouching) {
                let touchX = e.touches[0].clientX;
                platformX = touchX - platform.offsetWidth / 2;
                if (platformX < 0) platformX = 0;
                if (platformX > window.innerWidth - platform.offsetWidth) {
                    platformX = window.innerWidth - platform.offsetWidth;
                }
                platform.style.left = platformX + "px";
            }
        }, { passive: false });

        document.addEventListener("touchstart", (e) => {
            isTouching = true;
        });

        document.addEventListener("touchend", (e) => {
            isTouching = false;
        });

        let updateLives = () => {
            for (let i = 1; i <= 4; i++) {
                let life = document.getElementById(`life${i}`);
                if (i > remainingLives) {
                    life.style.visibility = "hidden";
                } else {
                    life.style.visibility = "visible";
                }
            }
        };

        let updateBallPosition = () => {
            ballY += ballSpeed;

            // Update ball position
            ball.style.top = ballY + "px";
            ball.style.left = ballX + "px";

            let ballRect = ball.getBoundingClientRect();
            let platformRect = platform.getBoundingClientRect();

            if (ballY + 30 >= platformRect.top && ballY + 30 <= platformRect.bottom) {
                if (ballX + 30 >= platformRect.left && ballX <= platformRect.right) {
                    // Ball hits the platform
                    currentScore++;
                    if (currentScore % 10 === 0) {
                        level++;
                        levelDisplay.innerHTML = "Level: " + level;
                        fallSpeed -= 0.2;
                        ballSpeed += 0.1;  // Make ball fall faster
                    }
                    score.innerHTML = "Score: " + currentScore;
                    resetBall();
                }
            } else if (ballY >= window.innerHeight) {
                // Ball missed the platform
                remainingLives--;
                updateLives();
                if (remainingLives <= 0) {
                    gameOver();
                }
                resetBall();
            }
        };

        let resetBall = () => {
            // Stop the animation when ball is reset
            ball.style.animation = 'none';
            setTimeout(() => {
                ball.style.animation = `fall ${fallSpeed}s linear infinite`;
            }, 50);  // Delay to restart the animation

            ballY = -30; // Reset ball to start position
            ballX = Math.floor(Math.random() * (window.innerWidth - 30)); // New random X position
            ball.style.top = ballY + "px";
            ball.style.left = ballX + "px";
        };

        let gameOver = () => {
            finalScoreDisplay.textContent = currentScore;
            gameOverDisplay.style.display = "block";
        };

        let restartGame = () => {
            remainingLives = 4;
            currentScore = 0;
            level = 1;
            ballSpeed = 2;
            fallSpeed = 3;
            score.innerHTML = "Score: " + currentScore;
            levelDisplay.innerHTML = "Level: " + level;
            updateLives();
            gameOverDisplay.style.display = "none";
            resetBall();
            gameLoop();
        };

        let gameLoop = () => {
            updateBallPosition();
            requestAnimationFrame(gameLoop);  // Loop the game
        };

        gameLoop(); // Start the game loop
    </script>
</body>
</html>