Update index.html
Browse files- index.html +92 -64
index.html
CHANGED
@@ -1,82 +1,110 @@
|
|
1 |
<!DOCTYPE html>
|
2 |
<html lang="en">
|
3 |
<head>
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
</head>
|
9 |
<body>
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
let score = 0;
|
21 |
-
let isGameOver = false;
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
if (isGameOver) return;
|
31 |
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
-
//
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
catcherPosition + catcher.offsetWidth >= ball.offsetLeft + ball.offsetWidth)) {
|
41 |
-
score++;
|
42 |
-
scoreDisplay.textContent = "Score: " + score;
|
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 |
</body>
|
82 |
</html>
|
|
|
1 |
<!DOCTYPE html>
|
2 |
<html lang="en">
|
3 |
<head>
|
4 |
+
<meta charset="UTF-8">
|
5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6 |
+
<title>Futuristic Catch Game</title>
|
7 |
+
<link rel="stylesheet" href="style.css">
|
8 |
</head>
|
9 |
<body>
|
10 |
+
<div class="game-container">
|
11 |
+
<div class="ball"></div>
|
12 |
+
<div class="platform"></div>
|
13 |
+
<div class="score-board">Score: <span id="score">0</span></div>
|
14 |
+
</div>
|
15 |
+
<div class="controls">
|
16 |
+
<button id="left-button">◀</button>
|
17 |
+
<button id="right-button">▶</button>
|
18 |
+
</div>
|
19 |
+
<script>
|
20 |
+
const platform = document.querySelector('.platform');
|
21 |
+
const ball = document.querySelector('.ball');
|
22 |
+
const leftButton = document.getElementById('left-button');
|
23 |
+
const rightButton = document.getElementById('right-button');
|
24 |
+
const scoreDisplay = document.getElementById('score');
|
25 |
+
const gameContainer = document.querySelector('.game-container');
|
26 |
|
27 |
+
let platformPosition = 50; // Platform awal di tengah.
|
28 |
+
let ballPosition = { top: 0, left: Math.random() * 90 }; // Bola muncul secara acak.
|
29 |
+
let isCaught = false; // Apakah bola ditangkap.
|
30 |
+
let score = 0; // Skor awal.
|
|
|
|
|
31 |
|
32 |
+
// Fungsi untuk memindahkan platform ke kiri
|
33 |
+
function movePlatformLeft() {
|
34 |
+
if (platformPosition > 0) {
|
35 |
+
platformPosition -= 5;
|
36 |
+
}
|
37 |
+
platform.style.left = platformPosition + '%';
|
38 |
+
}
|
|
|
39 |
|
40 |
+
// Fungsi untuk memindahkan platform ke kanan
|
41 |
+
function movePlatformRight() {
|
42 |
+
if (platformPosition < 90) {
|
43 |
+
platformPosition += 5;
|
44 |
+
}
|
45 |
+
platform.style.left = platformPosition + '%';
|
46 |
+
}
|
47 |
|
48 |
+
// Keyboard controls
|
49 |
+
window.addEventListener('keydown', (e) => {
|
50 |
+
if (e.key === 'ArrowLeft') movePlatformLeft();
|
51 |
+
if (e.key === 'ArrowRight') movePlatformRight();
|
52 |
+
});
|
|
|
|
|
|
|
|
|
53 |
|
54 |
+
// Button controls
|
55 |
+
leftButton.addEventListener('click', movePlatformLeft);
|
56 |
+
rightButton.addEventListener('click', movePlatformRight);
|
|
|
57 |
|
58 |
+
// Mouse/Touch controls
|
59 |
+
gameContainer.addEventListener('mousemove', (e) => {
|
60 |
+
const containerWidth = gameContainer.offsetWidth;
|
61 |
+
platformPosition = (e.clientX / containerWidth) * 100;
|
62 |
+
platform.style.left = platformPosition + '%';
|
63 |
+
});
|
64 |
|
65 |
+
gameContainer.addEventListener('touchmove', (e) => {
|
66 |
+
const touch = e.touches[0];
|
67 |
+
const containerWidth = gameContainer.offsetWidth;
|
68 |
+
platformPosition = (touch.clientX / containerWidth) * 100;
|
69 |
+
platform.style.left = platformPosition + '%';
|
70 |
+
});
|
71 |
|
72 |
+
// Fungsi untuk menggerakkan bola
|
73 |
+
function moveBall() {
|
74 |
+
if (isCaught) return; // Jangan lanjut jika bola ditangkap.
|
75 |
+
ballPosition.top += 2; // Bola turun.
|
76 |
+
ball.style.top = ballPosition.top + '%';
|
77 |
+
ball.style.left = ballPosition.left + '%';
|
|
|
78 |
|
79 |
+
// Deteksi tabrakan (collision detection)
|
80 |
+
if (ballPosition.top >= 90 && Math.abs(ballPosition.left - platformPosition) < 10) {
|
81 |
+
isCaught = true;
|
82 |
+
score++; // Tambahkan skor.
|
83 |
+
scoreDisplay.textContent = score; // Update skor di layar.
|
84 |
+
resetGame();
|
85 |
+
}
|
86 |
|
87 |
+
// Game over jika bola melewati platform
|
88 |
+
if (ballPosition.top >= 100) {
|
89 |
+
if (!isCaught) {
|
90 |
+
alert('Game Over! Final Score: ' + score);
|
91 |
+
score = 0; // Reset skor.
|
92 |
+
scoreDisplay.textContent = score; // Update skor.
|
93 |
+
}
|
94 |
+
resetGame();
|
95 |
+
}
|
96 |
+
|
97 |
+
requestAnimationFrame(moveBall);
|
98 |
+
}
|
99 |
+
|
100 |
+
// Fungsi untuk mereset permainan
|
101 |
+
function resetGame() {
|
102 |
+
isCaught = false;
|
103 |
+
ballPosition = { top: 0, left: Math.random() * 90 }; // Bola muncul ulang.
|
104 |
+
}
|
105 |
|
106 |
+
// Memulai permainan
|
107 |
+
moveBall();
|
108 |
+
</script>
|
109 |
</body>
|
110 |
</html>
|