HTML
Reset
Game Ular Sederhana
Game Ular Sederhana
Skor:
0
Mulai Permainan
CSS
Reset
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@400;600&display=swap'); :root { --bg-color: #1a1a2e; --card-bg: #22223b; --text-color: #e4e4f0; --primary-color: #e94560; --secondary-color: #55a630; } body { font-family: 'Poppins', sans-serif; display: flex; flex-direction: column; justify-content: center; align-items: center; min-height: 100vh; margin: 0; background: var(--bg-color); color: var(--text-color); padding: 20px; } h1 { font-size: 2.8rem; color: var(--primary-color); text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3); margin-bottom: 25px; } .game-container { display: flex; flex-direction: column; align-items: center; border: 3px solid var(--primary-color); background: var(--card-bg); border-radius: 12px; box-shadow: 0 8px 16px rgba(0, 0, 0, 0.4); padding: 20px; } #gameCanvas { background-color: #000; border: 2px solid var(--secondary-color); border-radius: 8px; box-shadow: inset 0 0 10px rgba(0, 0, 0, 0.5); transition: transform 0.3s ease-in-out; } #gameCanvas:hover { transform: scale(1.01); } .info-panel { display: flex; justify-content: space-between; align-items: center; width: 100%; margin-top: 20px; font-size: 1.4rem; font-weight: 600; } #skor { color: var(--secondary-color); font-size: 1.6rem; } #tombolMulai { padding: 12px 24px; font-size: 1rem; font-weight: 600; cursor: pointer; border: none; border-radius: 50px; background: linear-gradient(45deg, var(--primary-color), #ff7b90); color: white; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); transition: all 0.3s ease; } #tombolMulai:hover { transform: translateY(-2px); box-shadow: 0 6px 12px rgba(0, 0, 0, 0.3); background: linear-gradient(45deg, #ff7b90, var(--primary-color)); }
JavaScript
Reset
document.addEventListener('DOMContentLoaded', function() { const canvas = document.getElementById('gameCanvas'); const ctx = canvas.getContext('2d'); const tombolMulai = document.getElementById('tombolMulai'); const skorDisplay = document.getElementById('skor'); const ukuranKotak = 20; const canvasUkuran = 400; const kecepatanPiksel = 2; let ular; let makanan; let dx, dy; let skor; let gameLoop; let gameSedangBerjalan = false; let arahBerikutnya; function inisialisasiGame() { ular = [{ x: 10 * ukuranKotak, y: 10 * ukuranKotak }]; makanan = null; // Menambahkan ini agar tempatkanMakanan tidak menempatkan makanan di posisi null dx = 0; dy = 0; skor = 0; skorDisplay.textContent = skor; arahBerikutnya = null; tempatkanMakanan(); if (gameLoop) { cancelAnimationFrame(gameLoop); } gameSedangBerjalan = true; gameLoop = requestAnimationFrame(perbaruiGame); } function gambar() { ctx.fillStyle = '#000'; ctx.fillRect(0, 0, canvasUkuran, canvasUkuran); ctx.fillStyle = 'red'; if (makanan) { ctx.fillRect(makanan.x, makanan.y, ukuranKotak, ukuranKotak); } ular.forEach(bagian => { ctx.fillStyle = 'lime'; ctx.fillRect(bagian.x, bagian.y, ukuranKotak, ukuranKotak); ctx.strokeStyle = '#000'; ctx.strokeRect(bagian.x, bagian.y, ukuranKotak, ukuranKotak); }); } function perbaruiGame() { if (!gameSedangBerjalan) return; for (let i = ular.length - 1; i > 0; i--) { ular[i].x = ular[i - 1].x; ular[i].y = ular[i - 1].y; } ular[0].x += dx * kecepatanPiksel; ular[0].y += dy * kecepatanPiksel; if (ular[0].x % ukuranKotak === 0 && ular[0].y % ukuranKotak === 0 && arahBerikutnya) { dx = arahBerikutnya.x; dy = arahBerikutnya.y; arahBerikutnya = null; } if (tabrakan(ular[0])) { gameOver(); return; } if (makanan && ular[0].x === makanan.x && ular[0].y === makanan.y) { skor++; skorDisplay.textContent = skor; const ekor = { x: ular[ular.length - 1].x, y: ular[ular.length - 1].y }; ular.push(ekor); tempatkanMakanan(); } gambar(); gameLoop = requestAnimationFrame(perbaruiGame); } function tabrakan(kepala) { if (kepala.x < 0 || kepala.x >= canvasUkuran || kepala.y < 0 || kepala.y >= canvasUkuran) { return true; } for (let i = 1; i < ular.length; i++) { if (kepala.x === ular[i].x && kepala.y === ular[i].y) { return true; } } return false; } function gameOver() { cancelAnimationFrame(gameLoop); gameSedangBerjalan = false; Swal.fire({ title: 'Game Selesai!', text: `Skor Anda: ${skor}`, icon: 'info', confirmButtonText: 'Mulai Lagi' }).then((result) => { if (result.isConfirmed) { inisialisasiGame(); } }); } function tempatkanMakanan() { let posisiValid = false; while (!posisiValid) { makanan = { x: Math.floor(Math.random() * (canvasUkuran / ukuranKotak)) * ukuranKotak, y: Math.floor(Math.random() * (canvasUkuran / ukuranKotak)) * ukuranKotak }; posisiValid = true; for (let i = 0; i < ular.length; i++) { if (ular[i].x === makanan.x && ular[i].y === makanan.y) { posisiValid = false; break; } } } } function ubahArah(event) { const arahKey = event.key; if (!gameSedangBerjalan) return; if (ular[0].x % ukuranKotak !== 0 || ular[0].y % ukuranKotak !== 0) { switch (arahKey) { case 'ArrowUp': case 'w': if (dy !== 1) { arahBerikutnya = { x: 0, y: -1 }; } break; case 'ArrowDown': case 's': if (dy !== -1) { arahBerikutnya = { x: 0, y: 1 }; } break; case 'ArrowLeft': case 'a': if (dx !== 1) { arahBerikutnya = { x: -1, y: 0 }; } break; case 'ArrowRight': case 'd': if (dx !== -1) { arahBerikutnya = { x: 1, y: 0 }; } break; } return; } switch (arahKey) { case 'ArrowUp': case 'w': if (dy !== 1) { dy = -1; dx = 0; } break; case 'ArrowDown': case 's': if (dy !== -1) { dy = 1; dx = 0; } break; case 'ArrowLeft': case 'a': if (dx !== 1) { dy = 0; dx = -1; } break; case 'ArrowRight': case 'd': if (dx !== -1) { dy = 0; dx = 1; } break; } } tombolMulai.addEventListener('click', inisialisasiGame); document.addEventListener('keydown', ubahArah); gambar(); });
Output
Jalankan