Update index.html
Browse files- index.html +130 -57
index.html
CHANGED
@@ -3,77 +3,150 @@
|
|
3 |
<head>
|
4 |
<meta charset="UTF-8">
|
5 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6 |
-
<title>Futuristic Ball Game</title>
|
7 |
<link rel="stylesheet" href="style.css">
|
8 |
</head>
|
9 |
<body>
|
10 |
<div class="game-container">
|
|
|
|
|
11 |
<div id="score">Score: 0</div>
|
12 |
<div id="level">Level: 1</div>
|
13 |
-
<div id="lives">
|
14 |
-
|
|
|
|
|
|
|
|
|
15 |
</div>
|
16 |
-
|
17 |
<script>
|
18 |
-
let
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
let level = 1;
|
20 |
-
let
|
21 |
-
|
22 |
-
|
23 |
-
const levelElement = document.getElementById('level');
|
24 |
-
const livesElement = document.getElementById('lives');
|
25 |
-
|
26 |
-
// Function to create a new ball
|
27 |
-
function createBall() {
|
28 |
-
const ball = document.createElement('div');
|
29 |
-
ball.classList.add('ball');
|
30 |
-
|
31 |
-
// Random horizontal position for the ball
|
32 |
-
ball.style.left = `${Math.random() * (window.innerWidth - 30)}px`;
|
33 |
-
ball.style.top = '-30px'; // Start above the screen
|
34 |
-
document.body.appendChild(ball);
|
35 |
-
|
36 |
-
let ballFallInterval = setInterval(() => {
|
37 |
-
const ballRect = ball.getBoundingClientRect();
|
38 |
-
const platformRect = document.querySelector('.platform').getBoundingClientRect();
|
39 |
-
|
40 |
-
// Check if the ball has hit the platform
|
41 |
-
if (
|
42 |
-
ballRect.bottom >= platformRect.top &&
|
43 |
-
ballRect.left >= platformRect.left &&
|
44 |
-
ballRect.right <= platformRect.right
|
45 |
-
) {
|
46 |
-
score += 10;
|
47 |
-
scoreElement.textContent = `Score: ${score}`;
|
48 |
-
ball.remove(); // Remove the ball after it has been caught
|
49 |
-
clearInterval(ballFallInterval); // Stop the falling animation for this ball
|
50 |
-
createBall(); // Create a new ball for the next round
|
51 |
-
} else if (ballRect.top > window.innerHeight) {
|
52 |
-
lives -= 1;
|
53 |
-
livesElement.innerHTML = `Lives: ${'❤️ '.repeat(lives)}`;
|
54 |
-
ball.remove(); // Remove the ball if it falls off screen
|
55 |
-
clearInterval(ballFallInterval); // Stop the ball's fall
|
56 |
-
createBall(); // Create a new ball after losing a life
|
57 |
-
if (lives === 0) {
|
58 |
-
alert('Game Over!');
|
59 |
-
}
|
60 |
-
}
|
61 |
-
}, 20); // Check every 20ms
|
62 |
|
63 |
-
|
64 |
-
|
|
|
|
|
|
|
65 |
|
66 |
-
//
|
67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
|
69 |
-
//
|
70 |
-
|
71 |
-
|
72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
});
|
74 |
-
|
75 |
-
|
|
|
76 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
</script>
|
78 |
</body>
|
79 |
</html>
|
|
|
3 |
<head>
|
4 |
<meta charset="UTF-8">
|
5 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6 |
+
<title>Futuristic Falling Ball Game</title>
|
7 |
<link rel="stylesheet" href="style.css">
|
8 |
</head>
|
9 |
<body>
|
10 |
<div class="game-container">
|
11 |
+
<div id="ball" class="ball"></div>
|
12 |
+
<div id="platform" class="platform"></div>
|
13 |
<div id="score">Score: 0</div>
|
14 |
<div id="level">Level: 1</div>
|
15 |
+
<div id="lives">
|
16 |
+
<span id="life1" class="life">♥️</span>
|
17 |
+
<span id="life2" class="life">♥️</span>
|
18 |
+
<span id="life3" class="life">♥️</span>
|
19 |
+
<span id="life4" class="life">♥️</span>
|
20 |
+
</div>
|
21 |
</div>
|
22 |
+
|
23 |
<script>
|
24 |
+
let ball = document.getElementById("ball");
|
25 |
+
let platform = document.getElementById("platform");
|
26 |
+
let score = document.getElementById("score");
|
27 |
+
let levelDisplay = document.getElementById("level");
|
28 |
+
let livesDisplay = document.getElementById("lives");
|
29 |
+
|
30 |
+
let currentScore = 0;
|
31 |
let level = 1;
|
32 |
+
let fallSpeed = 3; // Initial fall speed (seconds)
|
33 |
+
let platformX = window.innerWidth / 2 - 50;
|
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
|
43 |
+
document.addEventListener("mousemove", (e) => {
|
44 |
+
platformX = e.clientX - platform.offsetWidth / 2;
|
45 |
+
if (platformX < 0) platformX = 0;
|
46 |
+
if (platformX > window.innerWidth - platform.offsetWidth) {
|
47 |
+
platformX = window.innerWidth - platform.offsetWidth;
|
48 |
+
}
|
49 |
+
platform.style.left = platformX + "px";
|
50 |
+
});
|
51 |
|
52 |
+
// Kontrol menggunakan Sentuhan
|
53 |
+
document.addEventListener("touchmove", (e) => {
|
54 |
+
if (isTouching) {
|
55 |
+
let touchX = e.touches[0].clientX;
|
56 |
+
platformX = touchX - platform.offsetWidth / 2;
|
57 |
+
if (platformX < 0) platformX = 0;
|
58 |
+
if (platformX > window.innerWidth - platform.offsetWidth) {
|
59 |
+
platformX = window.innerWidth - platform.offsetWidth;
|
60 |
+
}
|
61 |
+
platform.style.left = platformX + "px";
|
62 |
+
}
|
63 |
+
}, { passive: false });
|
64 |
+
|
65 |
+
document.addEventListener("touchstart", (e) => {
|
66 |
+
isTouching = true;
|
67 |
});
|
68 |
+
|
69 |
+
document.addEventListener("touchend", (e) => {
|
70 |
+
isTouching = false;
|
71 |
});
|
72 |
+
|
73 |
+
let updateLives = () => {
|
74 |
+
for (let i = 1; i <= 4; i++) {
|
75 |
+
let life = document.getElementById(`life${i}`);
|
76 |
+
if (i > remainingLives) {
|
77 |
+
life.style.visibility = "hidden";
|
78 |
+
} else {
|
79 |
+
life.style.visibility = "visible";
|
80 |
+
}
|
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 |
+
// Stop the animation when ball is reset
|
121 |
+
ball.style.animation = 'none';
|
122 |
+
setTimeout(() => {
|
123 |
+
ball.style.animation = `fall ${fallSpeed}s linear infinite`;
|
124 |
+
}, 50); // Delay to restart the animation
|
125 |
+
|
126 |
+
ballY = -30; // Reset ball to start position
|
127 |
+
ballX = Math.floor(Math.random() * (window.innerWidth - 30)); // New random X position
|
128 |
+
ball.style.top = ballY + "px";
|
129 |
+
ball.style.left = ballX + "px";
|
130 |
+
};
|
131 |
+
|
132 |
+
let resetGame = () => {
|
133 |
+
remainingLives = 4;
|
134 |
+
currentScore = 0;
|
135 |
+
level = 1;
|
136 |
+
ballSpeed = 2;
|
137 |
+
fallSpeed = 3;
|
138 |
+
score.innerHTML = "Score: " + currentScore;
|
139 |
+
levelDisplay.innerHTML = "Level: " + level;
|
140 |
+
updateLives();
|
141 |
+
resetBall();
|
142 |
+
};
|
143 |
+
|
144 |
+
let gameLoop = () => {
|
145 |
+
updateBallPosition();
|
146 |
+
requestAnimationFrame(gameLoop); // Loop the game
|
147 |
+
};
|
148 |
+
|
149 |
+
gameLoop(); // Start the game loop
|
150 |
</script>
|
151 |
</body>
|
152 |
</html>
|