Absolutely! Here's a modern take on the classic Snake game, all contained in a single HTML file. This version includes:
- Responsive design using canvas
- Smooth animations
- Clean UI with score tracking
- Arrow or WASD controls
- Restart button
You can copy this code into a .html file and open it in any modern browser.
✅ Full Modern Snake Game (Single HTML File)
<!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>
html, body {
margin: 0;
padding: 0;
height: 100%;
background: #1e1e1e;
color: #ffffff;
font-family: 'Segoe UI', sans-serif;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
}
canvas {
background: #111;
border: 2px solid #00ff99;
}
#score {
font-size: 1.5em;
margin-bottom: 10px;
}
#restart {
margin-top: 10px;
padding: 8px 16px;
background: #00ff99;
border: none;
color: #000;
font-size: 1em;
border-radius: 5px;
cursor: pointer;
display: none;
}
#restart:hover {
background-color: #00dd88;
}
</style>
</head>
<body>
<div id="score">Score: 0</div>
<canvas id="game" width="400" height="400"></canvas>
<button id="restart">Restart</button>
<script>
const canvas = document.getElementById("game");
const ctx = canvas.getContext("2d");
const box = 20;
const rows = canvas.height / box;
const cols = canvas.width / box;
const scoreElement = document.getElementById("score");
const restartButton = document.getElementById("restart");
let snake, direction, food, score, gameInterval, gameSpeed;
function resetGame() {
snake = [{x: 9 * box, y: 9 * box}];
direction = "RIGHT";
food = randomFood();
score = 0;
gameSpeed = 150;
scoreElement.innerText = "Score: 0";
restartButton.style.display = "none";
clearInterval(gameInterval);
gameInterval = setInterval(gameLoop, gameSpeed);
}
function randomFood() {
let newFood;
do {
newFood = {
x: Math.floor(Math.random() * cols) * box,
y: Math.floor(Math.random() * rows) * box
};
} while (snake.some(segment => segment.x === newFood.x && segment.y === newFood.y));
return newFood;
}
function draw() {
ctx.fillStyle = "#111";
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Draw Snake
snake.forEach((segment, index) => {
ctx.fillStyle = index === 0 ? "#00ff99" : "#00aa66";
ctx.fillRect(segment.x, segment.y, box, box);
});
// Draw Food
ctx.fillStyle = "#ff0033";
ctx.fillRect(food.x, food.y, box, box);
}
function moveSnake() {
const head = {...snake[0]};
switch (direction) {
case "LEFT": head.x -= box; break;
case "RIGHT": head.x += box; break;
case "UP": head.y -= box; break;
case "DOWN": head.y += box; break;
}
// Collision with wall
if (
head.x < 0 || head.x >= canvas.width ||
head.y < 0 || head.y >= canvas.height ||
snake.some(seg => seg.x === head.x && seg.y === head.y)
) {
gameOver();
return;
}
snake.unshift(head);
// Eat food
if (head.x === food.x && head.y === food.y) {
score++;
scoreElement.innerText = "Score: " + score;
food = randomFood();
// Speed up every 5 points
if (score % 5 === 0 && gameSpeed > 50) {
clearInterval(gameInterval);
gameSpeed -= 10;
gameInterval = setInterval(gameLoop, gameSpeed);
}
} else {
snake.pop();
}
}
function gameLoop() {
moveSnake();
draw();
}
function gameOver() {
clearInterval(gameInterval);
ctx.fillStyle = "rgba(0,0,0,0.5)";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = "#fff";
ctx.font = "24px sans-serif";
ctx.textAlign = "center";
ctx.fillText("Game Over", canvas.width / 2, canvas.height / 2 - 10);
ctx.font = "18px sans-serif";
ctx.fillText("Score: " + score, canvas.width / 2, canvas.height / 2 + 20);
restartButton.style.display = "block";
}
document.addEventListener("keydown", (e) => {
const key = e.key.toLowerCase();
if (
(key === "arrowleft" || key === "a") && direction !== "RIGHT"
) direction = "LEFT";
else if (
(key === "arrowup" || key === "w") && direction !== "DOWN"
) direction = "UP";
else if (
(key === "arrowright" || key === "d") && direction !== "LEFT"
) direction = "RIGHT";
else if (
(key === "arrowdown" || key === "s") && direction !== "UP"
) direction = "DOWN";
});
restartButton.addEventListener("click", resetGame);
// Start the first game
resetGame();
</script>
</body>
</html>
🎮 Features
- Modern Design: Dark mode, minimal UI, responsive canvas
- Controls: Arrow keys or WASD
- Gameplay: Snake grows, speeds up, resets on collision
- Score & Restart: Score displayed at top, button to restart
Let me know if you'd like advanced features like:
- High score tracking (with
localStorage)
- Touch/mobile support
- Sound effects
- Online leaderboards
Enjoy coding! 🐍💻