Sure! Below is a complete single HTML file that contains a visually appealing Snake game with several functionalities:
✅ Features included:
- Responsive and modern UI
- Start/Pause/Restart functionality
- Score tracking
- High score tracking (stored in localStorage)
- Speed increase as the snake grows
- Game over screen
- Sound effects (optional toggle)
- Mobile-friendly controls (WASD, arrow keys, and on-screen buttons)
📦 All in one HTML file — just copy and paste into a .html file and open in your browser.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Snake Game</title>
<style>
* {
box-sizing: border-box;
}
body {
margin: 0;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: linear-gradient(135deg, #1e1e2f, #2c2c3c);
color: #fff;
display: flex;
flex-direction: column;
align-items: center;
justify-content: flex-start;
height: 100vh;
overflow: hidden;
}
h1 {
margin: 20px 0 10px;
font-size: 2rem;
}
#game-container {
position: relative;
width: 90vmin;
height: 90vmin;
max-width: 600px;
max-height: 600px;
background-color: #111;
border: 4px solid #444;
display: flex;
align-items: center;
justify-content: center;
}
canvas {
background-color: #222;
width: 100%;
height: 100%;
}
#scoreboard {
display: flex;
justify-content: space-between;
width: 90vmin;
max-width: 600px;
margin-top: 10px;
font-size: 1.1rem;
}
#controls {
margin-top: 10px;
display: flex;
gap: 10px;
}
button {
padding: 10px 15px;
font-size: 1rem;
border: none;
border-radius: 5px;
background-color: #444;
color: white;
cursor: pointer;
transition: background 0.2s;
}
button:hover {
background-color: #666;
}
#game-over {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0,0,0,0.8);
color: white;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: 2rem;
z-index: 10;
display: none;
}
#mobile-controls {
display: none;
margin-top: 10px;
}
@media (max-width: 600px) {
#mobile-controls {
display: flex;
flex-direction: column;
align-items: center;
gap: 5px;
}
.mobile-row {
display: flex;
gap: 5px;
}
.mobile-btn {
width: 60px;
height: 60px;
font-size: 1.5rem;
background-color: #333;
border: none;
border-radius: 10px;
color: white;
}
.mobile-btn:active {
background-color: #555;
}
}
</style>
</head>
<body>
<h1>🐍 Snake Game</h1>
<div id="game-container">
<canvas id="gameCanvas" width="400" height="400"></canvas>
<div id="game-over">
<div>💀 Game Over!</div>
<div id="final-score" style="font-size: 1.2rem; margin: 10px 0;"></div>
<button onclick="restartGame()">🔁 Restart</button>
</div>
</div>
<div id="scoreboard">
<div>Score: <span id="score">0</span></div>
<div>High Score: <span id="highScore">0</span></div>
</div>
<div id="controls">
<button onclick="togglePause()">⏯️ Pause/Resume</button>
<button onclick="restartGame()">🔁 Restart</button>
<button onclick="toggleSound()">🔊 Sound: <span id="soundStatus">On</span></button>
</div>
<div id="mobile-controls">
<div class="mobile-row">
<button class="mobile-btn" onclick="setDirection('up')">⬆️</button>
</div>
<div class="mobile-row">
<button class="mobile-btn" onclick="setDirection('left')">⬅️</button>
<button class="mobile-btn" onclick="setDirection('down')">⬇️</button>
<button class="mobile-btn" onclick="setDirection('right')">➡️</button>
</div>
</div>
<script>
const canvas = document.getElementById("gameCanvas");
const ctx = canvas.getContext("2d");
const box = 20;
const rows = canvas.height / box;
const cols = canvas.width / box;
let snake = [];
let direction = "right";
let food = {};
let score = 0;
let highScore = localStorage.getItem("snakeHighScore") || 0;
let gameInterval;
let speed = 150;
let isPaused = false;
let soundOn = true;
const scoreEl = document.getElementById("score");
const highScoreEl = document.getElementById("highScore");
const gameOverEl = document.getElementById("game-over");
const finalScoreEl = document.getElementById("final-score");
const soundStatusEl = document.getElementById("soundStatus");
const eatSound = new Audio("https://www.soundjay.com/button/beep-07.wav");
const gameOverSound = new Audio("https://www.soundjay.com/button/beep-10.wav");
function init() {
snake = [{ x: 5, y: 5 }];
direction = "right";
score = 0;
speed = 150;
isPaused = false;
scoreEl.textContent = score;
highScoreEl.textContent = highScore;
gameOverEl.style.display = "none";
placeFood();
clearInterval(gameInterval);
gameInterval = setInterval(gameLoop, speed);
}
function placeFood() {
food = {
x: Math.floor(Math.random() * cols),
y: Math.floor(Math.random() * rows)
};
}
function gameLoop() {
if (isPaused) return;
const head = { ...snake[0] };
switch (direction) {
case "up": head.y--; break;
case "down": head.y++; break;
case "left": head.x--; break;
case "right": head.x++; break;
}
// Wall collision
if (head.x < 0 || head.x >= cols || head.y < 0 || head.y >= rows) {
return gameOver();
}
// Self collision
for (let segment of snake) {
if (head.x === segment.x && head.y === segment.y) {
return gameOver();
}
}
snake.unshift(head);
// Eat food
if (head.x === food.x && head.y === food.y) {
score++;
if (soundOn) eatSound.play();
scoreEl.textContent = score;
if (score > highScore) {
highScore = score;
localStorage.setItem("snakeHighScore", highScore);
highScoreEl.textContent = highScore;
}
placeFood();
if (speed > 50) {
speed -= 5;
clearInterval(gameInterval);
gameInterval = setInterval(gameLoop, speed);
}
} else {
snake.pop();
}
draw();
}
function draw() {
ctx.fillStyle = "#222";
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Draw snake
for (let i = 0; i < snake.length; i++) {
ctx.fillStyle = i === 0 ? "#0f0" : "#0a0";
ctx.fillRect(snake[i].x * box, snake[i].y * box, box - 1, box - 1);
}
// Draw food
ctx.fillStyle = "#f00";
ctx.fillRect(food.x * box, food.y * box, box - 1, box - 1);
}
function gameOver() {
clearInterval(gameInterval);
if (soundOn) gameOverSound.play();
finalScoreEl.textContent = `Your Score: ${score}`;
gameOverEl.style.display = "flex";
}
function restartGame() {
init();
}
function togglePause() {
isPaused = !isPaused;
}
function toggleSound() {
soundOn = !soundOn;
soundStatusEl.textContent = soundOn ? "On" : "Off";
}
function setDirection(dir) {
const opposites = {
up: "down",
down: "up",
left: "right",
right: "left"
};
if (dir !== opposites[direction]) {
direction = dir;
}
}
document.addEventListener("keydown", (e) => {
const key = e.key.toLowerCase();
if (["arrowup", "w"].includes(key)) setDirection("up");
else if (["arrowdown", "s"].includes(key)) setDirection("down");
else if (["arrowleft", "a"].includes(key)) setDirection("left");
else if (["arrowright", "d"].includes(key)) setDirection("right");
else if (key === " ") togglePause();
});
init();
</script>
</body>
</html>
📝 Notes:
- You can customize the colors, speed, and grid size by adjusting the variables in the script and CSS.
- The game uses localStorage to persist the high score.
- Sound effects are optional and can be toggled.
Enjoy your enhanced Snake game! 🐍🎮