From 63e1520e6af153666e110c1d6cc76c4f712d82f1 Mon Sep 17 00:00:00 2001 From: AdrienLSH Date: Tue, 12 Dec 2023 18:25:16 +0100 Subject: [PATCH] add: ball respawn timeout --- frontend/static/js/views/Game.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/frontend/static/js/views/Game.js b/frontend/static/js/views/Game.js index 57b936e..87dd0cb 100644 --- a/frontend/static/js/views/Game.js +++ b/frontend/static/js/views/Game.js @@ -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); } }