Sure! Here's a Snake Game using HTML, CSS, and JavaScript, all in a single HTML file, with a unique twist: the snake leaves a fading trail behind it, giving the game a cool visual effect.
🎮 Unique Twist: Fading Trail Effect
Instead of the traditional solid grid, the snake's body and previous positions fade away over time using CSS opacity and JavaScript, giving the game a retro, glowing trail aesthetic.
✅ How to Use
- Copy the code below into a file named
snake.html.
- Open it in your browser.
- Use arrow keys or WASD to move the snake.
🕹️ Complete Snake Game with Fading Trail (Single File)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Snake Game with Fading Trail</title>
<style>
* {
box-sizing: border-box;
}
body {
margin: 0;
background: #000;
color: #0f0;
font-family: monospace;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
overflow: hidden;
}
#game {
display: grid;
grid-template-columns: repeat(20, 20px);
grid-template-rows: repeat(20, 20px);
gap: 1px;
background-color: #111;
padding: 5px;
border: 2px solid #0f0;
}
.cell {
width: 20px;
height: 20px;
background-color: #000;
transition: opacity 0.8s ease-out;
}
.head {
background-color: #0f0;
}
.food {
background-color: #f00;
}
.trail {
opacity: 0.3;
}
#score {
position: absolute;
top: 10px;
left: 10px;
font-size: 1.2em;
}
</style>
</head>
<body>
<div id="score">Score: 0</div>
<div id="game"></div>
<script>
const gridSize = 20;
const game = document.getElementById('game');
const scoreDisplay = document.getElementById('score');
// Create grid
const cells = [];
for (let i = 0; i < gridSize * gridSize; i++) {
const cell = document.createElement('div');
cell.classList.add('cell');
game.appendChild(cell);
cells.push(cell);
}
let snake = [210];
let direction = 1;
let food = null;
let score = 0;
let gameInterval = setInterval(gameLoop, 150);
function draw() {
// Fade all snake parts
cells.forEach(cell => {
if (cell.classList.contains('head') || cell.classList.contains('food')) return;
cell.classList.add('trail');
});
// Clear all
cells.forEach(cell => cell.classList.remove('head', 'food'));
// Draw snake head
cells[snake[0]].classList.add('head');
// Draw food
if (food !== null) {
cells[food].classList.add('food');
}
// Draw trail
for (let i = 1; i < snake.length; i++) {
if (cells[snake[i]]) {
cells[snake[i]].classList.remove('trail');
cells[snake[i]].classList.add('head');
}
}
}
function placeFood() {
let newFood;
do {
newFood = Math.floor(Math.random() * cells.length);
} while (snake.includes(newFood));
food = newFood;
}
function gameLoop() {
const head = snake[0] + direction;
// Collision detection
if (
head < 0 ||
head >= gridSize * gridSize ||
(direction === 1 && head % gridSize === 0) ||
(direction === -1 && (head + 1) % gridSize === 0) ||
snake.includes(head)
) {
clearInterval(gameInterval);
alert('Game Over! Final Score: ' + score);
return;
}
snake.unshift(head);
// Eat food
if (head === food) {
score++;
scoreDisplay.textContent = 'Score: ' + score;
placeFood();
} else {
snake.pop();
}
draw();
}
function changeDirection(newDirection) {
// Prevent reversing
if ((direction === 1 && newDirection !== -1) ||
(direction === -1 && newDirection !== 1) ||
(direction === gridSize && newDirection !== -gridSize) ||
(direction === -gridSize && newDirection !== gridSize)) {
direction = newDirection;
}
}
window.addEventListener('keydown', e => {
switch (e.key) {
case 'ArrowUp':
case 'w':
changeDirection(-gridSize);
break;
case 'ArrowDown':
case 's':
changeDirection(gridSize);
break;
case 'ArrowLeft':
case 'a':
changeDirection(-1);
break;
case 'ArrowRight':
case 'd':
changeDirection(1);
break;
}
});
// Initial setup
placeFood();
draw();
</script>
</body>
</html>
🌟 Features
- Fading trail effect using CSS transitions.
- Classic snake gameplay.
- Score display.
- Keyboard controls (Arrow keys or WASD).
- Game over alert with final score.
Let me know if you'd like to add more twists like:
- Multiplayer mode
- Obstacles
- Power-ups
- Sound effects
- Mobile touch support
Happy coding! 🐍✨