6 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
Trianta 16242e95ca pong: increase speed of game 2024-12-30 17:03:50 -06:00
Trianta d1d9631ae8 pong: fix left paddle location 2024-12-30 17:03:22 -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() {
@@ -81,13 +83,13 @@ class PongCore {
this.grid = 15; // size of grid squares this.grid = 15; // size of grid squares
this.paddleHeight = this.grid * 5; this.paddleHeight = this.grid * 5;
this.paddleHeightMax = this.canvas.height - this.grid - this.paddleHeight; this.paddleHeightMax = this.canvas.height - this.grid - this.paddleHeight;
this.timeout = 32; // speed in ms this.timeout = 4; // speed in ms
this.width = this.canvas.width; this.width = this.canvas.width;
this.height = this.canvas.height; this.height = this.canvas.height;
this.gameover = false; this.gameover = false;
this.paddleSpeed = 6; this.paddleSpeed = 6;
this.ball = new Ball(new Box(this.width / 2, this.height / 2, this.grid, this.grid)); 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.left = new Paddle(new Box(this.grid * 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); this.right = new Paddle(new Box(this.width - this.grid * 3, this.height / 2 - this.paddleHeight / 2, this.grid, this.paddleHeight), false);
} }
@@ -122,8 +124,13 @@ class PongCore {
// Simulate game logic // Simulate game logic
simulate() { simulate() {
if (this.ball.dx < 0) {
this.botInput(this.left); this.botInput(this.left);
this.botIdle(this.right);
} else {
this.botInput(this.right); 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