@@ -10,6 +10,7 @@ class Ship {
1010 x : 0 ,
1111 y : 0
1212 } ;
13+ this . opacity = 1 ;
1314 const image = new Image ( ) ;
1415 image . src = "shipnew.png" ;
1516 image . onload = ( ) => {
@@ -24,8 +25,11 @@ class Ship {
2425 } ;
2526 }
2627 draw ( ) {
28+ a . save ( ) ;
29+ a . globalAlpha = this . opacity
2730 if ( this . image && this . width && this . height )
2831 a . drawImage ( this . image , this . position . x , this . position . y , this . width , this . height ) ;
32+ a . restore ( )
2933 }
3034 update ( ) {
3135 if ( this . image && this . width && this . height )
@@ -60,11 +64,11 @@ class Particle {
6064 this . velocity = velocity ;
6165 this . radius = radius ;
6266 this . color = color ;
63- this . opacity = 1 ; // Fixed from -1 to 1
67+ this . opacity = 1 ;
6468 }
6569 draw ( ) {
6670 a . save ( ) ;
67- a . globalAlpha = this . opacity ; // Fixed a.save() to a.save();
71+ a . globalAlpha = this . opacity ;
6872 a . beginPath ( ) ;
6973 a . arc ( this . position . x , this . position . y , this . radius , 0 , Math . PI * 2 ) ;
7074 a . fillStyle = this . color ;
@@ -202,6 +206,10 @@ const keys = {
202206
203207let frames = 0 ;
204208let ranInterval = Math . floor ( ( Math . random ( ) * 500 ) + 500 ) ;
209+ let game = {
210+ over : false ,
211+ active :true
212+ }
205213function createParticles ( { object, color } ) {
206214 for ( let k = 0 ; k < 15 ; k ++ ) {
207215 particles . push ( new Particle ( {
@@ -220,6 +228,7 @@ function createParticles({ object, color }) {
220228}
221229
222230function animate ( ) {
231+ if ( ! game . active ) return
223232 requestAnimationFrame ( animate ) ;
224233 a . fillStyle = 'black' ;
225234 a . fillRect ( 0 , 0 , canvas . width , canvas . height ) ;
@@ -240,7 +249,12 @@ function animate() {
240249 invaderProjectile . position . x + invaderProjectile . width >= player . position . x &&
241250 invaderProjectile . position . x <= player . position . x + player . width ) {
242251 invaderProjectiles . splice ( index , 1 ) ;
252+ player . opacity = 0 ;
253+ game . over = true ;
243254 console . log ( "You lose" ) ;
255+ setTimeout ( ( ) => {
256+ game . active = false ;
257+ } , 2000 ) ;
244258 createParticles ( {
245259 object : player ,
246260 color : 'white'
@@ -320,6 +334,7 @@ function animate() {
320334animate ( ) ;
321335
322336window . addEventListener ( "keydown" , ( event ) => {
337+ if ( game . over ) return
323338 switch ( event . key ) {
324339 case 'a' :
325340 keys . a . pressed = true ;
0 commit comments