Skip to content

Instantly share code, notes, and snippets.

@maalos
Last active November 3, 2023 15:45
Show Gist options
  • Save maalos/2e9fe8e4acb48ec691dc757cc53e7d49 to your computer and use it in GitHub Desktop.
Save maalos/2e9fe8e4acb48ec691dc757cc53e7d49 to your computer and use it in GitHub Desktop.
smallest snake game in js (498 characters)
<canvas id=g width=256 height=256 style="border:solid"><script>[c,j,s,z,h]=[g.getContext("2d"),16,[[8,8]]];a=s[0];onkeydown=({key:e})=>[z,h]={d:[z||1,0],a:[z||-1,0],s:[0,h||1],w:[0,h||-1]}[e];setInterval(()=>{if(s.unshift([s[0][0]+z&15,s[0][1]+h&15]),""+s[0]==a)with(Math)for(r=random(),f=floor(j*r);a=[f,f],s.some(e=>""+e==a););else s.slice(1).some(e=>""+e==s[0])?s.splice(1):s.pop();c.clearRect(0,0,256,256);c.fillRect(j*a[0],j*a[1],j,j);s.forEach(([e,l])=>c.fillRect(j*e,j*l,j,j))},125)</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment