Update index.html
Browse files- index.html +55 -32
index.html
CHANGED
@@ -34,6 +34,9 @@
|
|
34 |
platform.style.left = platformX + "px";
|
35 |
|
36 |
let remainingLives = 4;
|
|
|
|
|
|
|
37 |
let isTouching = false;
|
38 |
|
39 |
// Kontrol menggunakan Mouse
|
@@ -67,12 +70,6 @@
|
|
67 |
isTouching = false;
|
68 |
});
|
69 |
|
70 |
-
let dropBall = () => {
|
71 |
-
let ballX = Math.floor(Math.random() * (window.innerWidth - 30));
|
72 |
-
ball.style.left = ballX + "px";
|
73 |
-
ball.style.animation = `fall ${fallSpeed}s linear infinite`;
|
74 |
-
};
|
75 |
-
|
76 |
let updateLives = () => {
|
77 |
for (let i = 1; i <= 4; i++) {
|
78 |
let life = document.getElementById(`life${i}`);
|
@@ -84,40 +81,66 @@
|
|
84 |
}
|
85 |
};
|
86 |
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
}
|
101 |
-
|
102 |
-
|
103 |
-
} else if (ballPosition.bottom >= window.innerHeight) {
|
104 |
remainingLives--;
|
105 |
updateLives();
|
106 |
if (remainingLives <= 0) {
|
107 |
alert("Game Over! Your score is: " + currentScore);
|
108 |
-
|
109 |
-
currentScore = 0; // Reset score
|
110 |
-
level = 1; // Reset level
|
111 |
-
fallSpeed = 3; // Reset fall speed
|
112 |
-
score.innerHTML = "Score: " + currentScore;
|
113 |
-
levelDisplay.innerHTML = "Level: " + level;
|
114 |
-
updateLives();
|
115 |
}
|
116 |
-
|
117 |
}
|
118 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
|
120 |
-
|
121 |
</script>
|
122 |
</body>
|
123 |
</html>
|
|
|
34 |
platform.style.left = platformX + "px";
|
35 |
|
36 |
let remainingLives = 4;
|
37 |
+
let ballY = -30;
|
38 |
+
let ballX = Math.floor(Math.random() * (window.innerWidth - 30));
|
39 |
+
let ballSpeed = 2; // Ball fall speed per frame
|
40 |
let isTouching = false;
|
41 |
|
42 |
// Kontrol menggunakan Mouse
|
|
|
70 |
isTouching = false;
|
71 |
});
|
72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
let updateLives = () => {
|
74 |
for (let i = 1; i <= 4; i++) {
|
75 |
let life = document.getElementById(`life${i}`);
|
|
|
81 |
}
|
82 |
};
|
83 |
|
84 |
+
let updateBallPosition = () => {
|
85 |
+
ballY += ballSpeed;
|
86 |
+
|
87 |
+
// Update ball position
|
88 |
+
ball.style.top = ballY + "px";
|
89 |
+
ball.style.left = ballX + "px";
|
90 |
+
|
91 |
+
let ballRect = ball.getBoundingClientRect();
|
92 |
+
let platformRect = platform.getBoundingClientRect();
|
93 |
+
|
94 |
+
if (ballY + 30 >= platformRect.top && ballY + 30 <= platformRect.bottom) {
|
95 |
+
if (ballX + 30 >= platformRect.left && ballX <= platformRect.right) {
|
96 |
+
// Ball hits the platform
|
97 |
+
currentScore++;
|
98 |
+
if (currentScore % 10 === 0) {
|
99 |
+
level++;
|
100 |
+
levelDisplay.innerHTML = "Level: " + level;
|
101 |
+
fallSpeed -= 0.2;
|
102 |
+
ballSpeed += 0.1; // Make ball fall faster
|
103 |
+
}
|
104 |
+
score.innerHTML = "Score: " + currentScore;
|
105 |
+
resetBall();
|
106 |
}
|
107 |
+
} else if (ballY >= window.innerHeight) {
|
108 |
+
// Ball missed the platform
|
|
|
109 |
remainingLives--;
|
110 |
updateLives();
|
111 |
if (remainingLives <= 0) {
|
112 |
alert("Game Over! Your score is: " + currentScore);
|
113 |
+
resetGame();
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
}
|
115 |
+
resetBall();
|
116 |
}
|
117 |
+
};
|
118 |
+
|
119 |
+
let resetBall = () => {
|
120 |
+
ballY = -30; // Reset ball to start position
|
121 |
+
ballX = Math.floor(Math.random() * (window.innerWidth - 30)); // New random X position
|
122 |
+
ball.style.top = ballY + "px";
|
123 |
+
ball.style.left = ballX + "px";
|
124 |
+
};
|
125 |
+
|
126 |
+
let resetGame = () => {
|
127 |
+
remainingLives = 4;
|
128 |
+
currentScore = 0;
|
129 |
+
level = 1;
|
130 |
+
ballSpeed = 2;
|
131 |
+
fallSpeed = 3;
|
132 |
+
score.innerHTML = "Score: " + currentScore;
|
133 |
+
levelDisplay.innerHTML = "Level: " + level;
|
134 |
+
updateLives();
|
135 |
+
resetBall();
|
136 |
+
};
|
137 |
+
|
138 |
+
let gameLoop = () => {
|
139 |
+
updateBallPosition();
|
140 |
+
requestAnimationFrame(gameLoop); // Loop the game
|
141 |
+
};
|
142 |
|
143 |
+
gameLoop(); // Start the game loop
|
144 |
</script>
|
145 |
</body>
|
146 |
</html>
|