Created
August 24, 2018 20:31
-
-
Save remixer-dec/95ff53000b00981851a37c1634111c5a to your computer and use it in GitHub Desktop.
500B JS Snake
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Public Domain |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>500B Snake (JS ES6)</title> | |
<style> | |
#R{ | |
width: 144px;margin: 0 auto;font-size: 20px; | |
} | |
#mobilecontrols{ | |
display:grid; | |
grid-template:'a a' 60px 'b c' 64px 'd d' 60px; | |
} | |
</style> | |
</head> | |
<body> | |
<pre id='R'></pre> | |
<p id="mobilecontrols"> | |
<button style="grid-area: a" onclick="window.onkeydown({keyCode:38})">^</button> | |
<button style="grid-area: b" onclick="window.onkeydown({keyCode:37})"><</button> | |
<button style="grid-area: c" onclick="window.onkeydown({keyCode:39})">></button> | |
<button style="grid-area: d" onclick="window.onkeydown({keyCode:40})">V</button> | |
</p> | |
<center>if you're on PC, you can use arrow keys</center> | |
<script src="./snake.min.js"></script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
I=_=>{p=d=39,T=[25],f=0,O=e=>-1!=T.indexOf(e),F()},E=11,g=_=>Math.floor(99*Math.random()+E),F=_=>{while(O(f)||0==f%E)f=g()},I(),K=(c,e)=>d=d==c?e:d,r=_=>{for(H=T[0],v='',T[0]==f?T.push(f)&F():0,K(37,-1),K(39,1),K(38,-E),K(40,E),d=d==-1*p?p:d,O(H+d)||12>H||110<H||0==H%E?I():T.unshift(H+d)&T.pop(),i=0;122>i;i++)v+=0==i%E||E>i||110<i?'#':O(i)?'o':i==f?'$':' ',v+=0==i%E&&0<i&&121>i?'\n#':'';v+=T.length-1,R.innerHTML=v,p=d,89==T.length?alert('U won')&I():0},onkeydown=c=>d=c.keyCode,setInterval(r,400) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Live demo
Full explanation: