Here’s a single standalone HTML file that implements a feature‑rich, visually appealing Snake game. Just save it as, say, snake.html and open it in your browser!
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Ultimate Snake Game</title>
<style>
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: var(--bg);
color: var(--fg);
display: flex; flex-direction: column; align-items: center;
height: 100vh; overflow: hidden;
}
:root {
--bg: #222;
--fg: #eee;
--accent: #4caf50;
--board-bg: #111;
--grid-color: rgba(255,255,255,.05);
}
/* Light theme */
.light {
--bg: #f4f4f4;
--fg: #222;
--board-bg: #fff;
--grid-color: rgba(0,0,0,.05);
}
canvas {
background: var(--board-bg);
box-shadow: 0 0 20px rgba(0,0,0,.5);
margin: 20px;
}
.controls {
display: flex; flex-wrap: wrap; gap:10px; align-items: center;
}
.controls > * {
padding: 6px 12px;
font-size: 0.9rem;
border: 1px solid var(--fg);
background: transparent;
color: var(--fg);
border-radius: 4px;
cursor: pointer;
transition: background .2s;
}
.controls > *:hover { background: var(--fg); color: var(--bg); }
.controls label { display: flex; align-items: center; gap: 4px; cursor: pointer; }
#stats { margin-left: 20px; font-weight: bold; }
</style>
</head>
<body>
<h1>🐍 Ultimate Snake Game</h1>
<div class="controls">
<button id="startBtn">Start</button>
<button id="pauseBtn" disabled>Pause</button>
<button id="resetBtn" disabled>Reset</button>
<label>Speed: <input id="speedSlider" type="range" min="1" max="15" value="8"></label>
<label><input type="checkbox" id="wrapToggle"> Wrap Walls</label>
<label>Theme:
<select id="themeSelect">
<option value="dark">Dark</option>
<option value="light">Light</option>
</select>
</label>
<div id="stats">Score: 0 | High: 0</div>
</div>
<canvas id="game" width="400" height="400"></canvas>
<script>
(function(){
const canvas = document.getElementById('game'),
ctx = canvas.getContext('2d'),
startBtn = document.getElementById('startBtn'),
pauseBtn = document.getElementById('pauseBtn'),
resetBtn = document.getElementById('resetBtn'),
speedSlider = document.getElementById('speedSlider'),
wrapToggle = document.getElementById('wrapToggle'),
themeSelect = document.getElementById('themeSelect'),
stats = document.getElementById('stats');
let box = 20, cols = canvas.width/box, rows = canvas.height/box;
let snake, food, dir, score, highScore=0, intervalId, speed=8, paused=false, wrap=false;
function init(){
snake = [{x: Math.floor(cols/2), y: Math.floor(rows/2)}];
dir = {x:0, y:0};
placeFood();
score = 0;
paused = false;
wrap = wrapToggle.checked;
speed = +speedSlider.value;
updateStats();
clearInterval(intervalId);
intervalId = setInterval(gameLoop, 1000/speed);
pauseBtn.textContent = 'Pause'; pauseBtn.disabled = false;
resetBtn.disabled = false;
}
function placeFood(){
food = {
x: Math.floor(Math.random()*cols),
y: Math.floor(Math.random()*rows),
color: `hsl(${Math.random()*360},70%,60%)`
};
// ensure not on snake
if(snake.some(seg=>seg.x===food.x&&seg.y===food.y)) placeFood();
}
function updateStats(){
stats.textContent = `Score: ${score} | High: ${highScore}`;
}
function drawGrid(){
ctx.strokeStyle = getComputedStyle(document.body).getPropertyValue('--grid-color');
for(let i=0;i<=cols;i++){
ctx.beginPath();
ctx.moveTo(i*box,0); ctx.lineTo(i*box,canvas.height); ctx.stroke();
}
for(let j=0;j<=rows;j++){
ctx.beginPath();
ctx.moveTo(0,j*box); ctx.lineTo(canvas.width,j*box); ctx.stroke();
}
}
function draw(){
// background
ctx.fillStyle = getComputedStyle(document.body).getPropertyValue('--board-bg');
ctx.fillRect(0,0,canvas.width,canvas.height);
drawGrid();
// draw food
ctx.fillStyle = food.color;
ctx.fillRect(food.x*box, food.y*box, box, box);
// draw snake
snake.forEach((seg,i)=>{
ctx.fillStyle = i===0 ? 'var(--accent)' : `hsl(${i*20},80%,50%)`;
ctx.fillRect(seg.x*box, seg.y*box, box, box);
});
}
function gameOver(){
clearInterval(intervalId);
dir = {x:0,y:0};
pauseBtn.disabled = true;
alert('Game Over! Your score: ' + score);
}
function gameLoop(){
if(paused) return;
// new head
let head = {x: snake[0].x + dir.x, y: snake[0].y + dir.y};
// wall collisions / wrap
if(wrap){
head.x = (head.x + cols) % cols;
head.y = (head.y + rows) % rows;
} else {
if(head.x<0||head.x>=cols||head.y<0||head.y>=rows) return gameOver();
}
// self collision
if(snake.some(seg=>seg.x===head.x && seg.y===head.y)) return gameOver();
snake.unshift(head);
// eat?
if(head.x===food.x && head.y===food.y){
score += Math.floor(speed/2) + 1;
if(score>highScore) highScore=score;
updateStats();
placeFood();
} else {
snake.pop();
}
draw();
}
// Controls
document.addEventListener('keydown', e=>{
const k = e.keyCode;
if(k===37 && dir.x!==1) dir={x:-1,y:0};
if(k===38 && dir.y!==1) dir={x:0,y:-1};
if(k===39 && dir.x!==-1) dir={x:1,y:0};
if(k===40 && dir.y!==-1) dir={x:0,y:1};
});
startBtn.onclick = ()=>{
init();
startBtn.disabled = true;
};
pauseBtn.onclick = ()=>{
paused = !paused;
pauseBtn.textContent = paused ? 'Resume' : 'Pause';
};
resetBtn.onclick = ()=>{
clearInterval(intervalId);
startBtn.disabled = false;
pauseBtn.disabled = true;
resetBtn.disabled = true;
ctx.clearRect(0,0,canvas.width,canvas.height);
score = 0; updateStats();
};
speedSlider.oninput = ()=>{ speed=+speedSlider.value; if(intervalId) {clearInterval(intervalId); intervalId = setInterval(gameLoop,1000/speed);} };
themeSelect.onchange = ()=>{
document.body.className = themeSelect.value==='light' ? 'light' : '';
};
// initial draw
draw();
})();
</script>
</body>
</html>
Features included:
• Responsive canvas grid & smooth animation
• Start / Pause / Resume / Reset buttons
• Adjustable speed slider (1–15)
• “Wrap” walls toggle (teleport at edges)
• Light & Dark themes
• Colorful snake & fruit with HSL variations
• Score & high-score counter
• Collision detection & game‑over alert
Enjoy coding—or customizing—the game further! 🐍🎮