From 347c419d59770f16d6594350e5a3468f0f6a8aa6 Mon Sep 17 00:00:00 2001 From: Trianta <56975502+Trimutex@users.noreply.github.com> Date: Mon, 30 Dec 2024 16:43:27 -0600 Subject: [PATCH] pong: fix wack definition order --- src/pong.ts | 65 +++++++++++++++++++++++++++-------------------------- 1 file changed, 33 insertions(+), 32 deletions(-) diff --git a/src/pong.ts b/src/pong.ts index ef2a2cf..0985041 100644 --- a/src/pong.ts +++ b/src/pong.ts @@ -19,6 +19,25 @@ class Box { } } +class Ball { + geometry: Box; + speed: number; + dx: number; + dy: number; + resetting: boolean; + + constructor(_geometry: Box) { + // start in the middle of the game + this.geometry = _geometry; + this.speed = 5; + // ball velocity (start going to the top-right corner) + this.dx = this.speed; + this.dy = -this.speed; + // keep track of when need to reset the ball position + this.resetting = false; + } +} + class Paddle { geometry: Box; speed: number; @@ -39,35 +58,6 @@ class Paddle { scored() { this.score++; } - - input() { - if (!this.isBot) - return; - let ballRegion = Math.floor(g_pong.ball.geometry.y / 15); - let paddleRegion = Math.floor(this.geometry.y / 15); - if (ballRegion < paddleRegion) this.dy = -this.speed; - if (ballRegion > paddleRegion) this.dy = this.speed; - if (ballRegion === paddleRegion) this.dy = 0; - } -} - -class Ball { - geometry: Box; - speed: number; - dx: number; - dy: number; - resetting: boolean; - - constructor() { - // start in the middle of the game - this.geometry = new Box(g_pong.width / 2, g_pong.height / 2, g_pong.grid, g_pong.grid); - this.speed = 5; - // ball velocity (start going to the top-right corner) - this.dx = this.speed; - this.dy = -this.speed; - // keep track of when need to reset the ball position - this.resetting = false; - } } class PongCore { @@ -96,7 +86,7 @@ class PongCore { this.height = this.canvas.height; this.gameover = false; this.paddleSpeed = 6; - this.ball = new Ball(); + this.ball = new Ball(new Box(this.width / 2, this.height / 2, this.grid, this.grid)); this.left = new Paddle(new Box(this.width / 2, this.height / 2 - this.paddleHeight / 2, this.grid, this.paddleHeight), true); this.right = new Paddle(new Box(this.width - this.grid * 3, this.height / 2 - this.paddleHeight / 2, this.grid, this.paddleHeight), false); } @@ -132,8 +122,8 @@ class PongCore { // Simulate game logic simulate() { - this.left.input(); - this.right.input(); + this.botInput(this.left); + this.botInput(this.right); // move paddles by their velocity this.left.geometry.y += this.left.dy; @@ -245,6 +235,17 @@ class PongCore { document.getElementById("leftPaddle").innerHTML = this.left.geometry.y.toString(); document.getElementById("rightPaddle").innerHTML = this.right.geometry.y.toString(); } + + // Function for bot to play as a paddle + botInput(player: Paddle) { + if (!player.isBot) + return; + let ballRegion = Math.floor(this.ball.geometry.y / 15); + let paddleRegion = Math.floor(player.geometry.y / 15); + if (ballRegion < paddleRegion) player.dy = -player.speed; + if (ballRegion > paddleRegion) player.dy = player.speed; + if (ballRegion === paddleRegion) player.dy = 0; + } } // listen to keyboard events to move the paddles