Skip to content

Commit

Permalink
e
Browse files Browse the repository at this point in the history
  • Loading branch information
EhabElasam committed Apr 25, 2024
1 parent cc78f19 commit de7f929
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 53 deletions.
19 changes: 3 additions & 16 deletions srcs/frontend/js/playerai1.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,28 +184,18 @@ function showPlayerAi1Page() {
async function update() {
if (gameOver || isGamePaused) return;

const paddleSpeed = 8; //---------------
const cpuReactionBuffer = 40;//----
const paddleSpeed = 8;
const cpuReactionBuffer = 40;

// if (wPressed && player1.y > 0) player1.y -= 8;
// if (sPressed && (player1.y + player1.height) < canvas.height) player1.y += 8;

if (wPressed && player1.y > 0) player1.y -= paddleSpeed;
if (sPressed && (player1.y + player1.height) < canvas.height) player1.y += paddleSpeed;

// let cpuSpeed = 0.2;
// CPU.y += (ball.y - (CPU.y + CPU.height / 2)) * cpuSpeed;
// CPU.y = Math.max(Math.min(CPU.y, canvas.height - CPU.height), 0);

//----
if (Math.abs(ball.y - (CPU.y + CPU.height / 2)) > cpuReactionBuffer) {
let cpuDirection = ball.y < CPU.y + CPU.height / 2 ? -1 : 1;
CPU.y += cpuDirection * paddleSpeed;
}
CPU.y = Math.max(Math.min(CPU.y, canvas.height - CPU.height), 0);
//-------------



ball.x += ball.velocityX;
ball.y += ball.velocityY;

Expand Down Expand Up @@ -240,7 +230,6 @@ function showPlayerAi1Page() {
}
showGameOverModal('CPU');
} else {
//resetBall();
resetGame();
}
} else if (ball.x + ball.radius > canvas.width) {
Expand Down Expand Up @@ -268,7 +257,6 @@ function showPlayerAi1Page() {
}
showGameOverModal('player1');
} else {
//resetBall();
resetGame();
}
}
Expand All @@ -280,7 +268,6 @@ function showPlayerAi1Page() {
handlePaddleBallCollision(CPU, ball);
}
}


function resetBall() {
ball.x = canvas.width / 2;
Expand Down
8 changes: 0 additions & 8 deletions srcs/frontend/js/pong3.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,14 +275,6 @@ function showPong3() {
if (collisionDetect(player2, ball)) handlePaddleBallCollision(player2, ball);
}


// function resetBall() {
// ball.x = canvas.width / 2;
// ball.y = canvas.height / 2;
// ball.velocityX = (Math.random() > 0.5 ? 1 : -1) * ball.speed;
// ball.velocityY = (Math.random() * 2 - 1) * ball.speed;
// ball.speed = 7;
// }
function resetBall() {
ball.x = canvas.width / 2;
ball.y = canvas.height / 2;
Expand Down
29 changes: 0 additions & 29 deletions srcs/frontend/js/pong4.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,28 +232,6 @@ function showPong4() {
}
}

async function showGameOverModal2(loser) {
ctx.fillStyle = "white";
ctx.font = "48px Arial";
ctx.textAlign = "center";
let lost = await translateKey("lost");
ctx.fillText(`${loser} `+lost, canvas.width / 2, canvas.height / 2);
const nGButton2 = document.getElementById('nGButton');
if (nGButton2)
document.getElementById('nGButton').style.display = 'block';
nGButton();
}

function showGameOver() {
ctx.fillStyle = "rgba(0, 0, 0, 0.7)";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = "white";
ctx.font = "48px Arial";
ctx.textAlign = "center";
ctx.fillText(gameOverMessage, canvas.width / 2, canvas.height / 2 - 100);

}

async function showGameOverModal(loser) {
ctx.fillStyle = "rgba(0, 0, 0, 0.7)";
ctx.fillRect(0, 0, canvas.width, canvas.height);
Expand Down Expand Up @@ -349,13 +327,6 @@ function showPong4() {
}
}

// function resetBall() {
// ball.x = canvas.width / 2;
// ball.y = canvas.height / 2;
// ball.velocityX = (Math.random() > 0.5 ? 1 : -1) * ball.speed;
// ball.velocityY = (Math.random() * 2 - 1) * ball.speed;
// ball.speed = 7;
// }
function resetBall() {
ball.x = canvas.width / 2;
ball.y = canvas.height / 2;
Expand Down

0 comments on commit de7f929

Please sign in to comment.