<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Game Bom Hindari</title>
<style>
body {
margin: 0;
font-family: Arial;
background: linear-gradient(135deg,#141e30,#243b55);
color: white;
text-align: center;
}
h1 { color: #ffcc00; }
#game {
width: 320px;
height: 450px;
background: #172b46;
border: 4px solid white;
margin: 15px auto;
position: relative;
overflow: hidden;
border-radius: 15px;
}
#player {
position: absolute;
bottom: 10px;
left: 140px;
font-size: 35px;
}
.item {
position: absolute;
font-size: 30px;
}
button {
padding: 12px 22px;
background: #ffcc00;
border: none;
border-radius: 10px;
font-weight: bold;
cursor: pointer;
margin: 5px;
}
#info { font-size: 18px; }
</style>
</head>
<body>
<h1>💣 BOM HINDARI 💣</h1>
<p>Hindari bom dan kumpulkan bintang!</p>
<div id="info">
Skor: <span id="score">0</span> |
Nyawa: <span id="life">3</span>
</div>
<div id="game">
<div id="player">🚀</div>
</div>
<button onclick="startGame()">MULAI GAME</button>
<p>Gunakan tombol ← → untuk bergerak</p>
<script>
const game = document.getElementById("game");
const player = document.getElementById("player");
let x = 140;
let score = 0;
let life = 3;
let running = false;
let items = [];
let timer;
let keys = {};
document.addEventListener("keydown", e => {
keys[e.key] = true;
});
document.addEventListener("keyup", e => {
keys[e.key] = false;
});
function startGame() {
clearInterval(timer);
items.forEach(item => item.el.remove());
items = [];
score = 0;
life = 3;
x = 140;
running = true;
updateInfo();
player.style.left = x + "px";
timer = setInterval(updateGame, 25);
}
function updateGame() {
if (!running) return;
if (keys["ArrowLeft"]) x -= 5;
if (keys["ArrowRight"]) x += 5;
x = Math.max(0, Math.min(280, x));
player.style.left = x + "px";
if (Math.random() < 0.035) {
createItem();
}
items.forEach((item, index) => {
item.y += item.speed;
item.el.style.top = item.y + "px";
let hit =
item.y > 390 &&
item.y < 435 &&
item.x > x - 25 &&
item.x < x + 35;
if (hit) {
if (item.type === "💣") {
life--;
} else {
score += 10;
}
item.el.remove();
items.splice(index, 1);
updateInfo();
if (life <= 0) endGame();
return;
}
if (item.y > 450) {
item.el.remove();
items.splice(index, 1);
}
});
}
function createItem() {
let isBomb = Math.random() < 0.65;
let el = document.createElement("div");
el.className = "item";
el.textContent = isBomb ? "💣" : "⭐";
let obj = {
el: el,
x: Math.random() * 280,
y: -35,
speed: 2 + Math.random() * 2 +
Math.floor(score / 50) * 0.3,
type: isBomb ? "💣" : "⭐"
};
el.style.left = obj.x + "px";
game.appendChild(el);
items.push(obj);
}
function updateInfo() {
document.getElementById("score").textContent = score;
document.getElementById("life").textContent = life;
}
function endGame() {
running = false;
clearInterval(timer);
alert("GAME OVER! Skor kamu: " + score);
}
</script>
</body>
</html>