function(a,b,c,d,e){a.unshift(b);c^a[0]&&a.pop();
for(b=d*d;b--;)e=[e]+"■ "[!~a.indexOf(b)&b!=c]+["\n"[b%d]];
return~a.indexOf(a[0],1)||e} (function(){
var size = 30;
var oldstep, step = -1;
document.onkeydown = function(e) {
var keyCode = (e || window.event).keyCode,
nextstep = [1,size,-1,-size][keyCode-37]; // Bind arrows to change snake direction
step = (nextstep == -oldstep) ? oldstep : nextstep; // Don't enable to turn Pi/2
}
var center, apple = center = size*(size*.5+.5); // Init apple to middle of level
var f,snakie = (f=function(c,i){return i?f(c,--i).concat(c):[]})(center,5); // Fancy fn to create snakie = [center,center,..] with length = 5
(function() {
var oldlength = snakie.length, next = (snakie[0]+(oldstep=step)+size*size)%(size*size),
game = snake(snakie, next, apple, size);
if (typeof game === "number") { // Snake hit himself: Print Game Over centered
document.getElementById( "snake" ).innerHTML += (f=function(i){return i?f(--i)+" ":""})(size*.5-5) + "Game Over";
} else {
document.getElementById( "snake" ).innerHTML = game; // Print the level
setTimeout(arguments.callee, 100); // loop the game
}
if (snakie.length !== oldlength) {
while (~snakie.indexOf(apple)) {
apple = Math.floor(Math.random()*size*size); // reposition apple, if it was consumed
}
}
})();
})();The main logic to move blocks, detect collision, assign new blocks, remove full lines and render the layout are included. Excluded are keyboard controls and the final rendering.Again, it's an exercise in reduction, and with as confining a constraint as 140 bytes that's going to mean reducing a complicated thing like a game more aggressively than may be possible if your focus is on a working interactive demo rather than a distillation of the core game logic as an end in itself.
« Older MI6 intends to use the 1994 Intelligence Services ... | Peter Sellers enjoyed doing sp... Newer »
This thread has been archived and is closed to new comments
posted by empath at 8:00 AM on February 15