Here's a simple implementation of the Snake Game using HTML, CSS, and JavaScript, each separated appropriately. The game includes scoring, colored snake segments, and eyes for the snake's head.
HTML (index.html):
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Snake Game</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>Snake Game</h1>
<div id="score">Score: 0</div>
<canvas id="gameCanvas" width="400" height="400"></canvas>
<script src="game.js"></script>
</body>
</html>
CSS (style.css):
body {
background-color: #f0f0f0;
text-align: center;
font-family: Arial, sans-serif;
}
h1 {
color: #333;
}
#score {
font-size: 20px;
margin-bottom: 10px;
}
#gameCanvas {
background-color: #fff;
border: 1px solid #ccc;
}
JavaScript (game.js):
const canvas = document.getElementById("gameCanvas");
const ctx = canvas.getContext("2d");
const box = 20; // Size of each unit in the game
let snake = [];
snake[0] = { x: 10 * box, y: 10 * box }; // Starting position
let food = {
x: Math.floor(Math.random() * (canvas.width / box)) * box,
y: Math.floor(Math.random() * (canvas.height / box)) * box,
};
let score = 0;
let dir;
document.addEventListener("keydown", direction);
// Control the snake
function direction(event) {
let key = event.keyCode;
if (key == 37 && dir != "RIGHT") {
dir = "LEFT";
} else if (key == 38 && dir != "DOWN") {
dir = "UP";
} else if (key == 39 && dir != "LEFT") {
dir = "RIGHT";
} else if (key == 40 && dir != "UP") {
dir = "DOWN";
}
}
// Check for collision with walls or self
function collision(newHead, snake) {
// Check collision with self
for (let i = 0; i < snake.length; i++) {
if (newHead.x == snake[i].x && newHead.y == snake[i].y) {
return true;
}
}
// Check collision with walls
return (
newHead.x < 0 ||
newHead.x >= canvas.width ||
newHead.y < 0 ||
newHead.y >= canvas.height
);
}
// Draw the eyes on the snake's head
function drawEyes(x, y, size) {
ctx.fillStyle = 'white';
// Left eye
ctx.beginPath();
ctx.arc(x + size / 4, y + size / 4, size / 5, 0, 2 * Math.PI);
ctx.fill();
// Right eye
ctx.beginPath();
ctx.arc(x + (3 * size) / 4, y + size / 4, size / 5, 0, 2 * Math.PI);
ctx.fill();
}
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw the snake
for (let i = 0; i < snake.length; i++) {
ctx.fillStyle = i == 0 ? 'green' : 'lightgreen'; // Head is darker
ctx.fillRect(snake[i].x, snake[i].y, box, box);
ctx.strokeStyle = "darkgreen";
ctx.strokeRect(snake[i].x, snake[i].y, box, box);
// Draw eyes for the head
if (i == 0) {
drawEyes(snake[i].x, snake[i].y, box);
}
}
// Draw the food
ctx.fillStyle = 'red';
ctx.fillRect(food.x, food.y, box, box);
ctx.strokeStyle = "darkred";
ctx.strokeRect(food.x, food.y, box, box);
// Old head position
let snakeX = snake[0].x;
let snakeY = snake[0].y;
// Move the snake in the current direction
if (dir == "LEFT") snakeX -= box;
if (dir == "UP") snakeY -= box;
if (dir == "RIGHT") snakeX += box;
if (dir == "DOWN") snakeY += box;
// Snake eats the food
if (snakeX == food.x && snakeY == food.y) {
score++;
document.getElementById("score").innerHTML = "Score: " + score;
// Generate new food position
food = {
x: Math.floor(Math.random() * (canvas.width / box)) * box,
y: Math.floor(Math.random() * (canvas.height / box)) * box,
};
// Do not remove the tail to grow the snake
} else {
// Remove the tail
snake.pop();
}
let newHead = { x: snakeX, y: snakeY };
// Game over
if (collision(newHead, snake)) {
clearInterval(game);
alert("Game Over! Your score: " + score);
return;
}
// Add new head
snake.unshift(newHead);
}
// Call the draw function every 100 milliseconds
let game = setInterval(draw, 100);
Instructions:
-
Create three separate files in the same folder:
index.html
style.css
game.js
-
Copy the corresponding code from above into each file.
-
Open index.html in a web browser to play the game.
Game Controls:
- Use the arrow keys on your keyboard to control the direction of the snake.
Features:
- Score Display: The current score is displayed above the canvas.
- Snake Colors: The snake's head is a darker green, and the body is light green.
- Eyes: The snake's head has eyes drawn on it.
Notes:
- The game ends if the snake collides with the walls or with itself.
- Feel free to customize the game's appearance by modifying the CSS or enhancing the JavaScript code.
Enjoy playing your customized Snake Game!