Created
September 9, 2015 15:47
-
-
Save vilicvane/6327b97dfc4d9d78730d to your computer and use it in GitHub Desktop.
Snake game written on Nokia 6120c in 2009
This file contains hidden or 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
<body></body> | |
<script> | |
try{ | |
var w=20, h=20; | |
var s=10; | |
var t=2500; | |
var sl=5; | |
var lx=1, ly=0; | |
var ax=1, ay=0; | |
var hx=0, hy=0; | |
var snk=[]; | |
var ap; | |
var main=document.createElement("div"); | |
main.style.width=w*s+"px"; | |
main.style.height=h*s+"px"; | |
main.style.border="solid 1px Black"; | |
main.style.position="relative"; | |
main.style.overflow="hidden"; | |
var scoreBox=document.createElement("div"); | |
scoreBox.innerHTML="<span>Score: </span><span>0</span>"; | |
var score=scoreBox.childNodes[1]; | |
document.body.appendChild(main); | |
document.body.appendChild(scoreBox); | |
var map=[]; | |
for(var i=0;i<w;i++) | |
{ | |
var ys=map[i]=[]; | |
for(var j=0;j<h;j++) | |
ys[j]=false; | |
} | |
function create(x, y, isApple) | |
{ | |
var d=document.createElement("div"); | |
d.style.height=d.style.width=s+"px"; | |
d.style.position="absolute"; | |
d.setPos=function(x, y) | |
{ | |
if(d.x>=0 && d.y>=0) | |
map[d.x][d.y]=false; | |
if(x>=0 && y>=0) | |
map[x][y]=true; | |
d.x=x; | |
d.y=y; | |
d.style.left=x*s+"px"; | |
d.style.top=y*s+"px"; | |
}; | |
d.setMode=function(isApple) | |
{ | |
d.style.backgroundColor=isApple?"Red":"Black"; | |
} | |
d.setPos(x, y); | |
d.setMode(isApple); | |
main.appendChild(d); | |
return d; | |
} | |
function cApple() | |
{ | |
var x, y; | |
do | |
{ | |
x=rnd(w); | |
y=rnd(h); | |
} | |
while(map[x][y]); | |
ap=create(x, y, true); | |
} | |
function rnd(size) | |
{ | |
return Math.floor(Math.random()*size); | |
} | |
for(var i=0;i<sl;i++) | |
{ | |
snk.push(create(-1, 0)); | |
} | |
document.onkeypress=function(e) | |
{ | |
var c=e.keyCode; | |
switch(c) | |
{ | |
case 50: | |
case 119: | |
turn(0,-1);break; | |
case 52: | |
case 97: | |
turn(-1,0);break; | |
case 54: | |
case 100: | |
turn(1,0);break; | |
case 56: | |
case 115: | |
turn(0,1);break; | |
default:break; | |
} | |
return false; | |
}; | |
function turn(x, y) | |
{ | |
if(x && !ax) | |
{ | |
lx=x; | |
ly=0; | |
} | |
else if(y && !ay) | |
{ | |
lx=0; | |
ly=y; | |
} | |
} | |
function paint() | |
{ | |
ax=lx; | |
ay=ly; | |
hx+=lx; | |
hy+=ly; | |
if(hx==w)hx=0; | |
else if(hx==-1)hx=w-1; | |
else if(hy==h)hy=0; | |
else if(hy==-1)hy=h-1; | |
var hd; | |
if(hx==ap.x && hy==ap.y) | |
{ | |
hd=ap; | |
hd.setMode(false); | |
snk.push(hd); | |
score.innerHTML=snk.length-sl; | |
cApple(); | |
setTimeout(paint, t/snk.length); | |
} | |
else | |
{ | |
hd=snk.shift(); | |
snk.push(hd); | |
if((hd.x==hx && hd.y==hy) || !map[hx][hy]) | |
{ | |
hd.setPos(hx, hy); | |
setTimeout(paint, t/snk.length); | |
} | |
else | |
{ | |
alert("Game Over!\nScore: "+(snk.length-sl)); | |
window.location.reload(); | |
} | |
} | |
} | |
alert("Press \"OK\" to start."); | |
setTimeout(paint, t/snk.length); | |
cApple(); | |
}catch(e){alert(e);} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment