<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Snake Game</title>
<style>
body {
margin: 0;
background: #000;
display: flex;
justify-content: center;
align-items: center;
}
canvas {
background: #000;
border: 1px solid #fff;
}
#score {
position: absolute;
top: 10px;
color: #fff;
font-family: Arial, sans-serif;
font-size: 20px;
}
</style>
</head>
<body>
<div id="score">Score: 0</div>
<canvas id="gameCanvas" width="400" height="400"></canvas>
<script>
// Get the canvas and context
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
// Game constants
const gridSize = 20;
const tileCount = canvas.width / gridSize;
let score = 0;
// Snake
let snake = [];
snake[0] = { x: Math.floor(tileCount / 2), y: Math.floor(tileCount / 2) };
let direction = null;
let changingDirection = false;
// Food
let food = {
x: Math.floor(Math.random() * tileCount),
y: Math.floor(Math.random() * tileCount),
};
// Game loop
function gameLoop() {
if (isGameOver()) {
alert('Game Over! Your score was: ' + score);
document.location.reload();
return;
}
changingDirection = false;
setTimeout(function onTick() {
clearCanvas();
drawFood();
moveSnake();
drawSnake();
gameLoop();
}, 100);
}
// Clear the canvas
function clearCanvas() {
ctx.fillStyle = '#000';
ctx.fillRect(0, 0, canvas.width, canvas.height);
}
// Draw the snake
function drawSnake() {
ctx.fillStyle = '#0f0';
snake.forEach(function(part) {
ctx.fillRect(part.x * gridSize, part.y * gridSize, gridSize, gridSize);
});
}
// Move the snake
function moveSnake() {
const head = { x: snake[0].x, y: snake[0].y };
switch (direction) {
case 'LEFT':
head.x -= 1;
break;
case 'UP':
head.y -= 1;
break;
case 'RIGHT':
head.x += 1;
break;
case 'DOWN':
head.y += 1;
break;
default:
return;
}
snake.unshift(head);
// Check if snake has eaten the food
if (head.x === food.x && head.y === food.y) {
score += 10;
document.getElementById('score').innerHTML = 'Score: ' + score;
createFood();
} else {
snake.pop();
}
}
// Check for collision
function isGameOver() {
const head = snake[0];
// Wall collision
if (
head.x < 0 ||
head.x >= tileCount ||
head.y < 0 ||
head.y >= tileCount
) {
return true;
}
// Self collision
for (let i = 1; i < snake.length; i++) {
if (head.x === snake[i].x && head.y === snake[i].y) {
return true;
}
}
return false;
}
// Draw the food
function drawFood() {
ctx.fillStyle = '#f00';
ctx.fillRect(food.x * gridSize, food.y * gridSize, gridSize, gridSize);
}
// Create new food location
function createFood() {
food = {
x: Math.floor(Math.random() * tileCount),
y: Math.floor(Math.random() * tileCount),
};
// Ensure food doesn't appear on the snake
snake.forEach(function(part) {
if (part.x === food.x && part.y === food.y) {
createFood();
}
});
}
// Change direction with keyboard input
document.addEventListener('keydown', changeDirection);
function changeDirection(event) {
if (changingDirection) return;
changingDirection = true;
const keyPressed = event.keyCode;
const LEFT_KEY = 37;
const UP_KEY = 38;
const RIGHT_KEY = 39;
const DOWN_KEY = 40;
const goingUp = direction === 'UP';
const goingDown = direction === 'DOWN';
const goingRight = direction === 'RIGHT';
const goingLeft = direction === 'LEFT';
if (keyPressed === LEFT_KEY && !goingRight) {
direction = 'LEFT';
}
if (keyPressed === UP_KEY && !goingDown) {
direction = 'UP';
}
if (keyPressed === RIGHT_KEY && !goingLeft) {
direction = 'RIGHT';
}
if (keyPressed === DOWN_KEY && !goingUp) {
direction = 'DOWN';
}
}
// Start the game
gameLoop();
</script>
</body>
</html>