<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Modern Snake Game</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #e0e0e0;
font-family: Arial, sans-serif;
}
#gameCanvas {
border: 2px solid #333;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
background-color: #f0f0f0;
}
</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 cellSize = canvas.width / gridSize;
let snake = [];
let dx = 1; // Initial direction: right
let dy = 0;
let food = { x: 0, y: 0 };
let score = 0;
let gameRunning = false;
let gameInterval;
function startGame() {
snake = [{ x: 10, y: 10 }];
dx = 1;
dy = 0;
score = 0;
generateFood();
gameRunning = true;
gameInterval = setInterval(gameLoop, 100);
}
function generateFood() {
let foodX, foodY;
do {
foodX = Math.floor(Math.random() * gridSize);
foodY = Math.floor(Math.random() * gridSize);
} while (snake.some(segment => segment.x === foodX && segment.y === foodY));
food = { x: foodX, y: foodY };
}
function update() {
const head = { x: snake[0].x + dx, y: snake[0].y + dy };
if (head.x < 0 || head.x >= gridSize || head.y < 0 || head.y >= gridSize || snake.some(segment => segment.x === head.x && segment.y === head.y)) {
clearInterval(gameInterval);
gameRunning = false;
draw(); // Draw the last state
ctx.fillStyle = 'black';
ctx.font = '30px Arial';
ctx.textAlign = 'center';
ctx.fillText('Game Over! Score: ' + score, canvas.width / 2, canvas.height / 2);
ctx.fillText('Press Space to Restart', canvas.width / 2, canvas.height / 2 + 40);
return;
}
snake.unshift(head); // Add new head
if (head.x === food.x && head.y === food.y) {
score++;
generateFood();
} else {
snake.pop(); // Remove tail
}
}
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw snake
ctx.fillStyle = 'green';
snake.forEach(segment => {
ctx.fillRect(segment.x * cellSize, segment.y * cellSize, cellSize, cellSize);
});
// Draw food
ctx.fillStyle = 'red';
ctx.fillRect(food.x * cellSize, food.y * cellSize, cellSize, cellSize);
// Draw score
ctx.fillStyle = 'black';
ctx.font = '20px Arial';
ctx.fillText(`Score: ${score}`, 10, 20);
}
function gameLoop() {
if (gameRunning) {
update();
}
draw();
}
// Handle key presses
document.addEventListener('keydown', (event) => {
if (!gameRunning) {
if (event.key === ' ') {
event.preventDefault();
startGame();
}
return;
}
switch (event.key) {
case 'ArrowUp':
if (dy !== 1) { // Not moving down
dx = 0;
dy = -1;
}
event.preventDefault();
break;
case 'ArrowDown':
if (dy !== -1) { // Not moving up
dx = 0;
dy = 1;
}
event.preventDefault();
break;
case 'ArrowLeft':
if (dx !== 1) { // Not moving right
dx = -1;
dy = 0;
}
event.preventDefault();
break;
case 'ArrowRight':
if (dx !== -1) { // Not moving left
dx = 1;
dy = 0;
}
event.preventDefault();
break;
}
});
// Start the game automatically
startGame();
</script>
</body>
</html>