Compare commits
6 Commits
e2f111dd36
...
pong
| Author | SHA1 | Date | |
|---|---|---|---|
| 225e5e8422 | |||
| 3aaa6740f4 | |||
| 51ec729979 | |||
| be4eb906c5 | |||
| 16242e95ca | |||
| d1d9631ae8 |
+32
-4
@@ -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() {
|
||||||
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
|
||||||
|
|||||||
Reference in New Issue
Block a user