@@ -53,24 +53,30 @@ class Projectile {
5353 this . position . y += this . velocity . y ;
5454 }
5555}
56+
5657class Particle {
57- constructor ( { position, velocity, radius, color } ) {
58+ constructor ( { position, velocity, radius, color } ) {
5859 this . position = position ;
5960 this . velocity = velocity ;
6061 this . radius = radius ;
61- this . color = color ;
62+ this . color = color ;
63+ this . opacity = 1 ; // Fixed from -1 to 1
6264 }
6365 draw ( ) {
66+ a . save ( ) ;
67+ a . globalAlpha = this . opacity ; // Fixed a.save() to a.save();
6468 a . beginPath ( ) ;
6569 a . arc ( this . position . x , this . position . y , this . radius , 0 , Math . PI * 2 ) ;
6670 a . fillStyle = this . color ;
6771 a . fill ( ) ;
6872 a . closePath ( ) ;
73+ a . restore ( ) ;
6974 }
7075 update ( ) {
7176 this . draw ( ) ;
7277 this . position . x += this . velocity . x ;
7378 this . position . y += this . velocity . y ;
79+ this . opacity -= 0.01 ;
7480 }
7581}
7682
@@ -149,16 +155,16 @@ class Grid {
149155
150156 const rows = Math . floor ( Math . random ( ) * 5 + 2 ) ;
151157 const columns = Math . floor ( Math . random ( ) * 7 + 5 ) ;
152- const imagewidth = 41 ;
158+ const imageWidth = 41 ;
153159
154- this . width = columns * imagewidth ;
160+ this . width = columns * imageWidth ;
155161
156162 for ( let i = 0 ; i < columns ; i ++ ) {
157163 for ( let j = 0 ; j < rows ; j ++ ) {
158164 this . invaders . push ( new Invader ( {
159165 position : {
160- x : i * imagewidth ,
161- y : j * imagewidth
166+ x : i * imageWidth ,
167+ y : j * imageWidth
162168 }
163169 } ) ) ;
164170 }
@@ -181,7 +187,7 @@ const player = new Ship();
181187const projectiles = [ ] ;
182188const grids = [ ] ;
183189const invaderProjectiles = [ ] ;
184- const particles = [ ] ;
190+ const particles = [ ] ;
185191const keys = {
186192 a : {
187193 pressed : false
@@ -196,24 +202,49 @@ const keys = {
196202
197203let frames = 0 ;
198204let ranInterval = Math . floor ( ( Math . random ( ) * 500 ) + 500 ) ;
205+ function createParticles ( { object, color } ) {
206+ for ( let k = 0 ; k < 15 ; k ++ ) {
207+ particles . push ( new Particle ( {
208+ position : {
209+ x : object . position . x + object . width / 2 ,
210+ y : object . position . y + object . height / 2
211+ } ,
212+ velocity : {
213+ x : ( Math . random ( ) - 0.5 ) * 3 ,
214+ y : ( Math . random ( ) - 0.5 ) * 3
215+ } ,
216+ radius : Math . random ( ) * 3 ,
217+ color : color || '#BAA0DE'
218+ } ) ) ;
219+ }
220+ }
199221
200222function animate ( ) {
201223 requestAnimationFrame ( animate ) ;
202224 a . fillStyle = 'black' ;
203225 a . fillRect ( 0 , 0 , canvas . width , canvas . height ) ;
204226 player . update ( ) ;
205- particles . forEach ( particle => {
206- particle . update ( ) ;
207- } )
227+ particles . forEach ( ( particle , i ) => {
228+ if ( particle . opacity <= 0 ) {
229+ particles . splice ( i , 1 ) ;
230+ } else {
231+ particle . update ( ) ;
232+ }
233+ } ) ;
208234 invaderProjectiles . forEach ( ( invaderProjectile , index ) => {
209235 if ( invaderProjectile . position . y + invaderProjectile . height >= canvas . height ) {
210236 invaderProjectiles . splice ( index , 1 ) ;
211237 } else {
212238 invaderProjectile . update ( ) ;
213- if ( invaderProjectile . position . y + invaderProjectile . height >= player . position . y &&
214- invaderProjectile . position . x + invaderProjectile . width >= player . position . x &&
239+ if ( invaderProjectile . position . y + invaderProjectile . height >= player . position . y &&
240+ invaderProjectile . position . x + invaderProjectile . width >= player . position . x &&
215241 invaderProjectile . position . x <= player . position . x + player . width ) {
242+ invaderProjectiles . splice ( index , 1 ) ;
216243 console . log ( "You lose" ) ;
244+ createParticles ( {
245+ object : player ,
246+ color : 'white'
247+ } ) ;
217248 }
218249 }
219250 } ) ;
@@ -236,47 +267,36 @@ function animate() {
236267
237268 grids . forEach ( ( grid , gridIndex ) => {
238269 grid . update ( ) ;
239-
270+
240271 if ( frames % 100 === 0 && grid . invaders . length > 0 ) {
241272 grid . invaders [ Math . floor ( Math . random ( ) * grid . invaders . length ) ] . shoot ( invaderProjectiles ) ;
242273 }
243-
274+
244275 grid . invaders . forEach ( ( invader , i ) => {
245276 invader . update ( { velocity : grid . velocity } ) ;
246-
277+
247278 projectiles . forEach ( ( projectile , j ) => {
248279 if (
249280 projectile . position . y - projectile . radius <= invader . position . y + invader . height &&
250281 projectile . position . x + projectile . radius >= invader . position . x &&
251282 projectile . position . x - projectile . radius <= invader . position . x + invader . width &&
252283 projectile . position . y + projectile . radius >= invader . position . y
253284 ) {
254- for ( let k = 0 ; k < 15 ; k ++ ) {
255- particles . push ( new Particle ( {
256- position : {
257- x : invader . position . x + invader . width / 2 ,
258- y : invader . position . y + invader . height / 2
259- } ,
260- velocity : {
261- x : ( Math . random ( ) - 0.5 ) * 3 ,
262- y : ( Math . random ( ) - 0.50 ) * 3
263- } ,
264- radius : Math . random ( ) * 3 ,
265- color : '#BAA0DE'
266- } ) ) ;
267- }
268-
269285 const invaderFound = grid . invaders . find ( invader2 => invader2 === invader ) ;
270286 const projectileFound = projectiles . find ( projectile2 => projectile2 === projectile ) ;
271-
287+
272288 if ( invaderFound && projectileFound ) {
289+ createParticles ( {
290+ object : invader
291+ } ) ;
292+
273293 grid . invaders . splice ( i , 1 ) ;
274294 projectiles . splice ( j , 1 ) ;
275-
295+
276296 if ( grid . invaders . length > 0 ) {
277297 const firstInvader = grid . invaders [ 0 ] ;
278298 const lastInvader = grid . invaders [ grid . invaders . length - 1 ] ;
279-
299+
280300 grid . width = lastInvader . position . x - firstInvader . position . x + lastInvader . width ;
281301 grid . position . x = firstInvader . position . x ;
282302 } else {
@@ -287,7 +307,7 @@ function animate() {
287307 } ) ;
288308 } ) ;
289309 } ) ;
290-
310+
291311 if ( frames % ranInterval == 0 ) {
292312 grids . push ( new Grid ( ) ) ;
293313 ranInterval = Math . floor ( ( Math . random ( ) * 500 ) + 500 ) ;
0 commit comments