@@ -10,6 +10,7 @@ class Ship {
10
10
x : 0 ,
11
11
y : 0
12
12
} ;
13
+ this . opacity = 1 ;
13
14
const image = new Image ( ) ;
14
15
image . src = "shipnew.png" ;
15
16
image . onload = ( ) => {
@@ -24,8 +25,11 @@ class Ship {
24
25
} ;
25
26
}
26
27
draw ( ) {
28
+ a . save ( ) ;
29
+ a . globalAlpha = this . opacity
27
30
if ( this . image && this . width && this . height )
28
31
a . drawImage ( this . image , this . position . x , this . position . y , this . width , this . height ) ;
32
+ a . restore ( )
29
33
}
30
34
update ( ) {
31
35
if ( this . image && this . width && this . height )
@@ -60,11 +64,11 @@ class Particle {
60
64
this . velocity = velocity ;
61
65
this . radius = radius ;
62
66
this . color = color ;
63
- this . opacity = 1 ; // Fixed from -1 to 1
67
+ this . opacity = 1 ;
64
68
}
65
69
draw ( ) {
66
70
a . save ( ) ;
67
- a . globalAlpha = this . opacity ; // Fixed a.save() to a.save();
71
+ a . globalAlpha = this . opacity ;
68
72
a . beginPath ( ) ;
69
73
a . arc ( this . position . x , this . position . y , this . radius , 0 , Math . PI * 2 ) ;
70
74
a . fillStyle = this . color ;
@@ -202,6 +206,10 @@ const keys = {
202
206
203
207
let frames = 0 ;
204
208
let ranInterval = Math . floor ( ( Math . random ( ) * 500 ) + 500 ) ;
209
+ let game = {
210
+ over : false ,
211
+ active :true
212
+ }
205
213
function createParticles ( { object, color } ) {
206
214
for ( let k = 0 ; k < 15 ; k ++ ) {
207
215
particles . push ( new Particle ( {
@@ -220,6 +228,7 @@ function createParticles({ object, color }) {
220
228
}
221
229
222
230
function animate ( ) {
231
+ if ( ! game . active ) return
223
232
requestAnimationFrame ( animate ) ;
224
233
a . fillStyle = 'black' ;
225
234
a . fillRect ( 0 , 0 , canvas . width , canvas . height ) ;
@@ -240,7 +249,12 @@ function animate() {
240
249
invaderProjectile . position . x + invaderProjectile . width >= player . position . x &&
241
250
invaderProjectile . position . x <= player . position . x + player . width ) {
242
251
invaderProjectiles . splice ( index , 1 ) ;
252
+ player . opacity = 0 ;
253
+ game . over = true ;
243
254
console . log ( "You lose" ) ;
255
+ setTimeout ( ( ) => {
256
+ game . active = false ;
257
+ } , 2000 ) ;
244
258
createParticles ( {
245
259
object : player ,
246
260
color : 'white'
@@ -320,6 +334,7 @@ function animate() {
320
334
animate ( ) ;
321
335
322
336
window . addEventListener ( "keydown" , ( event ) => {
337
+ if ( game . over ) return
323
338
switch ( event . key ) {
324
339
case 'a' :
325
340
keys . a . pressed = true ;
0 commit comments