Update index.html
Browse files- index.html +106 -36
index.html
CHANGED
@@ -11,10 +11,12 @@
|
|
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');
|
@@ -22,12 +24,53 @@
|
|
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; //
|
28 |
-
let ballPosition = { top: 0, left: Math.random() * 90 }; // Bola
|
29 |
-
let
|
30 |
-
let
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
// Fungsi untuk memindahkan platform ke kiri
|
33 |
function movePlatformLeft() {
|
@@ -45,66 +88,93 @@
|
|
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 |
-
//
|
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 |
<script>
|
22 |
const platform = document.querySelector('.platform');
|
|
|
24 |
const leftButton = document.getElementById('left-button');
|
25 |
const rightButton = document.getElementById('right-button');
|
26 |
const scoreDisplay = document.getElementById('score');
|
27 |
+
const livesDisplay = document.getElementById('lives');
|
28 |
+
const pauseButton = document.getElementById('pause-button');
|
29 |
const gameContainer = document.querySelector('.game-container');
|
30 |
|
31 |
+
let platformPosition = 50; // Posisi platform di tengah
|
32 |
+
let ballPosition = { top: 0, left: Math.random() * 90 }; // Bola mulai di posisi acak
|
33 |
+
let score = 0; // Skor
|
34 |
+
let lives = 3; // Nyawa
|
35 |
+
let isCaught = false; // Status tangkap bola
|
36 |
+
let gamePaused = false; // Status pause
|
37 |
+
let ballSpeed = 2; // Kecepatan bola
|
38 |
+
let level = 1; // Level permainan
|
39 |
+
let timer = 30; // Timer permainan
|
40 |
+
|
41 |
+
// Display level dan timer
|
42 |
+
const levelDisplay = document.createElement('div');
|
43 |
+
levelDisplay.textContent = `Level: ${level}`;
|
44 |
+
levelDisplay.style.position = 'absolute';
|
45 |
+
levelDisplay.style.top = '10px';
|
46 |
+
levelDisplay.style.right = '10px';
|
47 |
+
levelDisplay.style.fontSize = '1.5em';
|
48 |
+
levelDisplay.style.color = '#ffffff';
|
49 |
+
levelDisplay.style.background = 'rgba(0, 0, 0, 0.5)';
|
50 |
+
levelDisplay.style.padding = '10px';
|
51 |
+
document.body.appendChild(levelDisplay);
|
52 |
+
|
53 |
+
const timerDisplay = document.createElement('div');
|
54 |
+
timerDisplay.textContent = `Time: ${timer}s`;
|
55 |
+
timerDisplay.style.position = 'absolute';
|
56 |
+
timerDisplay.style.top = '40px';
|
57 |
+
timerDisplay.style.right = '10px';
|
58 |
+
timerDisplay.style.fontSize = '1.5em';
|
59 |
+
timerDisplay.style.color = '#ffffff';
|
60 |
+
timerDisplay.style.background = 'rgba(0, 0, 0, 0.5)';
|
61 |
+
timerDisplay.style.padding = '10px';
|
62 |
+
document.body.appendChild(timerDisplay);
|
63 |
+
|
64 |
+
// Pause Functionality
|
65 |
+
pauseButton.addEventListener('click', () => {
|
66 |
+
gamePaused = !gamePaused;
|
67 |
+
if (gamePaused) {
|
68 |
+
pauseButton.textContent = 'Resume';
|
69 |
+
} else {
|
70 |
+
pauseButton.textContent = 'Pause';
|
71 |
+
moveBall(); // Continue game after pause
|
72 |
+
}
|
73 |
+
});
|
74 |
|
75 |
// Fungsi untuk memindahkan platform ke kiri
|
76 |
function movePlatformLeft() {
|
|
|
88 |
platform.style.left = platformPosition + '%';
|
89 |
}
|
90 |
|
91 |
+
// Kontrol keyboard
|
92 |
window.addEventListener('keydown', (e) => {
|
93 |
if (e.key === 'ArrowLeft') movePlatformLeft();
|
94 |
if (e.key === 'ArrowRight') movePlatformRight();
|
95 |
});
|
96 |
|
97 |
+
// Kontrol tombol
|
98 |
leftButton.addEventListener('click', movePlatformLeft);
|
99 |
rightButton.addEventListener('click', movePlatformRight);
|
100 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
// Fungsi untuk menggerakkan bola
|
102 |
function moveBall() {
|
103 |
+
if (gamePaused) return; // Jangan jalankan permainan jika di-pause
|
104 |
+
|
105 |
+
ballPosition.top += ballSpeed;
|
106 |
ball.style.top = ballPosition.top + '%';
|
107 |
ball.style.left = ballPosition.left + '%';
|
108 |
|
109 |
+
// Deteksi tabrakan
|
110 |
if (ballPosition.top >= 90 && Math.abs(ballPosition.left - platformPosition) < 10) {
|
111 |
isCaught = true;
|
112 |
+
score++;
|
113 |
+
scoreDisplay.textContent = score;
|
114 |
+
resetBall();
|
115 |
}
|
116 |
|
117 |
+
// Game Over jika bola jatuh tanpa ditangkap
|
118 |
if (ballPosition.top >= 100) {
|
119 |
if (!isCaught) {
|
120 |
+
lives--;
|
121 |
+
livesDisplay.textContent = `Lives: ${lives}`;
|
122 |
+
if (lives <= 0) {
|
123 |
+
alert(`Game Over! Final Score: ${score}`);
|
124 |
+
resetGame();
|
125 |
+
}
|
126 |
}
|
127 |
+
resetBall();
|
128 |
}
|
129 |
|
130 |
+
updateLevel(); // Perbarui level setelah mencapai skor tertentu
|
131 |
requestAnimationFrame(moveBall);
|
132 |
}
|
133 |
|
134 |
+
// Fungsi untuk mereset bola
|
135 |
+
function resetBall() {
|
136 |
isCaught = false;
|
137 |
+
ballPosition = { top: 0, left: Math.random() * 90 };
|
138 |
}
|
139 |
|
140 |
+
// Fungsi untuk memperbarui level
|
141 |
+
function updateLevel() {
|
142 |
+
if (score >= level * 10) {
|
143 |
+
level++;
|
144 |
+
levelDisplay.textContent = `Level: ${level}`;
|
145 |
+
ballSpeed += 0.5; // Meningkatkan kecepatan bola setiap level
|
146 |
+
}
|
147 |
+
}
|
148 |
+
|
149 |
+
// Fungsi untuk reset permainan
|
150 |
+
function resetGame() {
|
151 |
+
score = 0;
|
152 |
+
lives = 3;
|
153 |
+
level = 1;
|
154 |
+
ballSpeed = 2;
|
155 |
+
timer = 30;
|
156 |
+
scoreDisplay.textContent = score;
|
157 |
+
livesDisplay.textContent = `Lives: ${lives}`;
|
158 |
+
levelDisplay.textContent = `Level: ${level}`;
|
159 |
+
timerDisplay.textContent = `Time: ${timer}s`;
|
160 |
+
resetBall();
|
161 |
+
}
|
162 |
+
|
163 |
+
// Fungsi untuk timer
|
164 |
+
function countdownTimer() {
|
165 |
+
if (!gamePaused && timer > 0) {
|
166 |
+
timer--;
|
167 |
+
timerDisplay.textContent = `Time: ${timer}s`;
|
168 |
+
} else if (timer <= 0) {
|
169 |
+
alert('Game Over! Time is up.');
|
170 |
+
resetGame();
|
171 |
+
}
|
172 |
+
}
|
173 |
+
|
174 |
+
// Update timer setiap detik
|
175 |
+
setInterval(countdownTimer, 1000);
|
176 |
+
|
177 |
+
moveBall(); // Memulai permainan
|
178 |
</script>
|
179 |
</body>
|
180 |
</html>
|