add: ball respawn timeout

This commit is contained in:
AdrienLSH 2023-12-12 18:25:16 +01:00
parent 754e5867f2
commit 63e1520e6a

View File

@ -86,6 +86,7 @@ class Game {
}
];
this.ballStartSide = 0;
this.ballRespawned = false;
this.ball = new Ball(this.context, this.def, this.ballStartSide);
this.interval = setInterval(this.updateGame.bind(this), 10);
@ -140,8 +141,11 @@ class Game {
this.ball.vy *= -1;
else if (this.ball.y + this.ball.radius >= this.canvas.height)
this.ball.vy *= -1;
this.ball.x += this.ball.vx;
this.ball.y += this.ball.vy;
if (!this.ballRespawned) {
this.ball.x += this.ball.vx;
this.ball.y += this.ball.vy;
}
this.clear();
this.players[0].paddle.update();
@ -161,6 +165,9 @@ class Game {
this.scoresDisplay.innerHTML = `Scores: ${p1Score} - ${p2Score}`;
this.ballStartSide = 1 - this.ballStartSide;
this.ball = new Ball(this.context, this.def, this.ballStartSide);
this.ballRespawned = true;
new Promise(r => setTimeout(r, 300))
.then(_ => this.ballRespawned = false);
}
}