var d,e=window,f=[];function g(b){return Math.floor(Math.random()*(b-0)+0)}var h=[g(608),g(544)],j=9,k=0,o=592,m=0,n=9999;function p(b,l,c,r,s){a.beginPath();a.rect(b,l,c,r);a.fillStyle=s;a.fill()}function q(b,l,c){a.fillStyle="#fff";a.a(b,l,c)}a.a=a.fillText;a.canvas.width=a.canvas.height=640;e.onkeydown=function(b){32===b.keyCode&&f.push([k+16-3,o])};e.onmousemove=function(b){k=b.pageX;0>=k&&(k=0);608<=k&&(k=608)};p(0,0,640,640,"#987");q("GO",32,32);e.onclick=function(){this.onclick=null;d=e.setInterval(function(){a.clearRect(0,0,640,640);p(0,0,640,640,"#987");p(h[0],h[1],32,32,"#6"+j+j);for(var b=0;b<f.length;b++)if(f[b][1]-=2,0<=f[b][1]){var l=f[b][0]>h[0]-6&&f[b][0]<h[0]+32,c=h[0];f[b][1]<=h[1]+32&&l?(f.splice(b,1),0<j?(j--,c=5<=g(9)?c-48:c+48,0>c&&(c+=96),608<c&&(c-=96),h[0]=c):(m++,h=[g(608),g(544)],j=9),p(h[0],h[1],32,32,"#6"+j+j)):p(f[b][0],f[b][1],6,6,"#dd0")}else f.splice(b,1);p(k,o,32,32,"#dd0");n--;q("T: "+n/100,0,9);q("K: "+m,0,22);n||(e.clearInterval(d),q("HIT F5",320,320))},1)}
/* global a */
var M,
W = window,
S = 640, // window size
s = 32, // size
t = 22, // tempo
B = [], // bullet
k = 9, // bullet size
r = function (i, j) { // generate random number in range i to j
'use strict';
return Math.floor((Math.random() * (j - i)) + i);
},
N = function () { // generate new enemy
'use strict';
return [r(0, S - s), r(0, S - (s * 3))];
},
E = N(), // Enemy
l = 9, // Enemy life
O = 0, // Player y
o = S - (s * 1.5), // Player y
K = 0, // KILLS
T = 9999, // Time to count down
d = function (x, y, w, h, c) { // draw rectangle
'use strict';
a.beginPath();
a.rect(x, y, w, h);
a.fillStyle = c;
a.fill();
},
w = function (t, x, y) {
'use strict';
a.fillStyle = '#fff';
a.t(t, x, y);
};
a.t = a.fillText;
a.canvas.width = a.canvas.height = S;
// bindings
W.onkeydown = function (e) {
'use strict';
// fire, use var s, 'cause its - 1byte
if (e.keyCode === s) {
B.push([O + (s / 2) - (k / 2), o]);
}
};
W.onmousemove = function (e) {
'use strict';
// player position
O = e.pageX;
// collision
if (O <= 0) {
O = 0;
}
if (O >= S - s) {
O = S - s;
}
};
// background
d(0, 0, S, S, '#987');
w('GO', s, s);
W.onclick = function () {
'use strict';
this.onclick = null;
// main();
M = W.setInterval(function () {
a.clearRect(0, 0, S, S);
// draw map
d(0, 0, S, S, '#987');
// enemy
d(E[0], E[1], s, s, '#00' + l);
// bullets
for (var i = 0; i < B.length; i++) {
B[i][1] -= 2;
// if not end of the field
if (B[i][1] >= 0) {
var A = B[i][1] <= E[1] + s,
Z = B[i][0] > E[0] - k && B[i][0] < E[0] + s,
u = E[0],
m = s * 3;
// enemy collision
if (A && Z) {
B.splice(i, 1);
if (l > 0) {
l--;
if (r(0, 9) >= 5) {
u -= m;
} else {
u += m;
}
if (u < 0) {
u += m;
}
if (u > (S - s)) {
u -= m;
}
E[0] = u;
} else {
K++;
E = N();
l = 9;
}
d(E[0], E[1], s, s, '#00' + l);
} else {
// draw bullet
d(B[i][0], B[i][1], k, k, '#cc3');
}
} else {
B.splice(i, 1);
}
}
// draw player
d(O, o, s, s, '#ff0');
// count time down
T--;
// draw infos
w('T: ' + T/100, 0, 9);
w('K: ' + K, 0, t);
// if time is over
if (!T) {
W.clearInterval(M);
w('HIT F5', S / 2, S / 2);
}
}, 1);
};