|
| 1 | + |
| 2 | +var canvas, ctx, |
| 3 | + leftDown, rightDown; |
| 4 | + |
| 5 | +var playerShip, |
| 6 | + invaders, |
| 7 | + bullets, particles; |
| 8 | + |
| 9 | +var screenHeight, screenWidth; |
| 10 | + |
| 11 | +init(); |
| 12 | + |
| 13 | +function init() { |
| 14 | + |
| 15 | + screenWidth = window.innerWidth; |
| 16 | + screenHeight = window.innerHeight; |
| 17 | + |
| 18 | + canvas = document.createElement('canvas'); |
| 19 | + document.body.appendChild(canvas); |
| 20 | + canvas.width = screenWidth; |
| 21 | + canvas.height = screenHeight; |
| 22 | + ctx = canvas.getContext('2d'); |
| 23 | + |
| 24 | + window.addEventListener('keydown', keyDown); |
| 25 | + window.addEventListener('keyup', keyUp); |
| 26 | + |
| 27 | + playerShip = new PlayerShip(screenWidth/2,screenHeight-80); |
| 28 | + |
| 29 | + bullets = []; |
| 30 | + particles = []; |
| 31 | + resetInvaders(); |
| 32 | + |
| 33 | + setInterval(loop, 1000/50); |
| 34 | + |
| 35 | +} |
| 36 | + |
| 37 | +function loop() { |
| 38 | + ctx.clearRect(0,0,screenWidth, screenHeight); |
| 39 | + checkKeys(); |
| 40 | + |
| 41 | + updateBullets(); |
| 42 | + |
| 43 | + checkCollisions(); |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | + renderBullets(); |
| 48 | + renderInvaders(); |
| 49 | + updateParticles(); |
| 50 | + playerShip.render(ctx); |
| 51 | +} |
| 52 | + |
| 53 | +function checkCollisions() { |
| 54 | + |
| 55 | + |
| 56 | + for(var i = 0; i<bullets.length; i++) { |
| 57 | + var bullet = bullets[i]; |
| 58 | + |
| 59 | + for(var j = 0; j<invaders.length; j++) { |
| 60 | + var invader = invaders[j]; |
| 61 | + |
| 62 | + if( (bullet.x > invader.x) && (bullet.x < invader.x + invader.width) |
| 63 | + && (bullet.y >invader.y) && (bullet.y < invader.y +invader.height)) { |
| 64 | + |
| 65 | + invaders.splice(j, 1); |
| 66 | + j--; |
| 67 | + bullets.splice(i,1); |
| 68 | + i--; |
| 69 | + |
| 70 | + makeExplosion(invader.x +invader.width/2, invader.y+invader.height/2) |
| 71 | + |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | + } |
| 77 | + |
| 78 | + } |
| 79 | + |
| 80 | + |
| 81 | +} |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | +function resetInvaders() { |
| 86 | + invaders = []; |
| 87 | + |
| 88 | + for(var col = 0; col <10; col++) { |
| 89 | + for(var row = 0; row<4; row++ ) { |
| 90 | + var invader = new Invader(col * 80, row*60); |
| 91 | + invaders.push(invader); |
| 92 | + |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | +} |
| 97 | + |
| 98 | +function updateBullets() { |
| 99 | + for(var i =0; i<bullets.length; i++) { |
| 100 | + |
| 101 | + bullets[i].update(); |
| 102 | + } |
| 103 | + |
| 104 | + |
| 105 | +} |
| 106 | +function renderBullets() { |
| 107 | + for(var i =0; i<bullets.length; i++) { |
| 108 | + |
| 109 | + bullets[i].render(ctx); |
| 110 | + } |
| 111 | + |
| 112 | + |
| 113 | +} |
| 114 | + |
| 115 | + |
| 116 | +function updateParticles() { |
| 117 | + for(var i =0; i<particles.length; i++) { |
| 118 | + particles[i].update(); |
| 119 | + particles[i].render(ctx); |
| 120 | + } |
| 121 | + |
| 122 | + |
| 123 | +} |
| 124 | + |
| 125 | + |
| 126 | +function renderInvaders() { |
| 127 | + for(var i =0; i<invaders.length; i++) { |
| 128 | + |
| 129 | + invaders[i].render(ctx); |
| 130 | + } |
| 131 | + |
| 132 | + |
| 133 | +} |
| 134 | + |
| 135 | +function checkKeys() { |
| 136 | + |
| 137 | + if(leftDown) { |
| 138 | + playerShip.x -=10; |
| 139 | + } else if(rightDown) { |
| 140 | + playerShip.x +=10; |
| 141 | + } |
| 142 | + |
| 143 | +} |
| 144 | + |
| 145 | +function keyDown(e) { |
| 146 | + |
| 147 | + if(e.keyCode == 37) { |
| 148 | + leftDown = true; |
| 149 | + } else if (e.keyCode == 39) { |
| 150 | + rightDown = true; |
| 151 | + } |
| 152 | + |
| 153 | + if(e.keyCode == 32) { |
| 154 | + var bullet = new Bullet(playerShip.x + playerShip.width/2, playerShip.y); |
| 155 | + bullets.push(bullet); |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +function keyUp(e) { |
| 160 | + |
| 161 | + if(e.keyCode == 37) { |
| 162 | + leftDown = false; |
| 163 | + } else if (e.keyCode == 39) { |
| 164 | + rightDown = false; |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | + |
| 169 | +function PlayerShip(x, y) { |
| 170 | + |
| 171 | + this.x = x; |
| 172 | + this.y = y; |
| 173 | + this.width = 60; |
| 174 | + this.height = 40; |
| 175 | + |
| 176 | + this.render = function (ctx) { |
| 177 | + ctx.fillStyle = 'white'; |
| 178 | + ctx.fillRect(this.x, this.y, this.width, this.height); |
| 179 | + } |
| 180 | + |
| 181 | + |
| 182 | + |
| 183 | +} |
| 184 | + |
| 185 | + |
| 186 | +function Bullet(x, y) { |
| 187 | + |
| 188 | + this.x = x; |
| 189 | + this.y = y; |
| 190 | + |
| 191 | + this.update = function () { |
| 192 | + this.y -=20; |
| 193 | + |
| 194 | + |
| 195 | + } |
| 196 | + this.render = function (ctx) { |
| 197 | + ctx.fillStyle = 'white'; |
| 198 | + ctx.fillRect(this.x, this.y, 4,10); |
| 199 | + } |
| 200 | + |
| 201 | + |
| 202 | + |
| 203 | +} |
| 204 | + |
| 205 | + |
| 206 | +function Invader(x, y) { |
| 207 | + |
| 208 | + this.x = x; |
| 209 | + this.y = y; |
| 210 | + this.width = 60; |
| 211 | + this.height = 40; |
| 212 | + |
| 213 | + this.render = function (ctx) { |
| 214 | + ctx.fillStyle = 'red'; |
| 215 | + ctx.fillRect(this.x, this.y, this.width, this.height); |
| 216 | + } |
| 217 | + |
| 218 | +} |
| 219 | +function Particle(x, y) { |
| 220 | + |
| 221 | + this.x = x; |
| 222 | + this.y = y; |
| 223 | + this.velX = Math.random()* 20 -10; |
| 224 | + this.velY = Math.random()* 20 -10; |
| 225 | + this.size = 10; |
| 226 | + this.update = function() { |
| 227 | + this.x += this.velX; |
| 228 | + this.y += this.velY; |
| 229 | + |
| 230 | + } |
| 231 | + this.render = function (ctx) { |
| 232 | + ctx.fillStyle = 'red'; |
| 233 | + ctx.fillRect(this.x, this.y, this.size, this.size); |
| 234 | + } |
| 235 | + |
| 236 | +} |
| 237 | + |
| 238 | +function makeExplosion(x, y) { |
| 239 | + for(var i = 0; i<100; i++ ) { |
| 240 | + |
| 241 | + var p = new Particle(x,y); |
| 242 | + particles.push(p); |
| 243 | + |
| 244 | + |
| 245 | + |
| 246 | + } |
| 247 | + |
| 248 | + |
| 249 | +} |
| 250 | + |
| 251 | + |
| 252 | + |
| 253 | + |
| 254 | + |
| 255 | + |
| 256 | + |
0 commit comments