Added extra bot autoplay
This commit is contained in:
parent
62cd1a60b8
commit
9f95d3e977
25
scripts.js
25
scripts.js
@ -70,15 +70,30 @@ function restartGame() {
|
|||||||
requestAnimationFrame(loop);
|
requestAnimationFrame(loop);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function BotMovesPaddle(paddle) {
|
||||||
|
ballRegion = Math.floor(ball.y / 15);
|
||||||
|
paddleRegion = Math.floor(paddle.y / 15);
|
||||||
|
if (ballRegion < paddleRegion) paddle.dy = -paddleSpeed;
|
||||||
|
if (ballRegion > paddleRegion) paddle.dy = paddleSpeed;
|
||||||
|
if (ballRegion === paddleRegion) paddle.dy = 0;
|
||||||
|
}
|
||||||
|
|
||||||
// game loop
|
// game loop
|
||||||
function loop() {
|
function loop() {
|
||||||
if (leftScore < 7 && rightScore < 7) {
|
//if (leftScore < 7 && rightScore < 7) {
|
||||||
requestAnimationFrame(loop);
|
requestAnimationFrame(loop);
|
||||||
} else if (leftScore >= 7 || rightScore >= 7) {
|
//} else if (leftScore >= 7 || rightScore >= 7) {
|
||||||
gameOver();
|
// gameOver();
|
||||||
}
|
//}
|
||||||
context.clearRect(0,0,canvas.width,canvas.height);
|
context.clearRect(0,0,canvas.width,canvas.height);
|
||||||
|
|
||||||
|
if (ball.dx < 0) {
|
||||||
|
BotMovesPaddle(leftPaddle);
|
||||||
|
}
|
||||||
|
if (ball.dx > 0) {
|
||||||
|
BotMovesPaddle(rightPaddle);
|
||||||
|
}
|
||||||
|
|
||||||
// move paddles by their velocity
|
// move paddles by their velocity
|
||||||
leftPaddle.y += leftPaddle.dy;
|
leftPaddle.y += leftPaddle.dy;
|
||||||
rightPaddle.y += rightPaddle.dy;
|
rightPaddle.y += rightPaddle.dy;
|
||||||
@ -139,6 +154,7 @@ function loop() {
|
|||||||
// check to see if ball collides with paddle. if they do change x velocity
|
// check to see if ball collides with paddle. if they do change x velocity
|
||||||
if (collides(ball, leftPaddle)) {
|
if (collides(ball, leftPaddle)) {
|
||||||
ball.dx *= -1;
|
ball.dx *= -1;
|
||||||
|
leftPaddle.dy = 0;
|
||||||
|
|
||||||
// move ball next to the paddle otherwise the collision will happen again
|
// move ball next to the paddle otherwise the collision will happen again
|
||||||
// in the next frame
|
// in the next frame
|
||||||
@ -146,6 +162,7 @@ function loop() {
|
|||||||
}
|
}
|
||||||
else if (collides(ball, rightPaddle)) {
|
else if (collides(ball, rightPaddle)) {
|
||||||
ball.dx *= -1;
|
ball.dx *= -1;
|
||||||
|
rightPaddle.dy = 0;
|
||||||
|
|
||||||
// move ball next to the paddle otherwise the collision will happen again
|
// move ball next to the paddle otherwise the collision will happen again
|
||||||
// in the next frame
|
// in the next frame
|
||||||
|
Loading…
Reference in New Issue
Block a user