Below is a simple implementation of the classic Snake game using HTML, CSS, and JavaScript. The design is kept minimalistic for simplicity.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple Snake Game</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f0f0f0;
}
canvas {
border: 1px solid #000;
}
</style>
</head>
<body>
<canvas id="gameCanvas" width="400" height="400"></canvas>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const gridSize = 20;
const tileCount = canvas.width / gridSize;
let snake = [{ x: 10, y: 10 }];
let direction = { x: 0, y: 0 };
let food = { x: 15, y: 15 };
let score = 0;
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw snake
ctx.fillStyle = 'green';
snake.forEach(segment => {
ctx.fillRect(segment.x * gridSize, segment.y * gridSize, gridSize, gridSize);
});
// Draw food
ctx.fillStyle = 'red';
ctx.fillRect(food.x * gridSize, food.y * gridSize, gridSize, gridSize);
// Draw score
ctx.fillStyle = 'black';
ctx.font = '20px Arial';
ctx.fillText(`Score: ${score}`, 10, 20);
}
function move() {
const head = { x: snake[0].x + direction.x, y: snake[0].y + direction.y };
// Check collision with walls
if (head.x < 0 || head.x >= tileCount || head.y < 0 || head.y >= tileCount) {
resetGame();
return;
}
// Check collision with self
if (snake.some(segment => segment.x === head.x && segment.y === head.y)) {
resetGame();
return;
}
// Check collision with food
if (head.x === food.x && head.y === food.y) {
score++;
placeFood();
} else {
snake.pop();
}
snake.unshift(head);
draw();
}
function placeFood() {
food = {
x: Math.floor(Math.random() * tileCount),
y: Math.floor(Math.random() * tileCount)
};
// Ensure food doesn't spawn on the snake
if (snake.some(segment => segment.x === food.x && segment.y === food.y)) {
placeFood();
}
}
function resetGame() {
snake = [{ x: 10, y: 10 }];
direction = { x: 0, y: 0 };
score = 0;
placeFood();
draw();
}
document.addEventListener('keydown', event => {
switch (event.key) {
case 'ArrowUp':
if (direction.y === 0) direction = { x: 0, y: -1 };
break;
case 'ArrowDown':
if (direction.y === 0) direction = { x: 0, y: 1 };
break;
case 'ArrowLeft':
if (direction.x === 0) direction = { x: -1, y: 0 };
break;
case 'ArrowRight':
if (direction.x === 0) direction = { x: 1, y: 0 };
break;
}
});
function gameLoop() {
if (direction.x !== 0 || direction.y !== 0) {
move();
}
setTimeout(gameLoop, 100);
}
placeFood();
draw();
gameLoop();
</script>
</body>
</html>
How It Works:
- Canvas: The game is rendered on a 400x400 canvas divided into a 20x20 grid.
- Snake: The snake is represented as an array of segments, initially starting at position (10, 10).
- Food: Food is randomly placed on the grid, ensuring it doesn't spawn on the snake.
- Controls: Use the arrow keys to control the snake's direction.
- Game Loop: The
gameLoop function continuously updates the game state every 100 milliseconds.
- Collision Detection: The game checks for collisions with walls, the snake's own body, and food.
- Reset: If the snake collides with a wall or itself, the game resets.
This implementation is minimalistic and focuses on core functionality without unnecessary complexity.