pong: fix wack definition order
This commit is contained in:
parent
efcc58d0cd
commit
347c419d59
65
src/pong.ts
65
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
|
||||
|
Loading…
Reference in New Issue
Block a user