xonecas.io
211
Ze

zen / commit a5b8cee

you can now kill aliens, js files need cleanup

xonecas2011-01-30 a5b8ceed6c8c93b503db07ba5a18bb509dbced61 parent 44de4c5

1 file changed

js/script.js+37−8
@@ -18,7 +18,8 @@
1818 canvasHeight = $(window).height(),
1919 center = Math.ceil(canvasWidth / 2),
2020 currentX = center + 10,
21- currentY = canvasHeight - 58;
21+ currentY = canvasHeight - 58,
22+ aliens = [];
2223
2324
2425 $(canvas).attr({
@@ -36,12 +37,36 @@
3637
3738 ctx.drawImage(laser, currentX, currentY, 38, 38);
3839
39- var z = 11, startx = center - 279, consty = 20;
40- while (z--) {
41- ctx.drawImage(alien, startx, consty, 38, 38);
42- startx += 58;
40+ var x = 5, z = 11, startx = center - 279, starty = 20;
41+ while (x--) {
42+ while (z--) {
43+ ctx.drawImage(alien, startx, starty, 38, 38);
44+ aliens.push({ x: startx, y: starty });
45+ startx += 58;
46+ }
47+ z = 11;
48+ startx = center -279;
49+ starty += 58;
4350 }
4451
52+ function isHit (aliens, missile) {
53+ var hit = false,
54+ i = aliens.length;
55+
56+ while (!hit && --i > -1) {
57+ var alien = aliens[i];
58+ if (missile.y >= alien.y && missile.y <= (alien.y +38)) {
59+ if (missile.x >= alien.x && missile.x <= (alien.x +38)) {
60+ if (!alien.isDead) {
61+ hit = { x: alien.x, y: alien.y };
62+ alien.isDead = true;
63+ }
64+ }
65+ }
66+ }
67+
68+ if (hit) return hit;
69+ }
4570
4671 $(document).keydown(function (ev) {
4772 switch(ev.which) {
@@ -77,12 +102,16 @@
77102 ctx.fillStyle = '#fff';
78103 ctx.fillRect(originX, originY, 2, 10);
79104
80- if (originY >= -10)
105+ var hit = isHit(aliens, { x: originX, y: originY });
106+ if (originY >= -10 && !hit)
81107 setTimeout(fly, 30);
108+ else if (hit) {
109+ ctx.fillStyle = '#444';
110+ ctx.fillRect(hit.x, hit.y, 38, 38);
111+ ctx.fillRect(originX, originY, 2, 10);
112+ }
82113 }) ();
83114 break;
84- default:
85- ev.preventDefault();
86115 }
87116
88117 //log(ev.which);