@@ -53,24 +53,30 @@ class Projectile {
53
53
this . position . y += this . velocity . y ;
54
54
}
55
55
}
56
+
56
57
class Particle {
57
- constructor ( { position, velocity, radius, color } ) {
58
+ constructor ( { position, velocity, radius, color } ) {
58
59
this . position = position ;
59
60
this . velocity = velocity ;
60
61
this . radius = radius ;
61
- this . color = color ;
62
+ this . color = color ;
63
+ this . opacity = 1 ; // Fixed from -1 to 1
62
64
}
63
65
draw ( ) {
66
+ a . save ( ) ;
67
+ a . globalAlpha = this . opacity ; // Fixed a.save() to a.save();
64
68
a . beginPath ( ) ;
65
69
a . arc ( this . position . x , this . position . y , this . radius , 0 , Math . PI * 2 ) ;
66
70
a . fillStyle = this . color ;
67
71
a . fill ( ) ;
68
72
a . closePath ( ) ;
73
+ a . restore ( ) ;
69
74
}
70
75
update ( ) {
71
76
this . draw ( ) ;
72
77
this . position . x += this . velocity . x ;
73
78
this . position . y += this . velocity . y ;
79
+ this . opacity -= 0.01 ;
74
80
}
75
81
}
76
82
@@ -149,16 +155,16 @@ class Grid {
149
155
150
156
const rows = Math . floor ( Math . random ( ) * 5 + 2 ) ;
151
157
const columns = Math . floor ( Math . random ( ) * 7 + 5 ) ;
152
- const imagewidth = 41 ;
158
+ const imageWidth = 41 ;
153
159
154
- this . width = columns * imagewidth ;
160
+ this . width = columns * imageWidth ;
155
161
156
162
for ( let i = 0 ; i < columns ; i ++ ) {
157
163
for ( let j = 0 ; j < rows ; j ++ ) {
158
164
this . invaders . push ( new Invader ( {
159
165
position : {
160
- x : i * imagewidth ,
161
- y : j * imagewidth
166
+ x : i * imageWidth ,
167
+ y : j * imageWidth
162
168
}
163
169
} ) ) ;
164
170
}
@@ -181,7 +187,7 @@ const player = new Ship();
181
187
const projectiles = [ ] ;
182
188
const grids = [ ] ;
183
189
const invaderProjectiles = [ ] ;
184
- const particles = [ ] ;
190
+ const particles = [ ] ;
185
191
const keys = {
186
192
a : {
187
193
pressed : false
@@ -196,24 +202,49 @@ const keys = {
196
202
197
203
let frames = 0 ;
198
204
let 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
+ }
199
221
200
222
function animate ( ) {
201
223
requestAnimationFrame ( animate ) ;
202
224
a . fillStyle = 'black' ;
203
225
a . fillRect ( 0 , 0 , canvas . width , canvas . height ) ;
204
226
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
+ } ) ;
208
234
invaderProjectiles . forEach ( ( invaderProjectile , index ) => {
209
235
if ( invaderProjectile . position . y + invaderProjectile . height >= canvas . height ) {
210
236
invaderProjectiles . splice ( index , 1 ) ;
211
237
} else {
212
238
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 &&
215
241
invaderProjectile . position . x <= player . position . x + player . width ) {
242
+ invaderProjectiles . splice ( index , 1 ) ;
216
243
console . log ( "You lose" ) ;
244
+ createParticles ( {
245
+ object : player ,
246
+ color : 'white'
247
+ } ) ;
217
248
}
218
249
}
219
250
} ) ;
@@ -236,47 +267,36 @@ function animate() {
236
267
237
268
grids . forEach ( ( grid , gridIndex ) => {
238
269
grid . update ( ) ;
239
-
270
+
240
271
if ( frames % 100 === 0 && grid . invaders . length > 0 ) {
241
272
grid . invaders [ Math . floor ( Math . random ( ) * grid . invaders . length ) ] . shoot ( invaderProjectiles ) ;
242
273
}
243
-
274
+
244
275
grid . invaders . forEach ( ( invader , i ) => {
245
276
invader . update ( { velocity : grid . velocity } ) ;
246
-
277
+
247
278
projectiles . forEach ( ( projectile , j ) => {
248
279
if (
249
280
projectile . position . y - projectile . radius <= invader . position . y + invader . height &&
250
281
projectile . position . x + projectile . radius >= invader . position . x &&
251
282
projectile . position . x - projectile . radius <= invader . position . x + invader . width &&
252
283
projectile . position . y + projectile . radius >= invader . position . y
253
284
) {
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
-
269
285
const invaderFound = grid . invaders . find ( invader2 => invader2 === invader ) ;
270
286
const projectileFound = projectiles . find ( projectile2 => projectile2 === projectile ) ;
271
-
287
+
272
288
if ( invaderFound && projectileFound ) {
289
+ createParticles ( {
290
+ object : invader
291
+ } ) ;
292
+
273
293
grid . invaders . splice ( i , 1 ) ;
274
294
projectiles . splice ( j , 1 ) ;
275
-
295
+
276
296
if ( grid . invaders . length > 0 ) {
277
297
const firstInvader = grid . invaders [ 0 ] ;
278
298
const lastInvader = grid . invaders [ grid . invaders . length - 1 ] ;
279
-
299
+
280
300
grid . width = lastInvader . position . x - firstInvader . position . x + lastInvader . width ;
281
301
grid . position . x = firstInvader . position . x ;
282
302
} else {
@@ -287,7 +307,7 @@ function animate() {
287
307
} ) ;
288
308
} ) ;
289
309
} ) ;
290
-
310
+
291
311
if ( frames % ranInterval == 0 ) {
292
312
grids . push ( new Grid ( ) ) ;
293
313
ranInterval = Math . floor ( ( Math . random ( ) * 500 ) + 500 ) ;
0 commit comments