Compare commits
9 Commits
efcc58d0cd
...
pong
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
225e5e8422 | ||
|
|
3aaa6740f4 | ||
|
|
51ec729979 | ||
|
|
be4eb906c5 | ||
|
|
16242e95ca | ||
|
|
d1d9631ae8 | ||
|
|
e2f111dd36 | ||
|
|
43f652f695 | ||
|
|
347c419d59 |
+68
-39
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user