Certainly! Below is a basic implementation of the classic Snake game using plain JavaScript, HTML, and CSS. You can copy all parts into a single .html file and open it in a browser to play.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Snake Game</title>
<style>
body {
background: #000;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
color: #0f0;
font-family: monospace;
}
canvas {
background: #111;
display: block;
box-shadow: 0 0 20px #0f0;
}
#score {
position: absolute;
top: 10px;
color: #0f0;
font-size: 20px;
font-family: monospace;
}
</style>
</head>
<body>
<div id="score">Score: 0</div>
<canvas id="game" width="400" height="400"></canvas>
<script>
const canvas = document.getElementById("game");
const ctx = canvas.getContext("2d");
const scale = 20;
const rows = canvas.height / scale;
const cols = canvas.width / scale;
let snake;
let fruit;
let score = 0;
class Snake {
constructor() {
this.body = [{ x: Math.floor(cols / 2), y: Math.floor(rows / 2) }];
this.xSpeed = 0;
this.ySpeed = 0;
this.grow = 0;
}
draw() {
ctx.fillStyle = "#0f0";
for (let segment of this.body) {
ctx.fillRect(segment.x * scale, segment.y * scale, scale, scale);
}
}
update() {
const head = { ...this.body[0] };
head.x += this.xSpeed;
head.y += this.ySpeed;
// Wrap around edges
if (head.x >= cols) head.x = 0;
else if (head.x < 0) head.x = cols - 1;
if (head.y >= rows) head.y = 0;
else if (head.y < 0) head.y = rows - 1;
// Check self collision
for (let segment of this.body) {
if (segment.x === head.x && segment.y === head.y) {
resetGame();
return;
}
}
this.body.unshift(head);
if (this.grow > 0) {
this.grow--;
} else {
this.body.pop();
}
}
changeDirection(direction) {
switch (direction) {
case "Up":
if (this.ySpeed === 1) break;
this.xSpeed = 0;
this.ySpeed = -1;
break;
case "Down":
if (this.ySpeed === -1) break;
this.xSpeed = 0;
this.ySpeed = 1;
break;
case "Left":
if (this.xSpeed === 1) break;
this.xSpeed = -1;
this.ySpeed = 0;
break;
case "Right":
if (this.xSpeed === -1) break;
this.xSpeed = 1;
this.ySpeed = 0;
break;
}
}
eat(fruitPosition) {
const head = this.body[0];
if (head.x === fruitPosition.x && head.y === fruitPosition.y) {
this.grow += 1;
return true;
}
return false;
}
}
class Fruit {
constructor() {
this.position = this.randomPosition();
}
randomPosition() {
return {
x: Math.floor(Math.random() * cols),
y: Math.floor(Math.random() * rows)
};
}
draw() {
ctx.fillStyle = "#f00";
ctx.fillRect(this.position.x * scale, this.position.y * scale, scale, scale);
}
reset(snakeBody) {
let newPos;
do {
newPos = this.randomPosition();
} while (snakeBody.some(segment => segment.x === newPos.x && segment.y === newPos.y));
this.position = newPos;
}
}
function resetGame() {
alert("Game Over! Your score was " + score);
score = 0;
document.getElementById("score").textContent = "Score: 0";
snake = new Snake();
fruit.reset(snake.body);
}
function setup() {
snake = new Snake();
fruit = new Fruit();
fruit.reset(snake.body);
score = 0;
document.getElementById("score").textContent = "Score: 0";
window.addEventListener("keydown", e => {
const direction = e.key.replace("Arrow", "");
snake.changeDirection(direction);
});
gameLoop();
}
function gameLoop() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
snake.update();
if (snake.eat(fruit.position)) {
score += 1;
document.getElementById("score").textContent = "Score: " + score;
fruit.reset(snake.body);
}
fruit.draw();
snake.draw();
setTimeout(gameLoop, 100);
}
setup();
</script>
</body>
</html>
How to play:
- Use arrow keys to control the snake's direction.
- Eat red fruit squares to grow longer.
- Avoid colliding with yourself.
- The game wraps around edges (snake appears from the opposite side).
- On collision your score is shown and the game restarts.
Let me know if you'd like enhancements like levels, speed increase, or touch controls!