Update index.html
Browse files- index.html +32 -45
index.html
CHANGED
@@ -27,117 +27,104 @@
|
|
27 |
</div>
|
28 |
|
29 |
<script>
|
|
|
30 |
const platform = document.querySelector('.platform');
|
31 |
const ball = document.querySelector('.ball');
|
32 |
const leftButton = document.getElementById('left-button');
|
33 |
const rightButton = document.getElementById('right-button');
|
34 |
const scoreDisplay = document.getElementById('score');
|
35 |
-
const gameContainer = document.querySelector('.game-container');
|
36 |
const overlay = document.getElementById('game-over-overlay');
|
37 |
const finalScoreDisplay = document.getElementById('final-score');
|
38 |
const restartButton = document.getElementById('restart-button');
|
39 |
|
|
|
40 |
let platformPosition = 50; // Platform awal di tengah.
|
41 |
let ballPosition = { top: 0, left: Math.random() * 90 }; // Bola muncul secara acak.
|
42 |
-
let isCaught = false; // Apakah bola ditangkap.
|
43 |
let score = 0; // Skor awal.
|
44 |
-
let fallSpeed = 0.5; // Kecepatan bola
|
|
|
45 |
|
46 |
// Fungsi untuk memindahkan platform ke kiri
|
47 |
function movePlatformLeft() {
|
48 |
if (platformPosition > 0) {
|
49 |
platformPosition -= 5;
|
|
|
50 |
}
|
51 |
-
platform.style.left = platformPosition + '%';
|
52 |
}
|
53 |
|
54 |
// Fungsi untuk memindahkan platform ke kanan
|
55 |
function movePlatformRight() {
|
56 |
if (platformPosition < 90) {
|
57 |
platformPosition += 5;
|
|
|
58 |
}
|
59 |
-
platform.style.left = platformPosition + '%';
|
60 |
}
|
61 |
|
62 |
-
//
|
63 |
-
window.addEventListener('keydown', (e) => {
|
64 |
-
if (e.key === 'ArrowLeft') movePlatformLeft();
|
65 |
-
if (e.key === 'ArrowRight') movePlatformRight();
|
66 |
-
});
|
67 |
-
|
68 |
-
// Button controls
|
69 |
leftButton.addEventListener('click', movePlatformLeft);
|
70 |
rightButton.addEventListener('click', movePlatformRight);
|
71 |
|
72 |
-
//
|
73 |
-
|
74 |
-
const containerWidth =
|
75 |
platformPosition = (e.clientX / containerWidth) * 100;
|
76 |
platform.style.left = platformPosition + '%';
|
77 |
});
|
78 |
|
79 |
-
|
80 |
const touch = e.touches[0];
|
81 |
-
const containerWidth =
|
82 |
platformPosition = (touch.clientX / containerWidth) * 100;
|
83 |
platform.style.left = platformPosition + '%';
|
84 |
});
|
85 |
|
86 |
// Fungsi untuk menggerakkan bola
|
87 |
function moveBall() {
|
88 |
-
if (isCaught) return; // Jangan lanjut jika bola ditangkap.
|
89 |
-
|
|
|
90 |
ball.style.top = ballPosition.top + '%';
|
91 |
ball.style.left = ballPosition.left + '%';
|
92 |
|
93 |
// Deteksi tabrakan (collision detection)
|
94 |
if (ballPosition.top >= 90 && Math.abs(ballPosition.left - platformPosition) < 10) {
|
95 |
-
|
96 |
-
score
|
97 |
-
|
98 |
-
resetGame();
|
99 |
}
|
100 |
|
101 |
// Game over jika bola melewati platform
|
102 |
if (ballPosition.top >= 100) {
|
103 |
-
|
104 |
-
showGameOver();
|
105 |
-
}
|
106 |
-
resetGame();
|
107 |
}
|
108 |
|
109 |
-
requestAnimationFrame(moveBall);
|
110 |
}
|
111 |
|
112 |
-
// Fungsi untuk mereset
|
113 |
-
function
|
114 |
-
//
|
115 |
-
platformPosition = 50; // Platform kembali ke tengah
|
116 |
-
ballPosition = { top: 0, left: Math.random() * 90 }; // Bola muncul ulang
|
117 |
-
ball.style.left = ballPosition.left + '%';
|
118 |
ball.style.top = ballPosition.top + '%';
|
119 |
-
|
120 |
-
isCaught = false;
|
121 |
-
// Set kecepatan bola ke nilai default setiap kali permainan direset
|
122 |
-
fallSpeed = 0.5;
|
123 |
}
|
124 |
|
125 |
// Fungsi untuk menampilkan overlay Game Over
|
126 |
function showGameOver() {
|
127 |
-
finalScoreDisplay.textContent = score; // Tampilkan skor akhir
|
128 |
-
overlay.style.display = 'flex'; // Tampilkan overlay
|
129 |
}
|
130 |
|
131 |
// Tombol restart untuk memulai ulang permainan
|
132 |
restartButton.addEventListener('click', () => {
|
133 |
-
overlay.style.display = 'none'; // Sembunyikan overlay
|
134 |
-
score = 0; // Reset skor
|
135 |
scoreDisplay.textContent = score;
|
136 |
-
|
137 |
-
moveBall();
|
138 |
});
|
139 |
|
140 |
-
//
|
141 |
moveBall();
|
142 |
</script>
|
143 |
</body>
|
|
|
27 |
</div>
|
28 |
|
29 |
<script>
|
30 |
+
// Elemen UI
|
31 |
const platform = document.querySelector('.platform');
|
32 |
const ball = document.querySelector('.ball');
|
33 |
const leftButton = document.getElementById('left-button');
|
34 |
const rightButton = document.getElementById('right-button');
|
35 |
const scoreDisplay = document.getElementById('score');
|
|
|
36 |
const overlay = document.getElementById('game-over-overlay');
|
37 |
const finalScoreDisplay = document.getElementById('final-score');
|
38 |
const restartButton = document.getElementById('restart-button');
|
39 |
|
40 |
+
// Variabel Permainan
|
41 |
let platformPosition = 50; // Platform awal di tengah.
|
42 |
let ballPosition = { top: 0, left: Math.random() * 90 }; // Bola muncul secara acak.
|
|
|
43 |
let score = 0; // Skor awal.
|
44 |
+
let fallSpeed = 0.5; // Kecepatan bola tetap.
|
45 |
+
let isCaught = false; // Apakah bola sudah ditangkap.
|
46 |
|
47 |
// Fungsi untuk memindahkan platform ke kiri
|
48 |
function movePlatformLeft() {
|
49 |
if (platformPosition > 0) {
|
50 |
platformPosition -= 5;
|
51 |
+
platform.style.left = platformPosition + '%';
|
52 |
}
|
|
|
53 |
}
|
54 |
|
55 |
// Fungsi untuk memindahkan platform ke kanan
|
56 |
function movePlatformRight() {
|
57 |
if (platformPosition < 90) {
|
58 |
platformPosition += 5;
|
59 |
+
platform.style.left = platformPosition + '%';
|
60 |
}
|
|
|
61 |
}
|
62 |
|
63 |
+
// Kontrol menggunakan tombol
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
leftButton.addEventListener('click', movePlatformLeft);
|
65 |
rightButton.addEventListener('click', movePlatformRight);
|
66 |
|
67 |
+
// Kontrol menggunakan mouse/touch
|
68 |
+
document.querySelector('.game-container').addEventListener('mousemove', (e) => {
|
69 |
+
const containerWidth = document.querySelector('.game-container').offsetWidth;
|
70 |
platformPosition = (e.clientX / containerWidth) * 100;
|
71 |
platform.style.left = platformPosition + '%';
|
72 |
});
|
73 |
|
74 |
+
document.querySelector('.game-container').addEventListener('touchmove', (e) => {
|
75 |
const touch = e.touches[0];
|
76 |
+
const containerWidth = document.querySelector('.game-container').offsetWidth;
|
77 |
platformPosition = (touch.clientX / containerWidth) * 100;
|
78 |
platform.style.left = platformPosition + '%';
|
79 |
});
|
80 |
|
81 |
// Fungsi untuk menggerakkan bola
|
82 |
function moveBall() {
|
83 |
+
if (isCaught) return; // Jangan lanjut jika bola sudah ditangkap.
|
84 |
+
|
85 |
+
ballPosition.top += fallSpeed; // Bola jatuh dengan kecepatan tetap.
|
86 |
ball.style.top = ballPosition.top + '%';
|
87 |
ball.style.left = ballPosition.left + '%';
|
88 |
|
89 |
// Deteksi tabrakan (collision detection)
|
90 |
if (ballPosition.top >= 90 && Math.abs(ballPosition.left - platformPosition) < 10) {
|
91 |
+
score++; // Tambahkan skor
|
92 |
+
scoreDisplay.textContent = score; // Update skor di layar
|
93 |
+
resetBall(); // Reset bola setelah ditangkap.
|
|
|
94 |
}
|
95 |
|
96 |
// Game over jika bola melewati platform
|
97 |
if (ballPosition.top >= 100) {
|
98 |
+
showGameOver();
|
|
|
|
|
|
|
99 |
}
|
100 |
|
101 |
+
requestAnimationFrame(moveBall); // Loop pergerakan bola
|
102 |
}
|
103 |
|
104 |
+
// Fungsi untuk mereset posisi bola
|
105 |
+
function resetBall() {
|
106 |
+
ballPosition = { top: 0, left: Math.random() * 90 }; // Bola muncul ulang dengan posisi acak
|
|
|
|
|
|
|
107 |
ball.style.top = ballPosition.top + '%';
|
108 |
+
ball.style.left = ballPosition.left + '%';
|
109 |
+
isCaught = false; // Reset status bola yang ditangkap
|
|
|
|
|
110 |
}
|
111 |
|
112 |
// Fungsi untuk menampilkan overlay Game Over
|
113 |
function showGameOver() {
|
114 |
+
finalScoreDisplay.textContent = score; // Tampilkan skor akhir
|
115 |
+
overlay.style.display = 'flex'; // Tampilkan overlay
|
116 |
}
|
117 |
|
118 |
// Tombol restart untuk memulai ulang permainan
|
119 |
restartButton.addEventListener('click', () => {
|
120 |
+
overlay.style.display = 'none'; // Sembunyikan overlay
|
121 |
+
score = 0; // Reset skor
|
122 |
scoreDisplay.textContent = score;
|
123 |
+
resetBall(); // Reset bola
|
124 |
+
moveBall(); // Mulai pergerakan bola
|
125 |
});
|
126 |
|
127 |
+
// Mulai permainan
|
128 |
moveBall();
|
129 |
</script>
|
130 |
</body>
|