9 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
Trianta
e2f111dd36 pong: fix undefined function 2024-12-30 17:00:50 -06:00
Trianta
43f652f695 pong: there's no way this works 2024-12-30 16:54:01 -06:00
Trianta
347c419d59 pong: fix wack definition order 2024-12-30 16:43:27 -06:00
+68 -39
View File
@@ -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;
@@ -26,6 +45,7 @@ class Paddle {
isLeft: boolean;
score: number;
dy: number;
idleCounter: number;
constructor(_geometry: Box, _isLeft: boolean) {
this.geometry = _geometry;
@@ -34,40 +54,12 @@ class Paddle {
this.isBot = true;
this.score = 0;
this.dy = 0;
this.idleCounter = 0;
}
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 {
@@ -91,13 +83,13 @@ class PongCore {
this.grid = 15; // size of grid squares
this.paddleHeight = this.grid * 5;
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.height = this.canvas.height;
this.gameover = false;
this.paddleSpeed = 6;
this.ball = new Ball();
this.left = new Paddle(new Box(this.width / 2, this.height / 2 - this.paddleHeight / 2, this.grid, this.paddleHeight), true);
this.ball = new Ball(new Box(this.width / 2, this.height / 2, this.grid, this.grid));
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);
}
@@ -121,19 +113,24 @@ class PongCore {
// Game loop
loop() {
// Reset of needed
if (this.gameover)
this.reset();
if (g_pong.gameover)
g_pong.reset();
this.simulate();
g_pong.simulate();
this.draw();
this.updatePageText();
g_pong.draw();
g_pong.updatePageText();
}
// Simulate game logic
simulate() {
this.left.input();
this.right.input();
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;
@@ -245,6 +242,38 @@ 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;
}
// 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