gpt-4o-2024-11-20· OpenAI
WildChat-4.8M (AI2)
buatkan kode html untuk game snake tambahkan css dan java
- User
buatkan kode html untuk game snake tambahkan css dan java
- Assistant
buatkan kode html untuk game snake tambahkan css dan java
Berikut adalah contoh sederhana kode HTML, CSS, dan JavaScript untuk membuat game Snake. Kode ini akan membuat game Snake dasar di mana ular dapat bergerak dan mengumpulkan makanan dengan skor yang bertambah.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Snake Game</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>Snake Game</h1>
<div class="game-container">
<canvas id="gameCanvas" width="400" height="400"></canvas>
</div>
<script src="script.js"></script>
</body>
</html>
style.css:body {
font-family: Arial, sans-serif;
text-align: center;
margin: 0;
padding: 0;
}
h1 {
margin: 20px 0;
}
.game-container {
display: flex;
justify-content: center;
align-items: center;
}
canvas {
border: 1px solid #000;
background: #f0f0f0;
display: block;
margin: 20px auto;
box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.2);
}
script.js:const canvas = document.getElementById("gameCanvas");
const ctx = canvas.getContext("2d");
// Grid size
const tileSize = 20;
const rows = canvas.height / tileSize;
const columns = canvas.width / tileSize;
// Snake starting position
let snake = [{ x: 10, y: 10 }];
let direction = { x: 0, y: 0 };
let food = { x: Math.floor(Math.random() * columns), y: Math.floor(Math.random() * rows) };
let score = 0;
// Game loop
let gameInterval = setInterval(gameLoop, 200);
// Draw function
function draw() {
// Clear canvas
ctx.fillStyle = "#f0f0f0";
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Draw snake
ctx.fillStyle = "green";
snake.forEach(segment => {
ctx.fillRect(segment.x * tileSize, segment.y * tileSize, tileSize, tileSize);
});
// Draw food
ctx.fillStyle = "red";
ctx.fillRect(food.x * tileSize, food.y * tileSize, tileSize, tileSize);
// Draw score
ctx.fillStyle = "#000";
ctx.font = "20px Arial";
ctx.fillText(`Score: ${score}`, 10, 20);
}
// Update function
function update() {
// Move snake head
const head = { x: snake[0].x + direction.x, y: snake[0].y + direction.y };
// Check collisions
if (head.x < 0 || head.y < 0 || head.x >= columns || head.y >= rows || snake.some(segment => segment.x === head.x && segment.y === head.y)) {
alert(`Game Over! Your score was: ${score}`);
clearInterval(gameInterval);
return;
}
// Add new head
snake.unshift(head);
// Check if snake eats food
if (head.x === food.x && head.y === food.y) {
score++;
food = { x: Math.floor(Math.random() * columns), y: Math.floor(Math.random() * rows) };
} else {
// Remove snake tail
snake.pop();
}
}
// Game loop function
function gameLoop() {
update();
draw();
}
// Controls
document.addEventListener("keydown", e => {
if (e.key === "ArrowUp" && direction.y !== 1) direction = { x: 0, y: -1 };
if (e.key === "ArrowDown" && direction.y !== -1) direction = { x: 0, y: 1 };
if (e.key === "ArrowLeft" && direction.x !== 1) direction = { x: -1, y: 0 };
if (e.key === "ArrowRight" && direction.x !== -1) direction = { x: 1, y: 0 };
});
index.html, style.css, script.js.index.html di browser Anda.Selamat mencoba! 🚀
Not yet included in a published release.