Compare commits
4 Commits
16242e95ca
...
pong
| Author | SHA1 | Date | |
|---|---|---|---|
| 225e5e8422 | |||
| 3aaa6740f4 | |||
| 51ec729979 | |||
| be4eb906c5 |
+30
-2
@@ -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() {
|
||||||
@@ -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