4 Commits

Author SHA1 Message Date
Trianta 225e5e8422 pong: reset counter on idle and increase idle length 2024-12-30 17:15:41 -06:00
Trianta 3aaa6740f4 pong: let bot idle last longer 2024-12-30 17:14:00 -06:00
Trianta 51ec729979 pong: fix which bot should idle 2024-12-30 17:11:37 -06:00
Trianta be4eb906c5 pong: make other bot idle when not busy 2024-12-30 17:10:46 -06:00
+30 -2
View File
@@ -45,6 +45,7 @@ class Paddle {
isLeft: boolean; isLeft: boolean;
score: number; score: number;
dy: number; dy: number;
idleCounter: number;
constructor(_geometry: Box, _isLeft: boolean) { constructor(_geometry: Box, _isLeft: boolean) {
this.geometry = _geometry; this.geometry = _geometry;
@@ -53,6 +54,7 @@ class Paddle {
this.isBot = true; this.isBot = true;
this.score = 0; this.score = 0;
this.dy = 0; this.dy = 0;
this.idleCounter = 0;
} }
scored() { scored() {
@@ -122,8 +124,13 @@ class PongCore {
// Simulate game logic // Simulate game logic
simulate() { simulate() {
this.botInput(this.left); if (this.ball.dx < 0) {
this.botInput(this.right); this.botInput(this.left);
this.botIdle(this.right);
} else {
this.botInput(this.right);
this.botIdle(this.left);
}
// move paddles by their velocity // move paddles by their velocity
this.left.geometry.y += this.left.dy; this.left.geometry.y += this.left.dy;
@@ -246,6 +253,27 @@ class PongCore {
if (ballRegion > paddleRegion) player.dy = player.speed; if (ballRegion > paddleRegion) player.dy = player.speed;
if (ballRegion === paddleRegion) player.dy = 0; if (ballRegion === paddleRegion) player.dy = 0;
} }
// Randomly move the bot paddle when ball not towards it
botIdle(player: Paddle) {
player.idleCounter++;
if (player.idleCounter < 20)
return;
else
player.idleCounter = 0;
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 // listen to keyboard events to move the paddles