<!DOCTYPE html>
<html>
<head>
<title>Snake</title>
<style>
body{display:flex;justify-content:center;align-items:center;height:100vh;margin:0;background:#222;}
canvas{background:#000;border:2px solid #0f0;}
.score{color:#0f0;font-family:monospace;position:absolute;top:20px;font-size:20px;}
</style>
</head>
<body>
<div class="score">Score: <span id="score">0</span></div>
<canvas id="game"></canvas>
<script>
const c=document.getElementById("game").getContext("2d"),s=document.getElementById("score");
let w=20,h=20,sc=0,x=10,y=10,fx=15,fy=15,dx=0,dy=0,t=[];
function g(){c.fillStyle="#0f0";c.fillRect(x*w,y*h,w,h);c.fillStyle="#f00";c.fillRect(fx*w,fy*h,w,h);}
function u(){x+=dx;y+=dy;if(x<0||y<0||x>=20||y>=20)d();if(x==fx&&y==fy){sc++;s.textContent=sc;fx=Math.floor(Math.random()*20);fy=Math.floor(Math.random()*20);t.push([x,y]);}
c.clearRect(0,0,400,400);t.forEach(e=>{c.fillStyle="#0f0";c.fillRect(e[0]*w,e[1]*h,w,h);});if(t.length>sc){t.shift();}t.push([x,y]);g();}
function d(){x=10;y=10;dx=0;dy=0;sc=0;s.textContent=sc;t=[];}
document.addEventListener("keydown",e=>{if(e.key=="ArrowUp"&&dy!=1){dx=0;dy=-1;}if(e.key=="ArrowDown"&&dy!=-1){dx=0;dy=1;}if(e.key=="ArrowLeft"&&dx!=1){dx=-1;dy=0;}if(e.key=="ArrowRight"&&dx!=-1){dx=1;dy=0;}});
setInterval(u,100);
</script>
</body>
</html>