Update index.html
Browse files- index.html +55 -17
index.html
CHANGED
@@ -10,6 +10,7 @@
|
|
10 |
<div class="game-container">
|
11 |
<div class="ball"></div>
|
12 |
<div class="platform"></div>
|
|
|
13 |
</div>
|
14 |
<div class="controls">
|
15 |
<button id="left-button">◀</button>
|
@@ -18,54 +19,91 @@
|
|
18 |
<script>
|
19 |
const platform = document.querySelector('.platform');
|
20 |
const ball = document.querySelector('.ball');
|
21 |
-
const gameContainer = document.querySelector('.game-container');
|
22 |
const leftButton = document.getElementById('left-button');
|
23 |
const rightButton = document.getElementById('right-button');
|
|
|
|
|
24 |
|
25 |
-
let platformPosition = 50; //
|
26 |
-
let ballPosition = { top: 0, left: Math.random() * 90 }; //
|
|
|
|
|
27 |
|
28 |
-
//
|
29 |
-
|
30 |
if (platformPosition > 0) {
|
31 |
-
platformPosition -= 5;
|
32 |
}
|
33 |
platform.style.left = platformPosition + '%';
|
34 |
-
}
|
35 |
|
36 |
-
|
|
|
37 |
if (platformPosition < 90) {
|
38 |
-
platformPosition += 5;
|
39 |
}
|
40 |
platform.style.left = platformPosition + '%';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
});
|
42 |
|
43 |
-
//
|
44 |
function moveBall() {
|
45 |
-
|
|
|
46 |
ball.style.top = ballPosition.top + '%';
|
47 |
ball.style.left = ballPosition.left + '%';
|
48 |
|
49 |
-
//
|
50 |
if (ballPosition.top >= 90 && Math.abs(ballPosition.left - platformPosition) < 10) {
|
51 |
-
|
|
|
|
|
52 |
resetGame();
|
53 |
}
|
54 |
|
55 |
-
//
|
56 |
if (ballPosition.top >= 100) {
|
57 |
-
|
|
|
|
|
|
|
|
|
58 |
resetGame();
|
59 |
}
|
60 |
|
61 |
requestAnimationFrame(moveBall);
|
62 |
}
|
63 |
|
64 |
-
//
|
65 |
function resetGame() {
|
66 |
-
|
|
|
67 |
}
|
68 |
|
|
|
69 |
moveBall();
|
70 |
</script>
|
71 |
</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>
|
|
|
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>
|