From be4eb906c5e33e204668912d9da4a0fef39e8e88 Mon Sep 17 00:00:00 2001 From: Trianta <56975502+Trimutex@users.noreply.github.com> Date: Mon, 30 Dec 2024 17:10:46 -0600 Subject: [PATCH] pong: make other bot idle when not busy --- src/pong.ts | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/pong.ts b/src/pong.ts index 34c5190..cc1200b 100644 --- a/src/pong.ts +++ b/src/pong.ts @@ -122,8 +122,13 @@ class PongCore { // Simulate game logic simulate() { - this.botInput(this.left); - this.botInput(this.right); + if (this.ball.dx > 0) { + this.botInput(this.left); + this.botIdle(this.right); + } else { + this.botInput(this.right); + this.botIdle(this.left); + } // move paddles by their velocity this.left.geometry.y += this.left.dy; @@ -246,6 +251,22 @@ class PongCore { if (ballRegion > paddleRegion) player.dy = player.speed; if (ballRegion === paddleRegion) player.dy = 0; } + + // Randomly move the bot paddle when ball not towards it + botIdle(player: Paddle) { + let direction: number = Math.floor(Math.random() * 3); + switch (direction) { + case 0: + player.dy = 0; + break; + case 1: + player.dy = -player.speed; + break; + case 2: + player.dy = player.speed; + break; + } + } } // listen to keyboard events to move the paddles