Update index.html
Browse files- index.html +3 -119
index.html
CHANGED
@@ -3,129 +3,13 @@
|
|
3 |
<head>
|
4 |
<meta charset="UTF-8">
|
5 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6 |
-
<title>Futuristic
|
7 |
<link rel="stylesheet" href="style.css">
|
8 |
</head>
|
9 |
<body>
|
10 |
<div class="game-container">
|
11 |
-
<div class="
|
12 |
-
<div class="
|
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 |
-
|
20 |
-
<!-- Game Over Overlay -->
|
21 |
-
<div id="game-over-overlay" class="overlay">
|
22 |
-
<div class="overlay-content">
|
23 |
-
<h2>Game Over!</h2>
|
24 |
-
<p>Final Score: <span id="final-score"></span></p>
|
25 |
-
<button id="restart-button">Restart Game</button>
|
26 |
-
</div>
|
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>
|
131 |
</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 class="player"></div>
|
12 |
+
<div class="falling-ball"></div>
|
|
|
13 |
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
</body>
|
15 |
</html>
|