Skip to content

Commit 095ea50

Browse files
committed
Game stops as we lose
1 parent 8ac149c commit 095ea50

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

game.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

203207
let frames = 0;
204208
let ranInterval = Math.floor((Math.random() * 500) + 500);
209+
let game ={
210+
over: false,
211+
active:true
212+
}
205213
function 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

222230
function 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() {
320334
animate();
321335

322336
window.addEventListener("keydown", (event) => {
337+
if (game.over) return
323338
switch (event.key) {
324339
case 'a':
325340
keys.a.pressed = true;

index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
</head>
1010
<body>
1111
<canvas></canvas>
12+
<p><span>Score:</span><span>0</span></p>
1213

1314
</body>
1415
</html>

style.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
body{
22
margin: 0;
33
overflow:hidden;
4+
}
5+
p{
6+
position: fixed;
7+
z-index: 10;
8+
color: white;
49
}

0 commit comments

Comments
 (0)