Absolutely! Below is a single HTML file for a visually appealing Snake game with many functionalities:
- Features:
- Start/Pause/Restart controls
- Score and High Score tracking (localStorage)
- Multiple difficulty levels
- Sound effects (toggleable)
- Colorful, animated snake and food
- Responsive design (works on desktop and mobile)
- Touch controls for mobile
- Customizable snake/food colors
- Smooth movement and transitions
Just copy and save as snake.html and open in your browser!
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Snake Game Deluxe</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
html, body {
height: 100%;
margin: 0;
padding: 0;
background: radial-gradient(circle at 50% 30%, #222 60%, #111 100%);
font-family: 'Segoe UI', Arial, sans-serif;
color: #fff;
user-select: none;
overflow: hidden;
}
#container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: flex-start;
height: 100vh;
width: 100vw;
}
h1 {
margin: 20px 0 10px 0;
font-size: 2.5em;
letter-spacing: 2px;
text-shadow: 0 2px 8px #000a;
}
#gameArea {
position: relative;
background: linear-gradient(135deg, #232526 0%, #414345 100%);
border-radius: 18px;
box-shadow: 0 8px 32px #0008, 0 1.5px 0 #fff2 inset;
margin: 10px 0;
overflow: hidden;
touch-action: none;
}
#ui {
display: flex;
flex-wrap: wrap;
gap: 12px;
margin-bottom: 10px;
align-items: center;
justify-content: center;
}
.btn {
background: linear-gradient(90deg, #4e54c8 0%, #8f94fb 100%);
color: #fff;
border: none;
border-radius: 8px;
padding: 8px 18px;
font-size: 1em;
font-weight: bold;
cursor: pointer;
box-shadow: 0 2px 8px #0003;
transition: background 0.2s, transform 0.1s;
}
.btn:active {
background: linear-gradient(90deg, #8f94fb 0%, #4e54c8 100%);
transform: scale(0.97);
}
#scoreboard {
font-size: 1.1em;
margin: 0 10px;
letter-spacing: 1px;
text-shadow: 0 1px 4px #000a;
}
#settings {
display: flex;
gap: 10px;
align-items: center;
margin-top: 8px;
flex-wrap: wrap;
justify-content: center;
}
label {
font-size: 0.95em;
margin-right: 4px;
}
select, input[type="color"] {
border-radius: 5px;
border: 1px solid #444;
padding: 2px 6px;
font-size: 1em;
background: #222;
color: #fff;
margin-right: 8px;
}
#mobileControls {
display: none;
margin-top: 10px;
gap: 10px;
flex-direction: column;
align-items: center;
user-select: none;
}
.arrow-btn {
width: 48px;
height: 48px;
background: linear-gradient(135deg, #8f94fb 0%, #4e54c8 100%);
border: none;
border-radius: 50%;
color: #fff;
font-size: 2em;
display: flex;
align-items: center;
justify-content: center;
margin: 2px;
box-shadow: 0 2px 8px #0003;
cursor: pointer;
transition: background 0.2s, transform 0.1s;
}
.arrow-btn:active {
background: linear-gradient(135deg, #4e54c8 0%, #8f94fb 100%);
transform: scale(0.95);
}
@media (max-width: 700px) {
#gameArea {
width: 95vw !important;
height: 60vw !important;
max-width: 98vw !important;
max-height: 70vw !important;
}
#container {
padding-bottom: 60px;
}
#mobileControls {
display: flex;
}
}
</style>
</head>
<body>
<div id="container">
<h1>🐍 Snake Game Deluxe</h1>
<div id="ui">
<button class="btn" id="startBtn">Start</button>
<button class="btn" id="pauseBtn" disabled>Pause</button>
<button class="btn" id="restartBtn" disabled>Restart</button>
<span id="scoreboard">Score: 0 | High: 0</span>
<button class="btn" id="soundBtn">🔊</button>
</div>
<div id="settings">
<label>Difficulty:
<select id="difficulty">
<option value="easy">Easy</option>
<option value="normal" selected>Normal</option>
<option value="hard">Hard</option>
<option value="insane">Insane</option>
</select>
</label>
<label>Snake:
<input type="color" id="snakeColor" value="#00ff99">
</label>
<label>Food:
<input type="color" id="foodColor" value="#ff3366">
</label>
</div>
<div id="gameArea" style="width: 600px; height: 400px;">
<canvas id="gameCanvas" width="600" height="400"></canvas>
</div>
<div id="mobileControls">
<div style="display: flex; justify-content: center;">
<button class="arrow-btn" id="upBtn">▲</button>
</div>
<div style="display: flex; justify-content: center;">
<button class="arrow-btn" id="leftBtn">◀</button>
<button class="arrow-btn" id="downBtn">▼</button>
<button class="arrow-btn" id="rightBtn">▶</button>
</div>
</div>
<div style="margin-top: 10px; font-size: 0.95em; color: #aaa;">
<span>Controls: Arrow keys / WASD / Touch</span>
</div>
</div>
<audio id="eatSound" src="https://cdn.pixabay.com/audio/2022/07/26/audio_124bfae3e2.mp3"></audio>
<audio id="gameOverSound" src="https://cdn.pixabay.com/audio/2022/07/26/audio_124bfae3e2.mp3"></audio>
<script>
// --- CONFIG ---
const CELL_SIZE = 20;
const ROWS = 20;
const COLS = 30;
const CANVAS_W = COLS * CELL_SIZE;
const CANVAS_H = ROWS * CELL_SIZE;
const DIFFICULTY_SPEED = {
easy: 120,
normal: 80,
hard: 55,
insane: 35
};
const DIFFICULTY_NAME = {
easy: "Easy",
normal: "Normal",
hard: "Hard",
insane: "Insane"
};
// --- DOM ---
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const startBtn = document.getElementById('startBtn');
const pauseBtn = document.getElementById('pauseBtn');
const restartBtn = document.getElementById('restartBtn');
const scoreboard = document.getElementById('scoreboard');
const difficultySel = document.getElementById('difficulty');
const snakeColorInput = document.getElementById('snakeColor');
const foodColorInput = document.getElementById('foodColor');
const soundBtn = document.getElementById('soundBtn');
const eatSound = document.getElementById('eatSound');
const gameOverSound = document.getElementById('gameOverSound');
const mobileControls = document.getElementById('mobileControls');
const upBtn = document.getElementById('upBtn');
const downBtn = document.getElementById('downBtn');
const leftBtn = document.getElementById('leftBtn');
const rightBtn = document.getElementById('rightBtn');
// --- GAME STATE ---
let snake, direction, nextDirection, food, score, highScore, gameInterval, running, paused, gameOver, speed, difficulty;
let snakeColor = snakeColorInput.value;
let foodColor = foodColorInput.value;
let soundOn = true;
let touchStart = null;
// --- INIT ---
function initGame() {
snake = [
{x: Math.floor(COLS/2), y: Math.floor(ROWS/2)},
{x: Math.floor(COLS/2)-1, y: Math.floor(ROWS/2)},
{x: Math.floor(COLS/2)-2, y: Math.floor(ROWS/2)}
];
direction = 'right';
nextDirection = 'right';
food = randomFood();
score = 0;
highScore = getHighScore();
running = false;
paused = false;
gameOver = false;
speed = DIFFICULTY_SPEED[difficultySel.value];
updateScoreboard();
draw();
}
function getHighScore() {
let key = 'snake_highscore_' + difficultySel.value;
return parseInt(localStorage.getItem(key)) || 0;
}
function setHighScore(val) {
let key = 'snake_highscore_' + difficultySel.value;
localStorage.setItem(key, val);
}
// --- GAME LOOP ---
function startGame() {
if (running) return;
running = true;
paused = false;
gameOver = false;
startBtn.disabled = true;
pauseBtn.disabled = false;
restartBtn.disabled = false;
speed = DIFFICULTY_SPEED[difficultySel.value];
gameInterval = setInterval(gameTick, speed);
}
function pauseGame() {
if (!running || paused) return;
paused = true;
clearInterval(gameInterval);
pauseBtn.textContent = "Resume";
}
function resumeGame() {
if (!running || !paused) return;
paused = false;
gameInterval = setInterval(gameTick, speed);
pauseBtn.textContent = "Pause";
}
function restartGame() {
clearInterval(gameInterval);
initGame();
startGame();
}
function endGame() {
running = false;
gameOver = true;
clearInterval(gameInterval);
pauseBtn.disabled = true;
startBtn.disabled = false;
restartBtn.disabled = false;
if (soundOn) {
gameOverSound.currentTime = 0;
gameOverSound.play();
}
if (score > highScore) {
setHighScore(score);
highScore = score;
updateScoreboard();
}
drawGameOver();
}
// --- GAME LOGIC ---
function gameTick() {
if (paused || gameOver) return;
// Update direction
direction = nextDirection;
// Move snake
let head = {...snake[0]};
switch(direction) {
case 'up': head.y -= 1; break;
case 'down': head.y += 1; break;
case 'left': head.x -= 1; break;
case 'right': head.x += 1; break;
}
// Check collision
if (head.x < 0 || head.x >= COLS || head.y < 0 || head.y >= ROWS || snake.some(seg => seg.x === head.x && seg.y === head.y)) {
endGame();
return;
}
// Eat food
if (head.x === food.x && head.y === food.y) {
snake.unshift(head);
score += 1;
if (soundOn) {
eatSound.currentTime = 0;
eatSound.play();
}
food = randomFood();
// Speed up a bit every 5 points
if (score % 5 === 0 && speed > 25) {
speed -= 3;
clearInterval(gameInterval);
gameInterval = setInterval(gameTick, speed);
}
} else {
snake.unshift(head);
snake.pop();
}
updateScoreboard();
draw();
}
function randomFood() {
let pos;
do {
pos = {
x: Math.floor(Math.random() * COLS),
y: Math.floor(Math.random() * ROWS)
};
} while (snake.some(seg => seg.x === pos.x && seg.y === pos.y));
return pos;
}
// --- DRAWING ---
function draw() {
// Clear
ctx.clearRect(0, 0, CANVAS_W, CANVAS_H);
// Draw grid (subtle)
ctx.save();
ctx.strokeStyle = "#222";
ctx.lineWidth = 1;
for (let i = 0; i <= COLS; i++) {
ctx.beginPath();
ctx.moveTo(i*CELL_SIZE, 0);
ctx.lineTo(i*CELL_SIZE, CANVAS_H);
ctx.stroke();
}
for (let i = 0; i <= ROWS; i++) {
ctx.beginPath();
ctx.moveTo(0, i*CELL_SIZE);
ctx.lineTo(CANVAS_W, i*CELL_SIZE);
ctx.stroke();
}
ctx.restore();
// Draw food (animated pulse)
let t = Date.now() / 400;
let pulse = 1 + 0.15 * Math.sin(t*2);
ctx.save();
ctx.shadowColor = foodColor;
ctx.shadowBlur = 16;
ctx.beginPath();
ctx.arc(
food.x * CELL_SIZE + CELL_SIZE/2,
food.y * CELL_SIZE + CELL_SIZE/2,
(CELL_SIZE/2-2) * pulse,
0, 2*Math.PI
);
ctx.fillStyle = foodColor;
ctx.fill();
ctx.restore();
// Draw snake (gradient body, animated head)
for (let i = snake.length-1; i >= 0; i--) {
let seg = snake[i];
let grad = ctx.createLinearGradient(
seg.x*CELL_SIZE, seg.y*CELL_SIZE,
(seg.x+1)*CELL_SIZE, (seg.y+1)*CELL_SIZE
);
let base = shadeColor(snakeColor, -20 + 40*i/snake.length);
grad.addColorStop(0, base);
grad.addColorStop(1, snakeColor);
ctx.save();
ctx.beginPath();
ctx.roundRect(
seg.x*CELL_SIZE+2, seg.y*CELL_SIZE+2,
CELL_SIZE-4, CELL_SIZE-4,
6
);
ctx.fillStyle = grad;
ctx.shadowColor = "#000";
ctx.shadowBlur = 4;
ctx.fill();
ctx.restore();
}
// Draw eyes on head
let head = snake[0];
let eyeOffset = {
up: [ [5,5], [15,5] ],
down:[ [5,15],[15,15] ],
left:[ [5,5], [5,15] ],
right:[ [15,5],[15,15] ]
}[direction];
ctx.save();
ctx.fillStyle = "#fff";
for (let e of eyeOffset) {
ctx.beginPath();
ctx.arc(
head.x*CELL_SIZE + e[0],
head.y*CELL_SIZE + e[1],
2.5, 0, 2*Math.PI
);
ctx.fill();
ctx.beginPath();
ctx.arc(
head.x*CELL_SIZE + e[0]+0.5*Math.sin(t*2),
head.y*CELL_SIZE + e[1]+0.5*Math.cos(t*2),
1.2, 0, 2*Math.PI
);
ctx.fillStyle = "#222";
ctx.fill();
}
ctx.restore();
}
function drawGameOver() {
ctx.save();
ctx.globalAlpha = 0.7;
ctx.fillStyle = "#000";
ctx.fillRect(0, 0, CANVAS_W, CANVAS_H);
ctx.globalAlpha = 1;
ctx.font = "bold 48px Segoe UI, Arial";
ctx.fillStyle = "#ff3366";
ctx.textAlign = "center";
ctx.fillText("Game Over", CANVAS_W/2, CANVAS_H/2-20);
ctx.font = "bold 28px Segoe UI, Arial";
ctx.fillStyle = "#fff";
ctx.fillText("Score: " + score, CANVAS_W/2, CANVAS_H/2+30);
if (score >= highScore) {
ctx.fillStyle = "#00ff99";
ctx.font = "bold 22px Segoe UI, Arial";
ctx.fillText("🎉 New High Score! 🎉", CANVAS_W/2, CANVAS_H/2+65);
}
ctx.restore();
}
function updateScoreboard() {
scoreboard.textContent = `Score: ${score} | High: ${highScore}`;
}
// --- UTILITIES ---
function shadeColor(color, percent) {
// color: "#rrggbb", percent: -100..100
let R = parseInt(color.substring(1,3),16);
let G = parseInt(color.substring(3,5),16);
let B = parseInt(color.substring(5,7),16);
R = Math.min(255, Math.max(0, R + percent));
G = Math.min(255, Math.max(0, G + percent));
B = Math.min(255, Math.max(0, B + percent));
return "#" + ((1<<24)+(R<<16)+(G<<8)+B).toString(16).slice(1);
}
// --- EVENTS ---
document.addEventListener('keydown', e => {
if (!running || paused || gameOver) return;
let key = e.key.toLowerCase();
let dir = null;
if (key === 'arrowup' || key === 'w') dir = 'up';
if (key === 'arrowdown' || key === 's') dir = 'down';
if (key === 'arrowleft' || key === 'a') dir = 'left';
if (key === 'arrowright' || key === 'd') dir = 'right';
if (dir) {
if (isOpposite(dir, direction)) return;
nextDirection = dir;
}
if (key === ' ' && running) {
paused ? resumeGame() : pauseGame();
}
});
function isOpposite(dir1, dir2) {
return (dir1 === 'up' && dir2 === 'down') ||
(dir1 === 'down' && dir2 === 'up') ||
(dir1 === 'left' && dir2 === 'right') ||
(dir1 === 'right' && dir2 === 'left');
}
startBtn.onclick = () => {
if (!running) {
startGame();
}
};
pauseBtn.onclick = () => {
if (!running) return;
if (paused) resumeGame();
else pauseGame();
};
restartBtn.onclick = () => {
restartGame();
};
difficultySel.onchange = () => {
if (running) {
restartGame();
} else {
initGame();
}
};
snakeColorInput.oninput = () => {
snakeColor = snakeColorInput.value;
draw();
};
foodColorInput.oninput = () => {
foodColor = foodColorInput.value;
draw();
};
soundBtn.onclick = () => {
soundOn = !soundOn;
soundBtn.textContent = soundOn ? "🔊" : "🔇";
};
// --- MOBILE CONTROLS ---
function showMobileControls() {
if (window.innerWidth < 700) {
mobileControls.style.display = "flex";
} else {
mobileControls.style.display = "none";
}
}
window.addEventListener('resize', showMobileControls);
showMobileControls();
upBtn.onclick = () => { if (!isOpposite('up', direction)) nextDirection = 'up'; };
downBtn.onclick = () => { if (!isOpposite('down', direction)) nextDirection = 'down'; };
leftBtn.onclick = () => { if (!isOpposite('left', direction)) nextDirection = 'left'; };
rightBtn.onclick = () => { if (!isOpposite('right', direction)) nextDirection = 'right'; };
// Touch swipe controls
canvas.addEventListener('touchstart', e => {
if (e.touches.length === 1) {
touchStart = {x: e.touches[0].clientX, y: e.touches[0].clientY};
}
});
canvas.addEventListener('touchend', e => {
if (!touchStart) return;
let dx = e.changedTouches[0].clientX - touchStart.x;
let dy = e.changedTouches[0].clientY - touchStart.y;
if (Math.abs(dx) > Math.abs(dy)) {
if (dx > 20 && !isOpposite('right', direction)) nextDirection = 'right';
else if (dx < -20 && !isOpposite('left', direction)) nextDirection = 'left';
} else {
if (dy > 20 && !isOpposite('down', direction)) nextDirection = 'down';
else if (dy < -20 && !isOpposite('up', direction)) nextDirection = 'up';
}
touchStart = null;
});
// --- RESPONSIVE CANVAS ---
function resizeCanvas() {
let w = Math.min(window.innerWidth*0.95, 600);
let h = Math.min(window.innerHeight*0.6, 400);
let scale = Math.min(w/CANVAS_W, h/CANVAS_H);
canvas.style.width = (CANVAS_W*scale) + "px";
canvas.style.height = (CANVAS_H*scale) + "px";
document.getElementById('gameArea').style.width = (CANVAS_W*scale) + "px";
document.getElementById('gameArea').style.height = (CANVAS_H*scale) + "px";
}
window.addEventListener('resize', resizeCanvas);
resizeCanvas();
// --- INIT ---
initGame();
// --- ANIMATION LOOP (for food pulse, etc) ---
function animationLoop() {
if (!gameOver) draw();
requestAnimationFrame(animationLoop);
}
animationLoop();
</script>
</body>
</html>
Enjoy your feature-rich, beautiful Snake game!
- Works on desktop and mobile
- Try changing colors, difficulty, and use touch or keyboard!
- High scores are saved per difficulty.
- Sound can be toggled.
- Responsive and visually appealing!