Update index.html
Browse files- index.html +103 -41
index.html
CHANGED
@@ -11,23 +11,54 @@
|
|
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 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
// Fungsi untuk memindahkan platform ke kiri
|
33 |
function movePlatformLeft() {
|
@@ -45,66 +76,97 @@
|
|
45 |
platform.style.left = platformPosition + '%';
|
46 |
}
|
47 |
|
48 |
-
//
|
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 (
|
75 |
-
|
|
|
76 |
ball.style.top = ballPosition.top + '%';
|
77 |
ball.style.left = ballPosition.left + '%';
|
78 |
|
79 |
-
// Deteksi tabrakan
|
80 |
if (ballPosition.top >= 90 && Math.abs(ballPosition.left - platformPosition) < 10) {
|
81 |
isCaught = true;
|
82 |
-
score++;
|
83 |
-
scoreDisplay.textContent = score;
|
84 |
-
|
85 |
}
|
86 |
|
87 |
-
// Game
|
88 |
if (ballPosition.top >= 100) {
|
89 |
if (!isCaught) {
|
90 |
-
|
91 |
-
|
92 |
-
|
|
|
|
|
93 |
}
|
94 |
-
|
95 |
}
|
96 |
|
|
|
97 |
requestAnimationFrame(moveBall);
|
98 |
}
|
99 |
|
100 |
-
// Fungsi untuk mereset
|
101 |
-
function
|
102 |
isCaught = false;
|
103 |
-
ballPosition = { top: 0, left: Math.random() * 90 };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
}
|
105 |
|
106 |
-
//
|
107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
</script>
|
109 |
</body>
|
110 |
</html>
|
|
|
11 |
<div class="ball"></div>
|
12 |
<div class="platform"></div>
|
13 |
<div class="score-board">Score: <span id="score">0</span></div>
|
14 |
+
<div class="lives">Lives: <span id="lives">3</span></div>
|
15 |
</div>
|
16 |
<div class="controls">
|
17 |
<button id="left-button">◀</button>
|
18 |
<button id="right-button">▶</button>
|
19 |
+
<button id="pause-button">Pause</button>
|
20 |
</div>
|
21 |
+
|
22 |
+
<!-- Game Over message -->
|
23 |
+
<div class="game-over-message">
|
24 |
+
<h2>Game Over!</h2>
|
25 |
+
<p>Final Score: <span id="final-score"></span></p>
|
26 |
+
<button id="restart-button">Restart</button>
|
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 livesDisplay = document.getElementById('lives');
|
36 |
+
const pauseButton = document.getElementById('pause-button');
|
37 |
const gameContainer = document.querySelector('.game-container');
|
38 |
+
const gameOverMessage = document.querySelector('.game-over-message');
|
39 |
+
const finalScoreDisplay = document.getElementById('final-score');
|
40 |
+
const restartButton = document.getElementById('restart-button');
|
41 |
+
|
42 |
+
let platformPosition = 50; // Posisi platform di tengah
|
43 |
+
let ballPosition = { top: 0, left: Math.random() * 90 }; // Bola mulai di posisi acak
|
44 |
+
let score = 0; // Skor
|
45 |
+
let lives = 3; // Nyawa
|
46 |
+
let isCaught = false; // Status tangkap bola
|
47 |
+
let gamePaused = false; // Status pause
|
48 |
+
let ballSpeed = 2; // Kecepatan bola
|
49 |
+
let level = 1; // Level permainan
|
50 |
+
let timer = 30; // Timer permainan
|
51 |
|
52 |
+
// Pause Functionality
|
53 |
+
pauseButton.addEventListener('click', () => {
|
54 |
+
gamePaused = !gamePaused;
|
55 |
+
if (gamePaused) {
|
56 |
+
pauseButton.textContent = 'Resume';
|
57 |
+
} else {
|
58 |
+
pauseButton.textContent = 'Pause';
|
59 |
+
moveBall(); // Continue game after pause
|
60 |
+
}
|
61 |
+
});
|
62 |
|
63 |
// Fungsi untuk memindahkan platform ke kiri
|
64 |
function movePlatformLeft() {
|
|
|
76 |
platform.style.left = platformPosition + '%';
|
77 |
}
|
78 |
|
79 |
+
// Kontrol tombol
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
leftButton.addEventListener('click', movePlatformLeft);
|
81 |
rightButton.addEventListener('click', movePlatformRight);
|
82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
// Fungsi untuk menggerakkan bola
|
84 |
function moveBall() {
|
85 |
+
if (gamePaused) return; // Jangan jalankan permainan jika di-pause
|
86 |
+
|
87 |
+
ballPosition.top += ballSpeed;
|
88 |
ball.style.top = ballPosition.top + '%';
|
89 |
ball.style.left = ballPosition.left + '%';
|
90 |
|
91 |
+
// Deteksi tabrakan
|
92 |
if (ballPosition.top >= 90 && Math.abs(ballPosition.left - platformPosition) < 10) {
|
93 |
isCaught = true;
|
94 |
+
score++;
|
95 |
+
scoreDisplay.textContent = score;
|
96 |
+
resetBall();
|
97 |
}
|
98 |
|
99 |
+
// Game Over jika bola jatuh tanpa ditangkap
|
100 |
if (ballPosition.top >= 100) {
|
101 |
if (!isCaught) {
|
102 |
+
lives--;
|
103 |
+
livesDisplay.textContent = `Lives: ${lives}`;
|
104 |
+
if (lives <= 0) {
|
105 |
+
showGameOver(); // Tampilkan Game Over
|
106 |
+
}
|
107 |
}
|
108 |
+
resetBall();
|
109 |
}
|
110 |
|
111 |
+
updateLevel(); // Perbarui level setelah mencapai skor tertentu
|
112 |
requestAnimationFrame(moveBall);
|
113 |
}
|
114 |
|
115 |
+
// Fungsi untuk mereset bola
|
116 |
+
function resetBall() {
|
117 |
isCaught = false;
|
118 |
+
ballPosition = { top: 0, left: Math.random() * 90 };
|
119 |
+
}
|
120 |
+
|
121 |
+
// Fungsi untuk memperbarui level
|
122 |
+
function updateLevel() {
|
123 |
+
if (score >= level * 10) {
|
124 |
+
level++;
|
125 |
+
ballSpeed += 0.5; // Meningkatkan kecepatan bola setiap level
|
126 |
+
}
|
127 |
+
}
|
128 |
+
|
129 |
+
// Fungsi untuk reset permainan
|
130 |
+
function resetGame() {
|
131 |
+
score = 0;
|
132 |
+
lives = 3;
|
133 |
+
level = 1;
|
134 |
+
ballSpeed = 2;
|
135 |
+
timer = 30;
|
136 |
+
scoreDisplay.textContent = score;
|
137 |
+
livesDisplay.textContent = `Lives: ${lives}`;
|
138 |
+
finalScoreDisplay.textContent = score;
|
139 |
+
levelDisplay.textContent = `Level: ${level}`;
|
140 |
+
timerDisplay.textContent = `Time: ${timer}s`;
|
141 |
+
gameOverMessage.style.display = 'none'; // Sembunyikan Game Over
|
142 |
+
resetBall();
|
143 |
+
moveBall(); // Mulai kembali permainan
|
144 |
}
|
145 |
|
146 |
+
// Fungsi untuk menampilkan Game Over
|
147 |
+
function showGameOver() {
|
148 |
+
gameContainer.style.display = 'none'; // Sembunyikan konten permainan
|
149 |
+
gameOverMessage.style.display = 'block'; // Tampilkan Game Over
|
150 |
+
}
|
151 |
+
|
152 |
+
// Fungsi untuk timer
|
153 |
+
function countdownTimer() {
|
154 |
+
if (!gamePaused && timer > 0) {
|
155 |
+
timer--;
|
156 |
+
timerDisplay.textContent = `Time: ${timer}s`;
|
157 |
+
} else if (timer <= 0) {
|
158 |
+
alert('Game Over! Time is up.');
|
159 |
+
resetGame();
|
160 |
+
}
|
161 |
+
}
|
162 |
+
|
163 |
+
// Update timer setiap detik
|
164 |
+
setInterval(countdownTimer, 1000);
|
165 |
+
|
166 |
+
// Restart game ketika tombol restart diklik
|
167 |
+
restartButton.addEventListener('click', resetGame);
|
168 |
+
|
169 |
+
moveBall(); // Memulai permainan
|
170 |
</script>
|
171 |
</body>
|
172 |
</html>
|