p=document;j=[];g=1;F=256;e=d=F/2;i=[];k=[];m=s=h=0;o=Math;t=o.sqrt;q=o.floor;r=o.random;a.a=a.fillRect;C="#000";S="fillStyle";L="length";P="splice";p.onkeyup=p.onkeydown=function(u){j[u.keyCode]=!!u.type[5]};c.width=c.height=F;setInterval('a[S]="#eee";a.a(0,0,F,F);a[S]=C;if(j[38]){e-=2;g=0;h=-1}if(j[40]){e+=2;g=0;h=1}if(j[37]){d-=2;g=-1;h=0}if(j[39]){d+=2;g=1;h=0}j[32]&&m%3==0&&k.push({x:d,y:e,c:g,b:h});a:for(n=k[L];n--;){b=k[n];f=i[L];b.x+=b.c*3;for(b.y+=b.b*3;f--;){l=i[f];if(t((b.x-l.x)*(b.x-l.x)+(b.y-l.y)*(b.y-l.y))<5){i[P](f,1);k[P](n,1);s+=1;break a}}a.a(b.x-1,b.y-1,2,2);if(b.x<1||b.x>255||b.y<1||b.y>255)k[P](n,1)}if(m%32==0){b=q(r()*257);f=q(r()*4);X=f==0||f==2?b:f==1?F:0;Y=f==1||f==3?b:f*128;i.push({x:X,y:Y})}for(n=i[L];n--;){b=i[n];if(t((b.x-d)*(b.x-d)+(b.y-e)*(b.y-e))<8){i=[];k=[];s=0;d=e=128;break}b.x+=(b.x>d?-1:1)/2;b.y+=(b.y>e?-1:1)/2;a.a(b.x-3,b.y-3,6,6)}a.a(d-6,e-6,12,12);g!=0&&a.a(d+(g<0?-10:0),e-1,9,2);h!=0&&a.a(d,e+(h<0?-9:1),2,9);a.fillText(s,8,14);++m',33)
/*
* Survival of the lost pixel
* CAUNTION!!! the game source and compressed version are different
*/
(function (){
function Enemy(x,y){return {x: x, y: y};}
function Bullet(x,y,dx,dy){return {x: x, y: y, z:dx,t:dy};}
var d = document, Keys = [], UP=38,DOWN=40,LEFT=37,RIGHT=39,SPACE=32,
//character
direction_x = 1,
direction_y = 0,
x = 128,
y = 128,
characterSpeed = 2,
bulletSpeed = 3,
enemies = [],
bullets = [],
score = 0,
//frames count
Frame = 0,
//Colors
GroundColor = '#928920'/* || '#eee'*/,
CharacterColor = '#8dc63f'/* || '#0f0'*/,
EnemyColor = '#1a5036'/* || '#00f'*/,
BulletColor = '#fffb82'/* || '#ff0'*/,
GunColor = '#000',
//Math shorts
M = Math,
Sqrt = M.sqrt,
Floor = M.floor,
Random = M.random;
d.onkeyup=d.onkeydown=function (e){Keys[e.keyCode]=!!e.type[5];};
//setting up screen
c.width=c.height=256;
setInterval(function (){
//clearing canvas
a.fillStyle = GroundColor;
a.fillRect(0,0,256,256);
if (Keys[UP]){
y -= characterSpeed;
direction_x = 0;
direction_y = -1;
}
if (Keys[DOWN]){
y += characterSpeed;
direction_x = 0;
direction_y = 1;
}
if (Keys[LEFT]){
x -= characterSpeed;
direction_x = -1;
direction_y = 0;
}
if (Keys[RIGHT]){
x += characterSpeed;
direction_x = 1;
direction_y = 0;
}
if (Keys[SPACE] && Frame % 3 == 0)
bullets.push(Bullet(x,y,direction_x,direction_y));
n = bullets.length;
//rendering bullets and checking collisions
Collide:
while (n--){
var u = bullets[n],
e = enemies.length;
u.x += u.z * bulletSpeed;
u.y += u.t * bulletSpeed;
while (e--){
var m = enemies[e];
if (Math.sqrt((u.x-m.x)*(u.x-m.x)+(u.y-m.y)*(u.y-m.y)) < 5){
enemies.splice(e, 1);
bullets.splice(n, 1);
score += 1;
break Collide;
}
}
a.fillStyle = BulletColor;
a.fillRect(u.x-1,u.y-1,2,2);
//removing bullet if it out the screen
if (u.x<1 || u.x>255 || u.y<1 || u.y>255)
bullets.splice(n,1);
}
//adding new enemy
if (Frame%64 == 0) {
var ra = Floor(Random()*257),
rb = Floor(Random()*4);
ex = rb == 0 || rb == 2 ? ra : rb == 1 ? 256 : 0;
ey = rb == 1 || rb == 3 ? ra : rb * 128;
enemies.push(Enemy(ex,ey));
}
//rendering enemies
n = enemies.length;
while (n--){
var u = enemies[n];
//if you collide with enemy you die!
if (Sqrt((u.x-x)*(u.x-x)+(u.y-y)*(u.y-y)) < 8){
enemies = [];
bullets = [];
x=y=128;
break;
}
//enemy logic
u.x+=(u.x>x?-1:1)/2;
u.y+=(u.y>y?-1:1)/2;
a.fillStyle = EnemyColor;
a.fillRect(u.x-3,u.y-3,6,6);
}
//drawing character
a.fillStyle = CharacterColor;
a.fillRect(x-6,y-6,12,12);
//drawing gun and character
a.fillStyle = GunColor;
if (direction_x<0) a.fillRect(x-10,y-1,4,2);
if (direction_y<0) a.fillRect(x,y-9,2,3);
if (direction_x>0) a.fillRect(x,y-1,9,2);
if (direction_y>0) a.fillRect(x,y+1,2,9);
++Frame;
}, 33);
})();