var w,h,el,g,my={};var snake;function snakeMain(mode){my.version='0.64';my.typ=typeof mode!=='undefined'?mode:'bla';my.hdrHt=40
w=Math.min(500,window.innerWidth-20,window.innerHeight-my.hdrHt)
h=w
my.xN=20
my.sz=w/my.xN
my.wmax=Math.floor(w/my.sz)-1
my.hmax=Math.floor(h/my.sz)-1
my.hz=10
my.millisecs=1000/my.hz
my.bgClr='hsla(0,0%,15%,1)'
my.edgeTypes=['right','left','wrap','bounce','reset']
my.edgeType=my.edgeTypes[0];my.bgs=['black','coords']
my.food=new Pt(0,0)
my.prevFood=new Pt(0,0)
my.soundQ=true
my.hiScore=0
console.log('my',my)
my.dummy=[playToggle]
var s='';my.sndHome=(document.domain=='localhost')?'/mathsisfun/images/sounds/':'/images/sounds/'
s+=' ';s+=' ';my.snds=[];s+='
';s+='
';s+='
Press Play to Start
'
s+='';s+='
Options ';s+=' '
s+=playHTML(36)
s+='
';s+='
';s+=' ';s+='
';s+=optPopHTML()
s+='
© 2019 MathsIsFun.com v'+my.version+'
';s+='
'
document.write(s);el=document.getElementById('canvas1');var ratio=3;el.width=w*ratio;el.height=h*ratio;el.style.width=w+"px";el.style.height=h+"px";g=el.getContext("2d");g.setTransform(ratio,0,0,ratio,0,0);this.clrs=[["Blue",'#0000FF'],["Red",'#FF0000'],["Black",'#000000'],["Green",'#00cc00'],["Orange",'#FFA500'],["Slate Blue",'#6A5ACD'],["Lime",'#00FF00'],["Spring Green",'#00FF7F'],["Teal",'#008080'],["Gold",'#ffd700'],["Med Purple",'#aa00aa'],["Light Blue",'#ADD8E6'],["Navy",'#000080'],["Purple",'#800080'],["Dark SeaGreen",'#8FBC8F']];console.log("my.typ",my.typ);window.addEventListener("keydown",key,false);el.addEventListener("touchstart",touchStart,false)
el.addEventListener("mousedown",mouseDown,false)
snake=new Snake();foodLocn()
radioPress('edge',0)
my.playQ=false
playToggle()
playToggle()}
function anim(){if(my.playQ){draw()
requestAnimationFrame(anim);}}
function gameNew(){draw()}
function key(ev){var keyCode=ev.keyCode;console.log("key",keyCode);switch(keyCode){case 37:case 65:case 100:case 52:snake.dir(-1,0)
ev.preventDefault();break;case 39:case 68:case 102:case 54:snake.dir(1,0)
ev.preventDefault();break;case 38:case 87:case 104:case 56:snake.dir(0,-1)
ev.preventDefault();break;case 40:case 83:case 98:case 50:snake.dir(0,1)
ev.preventDefault();break;case 32:playToggle()
ev.preventDefault();break;default:}}
function touchStart(ev){var touch=ev.targetTouches[0];ev.clientX=touch.clientX;ev.clientY=touch.clientY;ev.touchQ=true;mouseDown(ev)}
function mouseDown(ev){var bRect=el.getBoundingClientRect();var mousex=ev.clientX-bRect.left
var mousey=ev.clientY-bRect.top
var dx=mousex-(snake.x+0.5)*my.sz
var dy=mousey-(snake.y+0.5)*my.sz
if(Math.abs(dx)>Math.abs(dy)){snake.dir(dx/Math.abs(dx),0)}else{snake.dir(0,dy/Math.abs(dy))}}
function foodLocn(){my.prevFood={x:my.food.x,y:my.food.y}
do{my.food=new Pt(randomInt(0,my.wmax),randomInt(0,my.hmax));}while(my.food.x==my.prevFood.x||my.food.y==my.prevFood.y)
console.log('food',my.sz,my.food)}
function randomInt(min,max){return Math.floor(Math.random()*(max-min+1))+min;}
function Pt(x,y){this.x=x;this.y=y;return this;}
function draw(){g.clearRect(0,0,g.canvas.width,g.canvas.height)
g.fillStyle=my.bgClr
g.beginPath()
g.rect(0,0,g.canvas.width,g.canvas.height);g.fill();g.fillStyle='rgba(255,255,255,0.02)'
for(var i=0;i