// Snake Game
// [PLAY IN YOUR BROWSER]
//
// IMPORTANT - Configuration:
// Enable Sounds? (Set this to false if game is not loading.)
let enableSounds = true
//
// How to run:
// 1. Copy everything in this token.
// 2. Open developer console using F12 or inspect element.
// 3. Paste everything in the Console.
// 4. Press Enter.
// 5. The game should load in the same token.
//
const aTags=document.getElementsByTagName("p");let par=document.createElement("p");for(let i=0;i<aTags.length;i++)if(aTags[i].textContent.includes("// Snake Game")){par=aTags[i];break}document.body.style.backgroundColor="rgb(0,0,0)",document.body.style.height="100vh";let renderer=document.createElement("canvas"),gameWindow=document.createElement("div");par.parentElement.appendChild(gameWindow),gameWindow.style.width="350px",gameWindow.style.height="350px",par.parentElement.removeChild(par);let textHandler=document.createElement("p");textHandler.style.fontFamily="monospace",textHandler.style.width="350px",textHandler.style.height="350px",textHandler.style.textAlign="center",textHandler.style.position="absolute",textHandler.style.margin="0",textHandler.style.display="flex",textHandler.style.justifyContent="center",textHandler.style.alignContent="center",textHandler.style.alignItems="center",gameWindow.appendChild(textHandler),gameWindow.appendChild(renderer),renderer.scrollIntoView({behavior:"smooth",block:"center"}),setTimeout(()=>{document.body.style.overflowY="hidden"},2500);try{document.getElementsByClassName("header")[0].style.backgroundColor="rgb(0,0,0)",document.getElementsByClassName("page")[0].style.filter="grayscale(0%)"}catch(a){}renderer.width=40,renderer.height=40,renderer.style.imageRendering="pixelated";let colorTheme="#00ff00";textHandler.style.color=colorTheme,renderer.style.backgroundColor=colorTheme,renderer.style.width="350px";const ctx=renderer.getContext("2d");function setPixel(a,b,c,d,e){ctx.fillStyle=rgb(${c},${d},${e}),ctx.fillRect(a,b,1,1)}function hexToRgb(b){var a=/^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(b);return a?{r:parseInt(a[1],16),g:parseInt(a[2],16),b:parseInt(a[3],16)}:null}function resetCanvas(){let a=hexToRgb(colorTheme);for(let b=0;b<renderer.width;b++)for(let c=0;c<renderer.height;c++)setPixel(b,c,a.r,a.g,a.b);for(let d=1;d<renderer.width-1;d++)for(let e=1;e<renderer.height-1;e++)setPixel(d,e,0,0,0)}resetCanvas();let gameState="Menu",menuIndex=0;textHandler.innerHTML="SNAKE GAME
[ENTER] = START
CONTROLS:
[Arrow Keys] to MOVE
[R] to RESTART";let playerDirection="Up",snakeLength=3,snakeX=0,snakeY=0,appleX=-100,appleY=-100,snake=[],score=0,appleSound="",loseSound="",buttonSound="";function loadGame(){function a(){appleX=5+Math.round(Math.random()(renderer.width-10)),appleY=5+Math.round(Math.random()(renderer.height-10)),setPixel(appleX,appleY,255-hexToRgb(colorTheme).r,255-hexToRgb(colorTheme).g,255-hexToRgb(colorTheme).b)}document.body.onkeydown=function(b){if("Enter"==b.key&&"Menu"==gameState){playerDirection="Up",snakeLength=3,snakeX=Math.round(renderer.width/2),snakeY=Math.round(renderer.width/2),snake=[],resetCanvas(),a(),score=0,gameState="Game";let c=document.createElement("audio");c.autoplay=!0,c.volume=.4,c.innerHTML=<source src="${buttonSound}" type="audio/mpeg">,document.body.appendChild(c)}if("r"==b.key){resetCanvas(),gameState="Menu";let d=document.createElement("audio");d.autoplay=!0,d.volume=.3,d.innerHTML=<source src="${buttonSound}" type="audio/mpeg">,document.body.appendChild(d)}"ArrowRight"==b.key&&(playerDirection="Right"),"ArrowLeft"==b.key&&(playerDirection="Left"),"ArrowDown"==b.key&&(playerDirection="Down"),"ArrowUp"==b.key&&(playerDirection="Up")},setInterval(()=>{"Menu"==gameState&&(textHandler.style.display="flex",textHandler.style.justifyContent="center",textHandler.style.alignContent="center",textHandler.style.alignItems="center",(menuIndex+=1)>=2&&(menuIndex=0),textHandler.innerHTML="SNAKE GAME
[ENTER] = START
CONTROLS:
[Arrow Keys] to MOVE
[R] to RESTART",1==menuIndex&&(textHandler.innerHTML="SNAKE GAME
CONTROLS:
[Arrow Keys] to MOVE
[R] to RESTART")),"Lost"==gameState&&(textHandler.style.display="flex",textHandler.style.justifyContent="center",textHandler.style.alignContent="center",textHandler.style.alignItems="center",(menuIndex+=1)>=2&&(menuIndex=0),textHandler.innerHTML="YOU LOST
[R] = RESTART",1==menuIndex&&(textHandler.innerHTML="YOU LOST
"))},500),setInterval(()=>{"Game"==gameState&&function(){if("Right"==playerDirection&&(snakeX+=1),"Left"==playerDirection&&(snakeX-=1),"Down"==playerDirection&&(snakeY+=1),"Up"==playerDirection&&(snakeY-=1),snake.push({x:snakeX,y:snakeY}),ctx.getImageData(snakeX,snakeY,1,1).data[0]>0||ctx.getImageData(snakeX,snakeY,1,1).data[1]>0||ctx.getImageData(snakeX,snakeY,1,1).data[2]>0){if(snakeX==appleX&&snakeY==appleY){snakeLength+=1;let c=document.createElement("audio");c.autoplay=!0,c.innerHTML=<source src="${appleSound}" type="audio/mpeg">,document.body.appendChild(c),a()}else{resetCanvas(),setTimeout(()=>{resetCanvas()},100),gameState="Lost";let d=document.createElement("audio");d.autoplay=!0,d.innerHTML=<source src="${loseSound}" type="audio/mpeg">,document.body.appendChild(d)}}if(setPixel(snakeX,snakeY,hexToRgb(colorTheme).r,hexToRgb(colorTheme).g,hexToRgb(colorTheme).b),snake.length>snakeLength)for(let b=0;b<1;b++)setPixel(snake[b].x,snake[b].y,0,0,0),snake.splice(b,1)}()},125),setInterval(()=>{"Game"==gameState&&(textHandler.style.display="inline",textHandler.innerHTML=<br>SCORE: ${score.toString().slice(0,5)},score+=.05+Math.random()/500)},50)}!0==enableSounds?(textHandler.innerHTML="LOADING SOUNDS FROM IPFS...
(disable sounds in the config
if it's not loading)",fetch("https://ipfs.io/ipfs/QmY8468gQx6tjhMrzdrzvB4U6bqL6S1PkuY4n96jEVdrxA").then(function(a){return a.text()}).then(function(a){console.log(a),appleSound="data:audio/wav;base64,"+a.split(";")[0],loseSound="data:audio/wav;base64,"+a.split(";")[1],buttonSound="data:audio/wav;base64,"+a.split(";")[2],loadGame()}).catch(function(a){})):loadGame()