Current directory: /home/klas4s23/domains/585455.klas4s23.mid-ica.nl/public_html/Gastenboek/uploads
const canvas = document.getElementById('pongCanvas');
const context = canvas.getContext('2d');
const player = {
x: 0,
y: canvas.height / 2 - 50,
width: 10,
height: 100,
score: 0,
winCount: 0, // New property to track win count
speed: 0
};
const robot = {
x: canvas.width - 10,
y: canvas.height / 2 - 50,
width: 10,
height: 100,
score: 0,
winCount: 0, // New property to track win count
};
const ball = {
x: canvas.width / 2,
y: canvas.height / 2,
radius: 10,
speedX: 5,
speedY: 5,
speed: 5
};
const line = {
x: canvas.width / 2 - 1,
};
function drawRect(x, y, width, height, color) {
context.fillStyle = color;
context.fillRect(x, y, width, height);
}
function drawCircle(x, y, radius, color) {
context.fillStyle = color;
context.beginPath();
context.arc(x, y, radius, 0, Math.PI*2, false);
context.closePath();
context.fill();
}
function drawScore(text, x, y) {
context.fillStyle = '#FFF';
context.font = '20px Arial';
context.fillText(text, x, y);
}
function resetBall() {
ball.x = canvas.width / 2;
ball.y = canvas.height / 2;
ball.speedX = -ball.speedX;
ball.speed = 5;
}
function collisionDetection(paddle, ball) {
paddle.top = paddle.y;
paddle.bottom = paddle.y + paddle.height;
paddle.left = paddle.x;
paddle.right = paddle.x + paddle.width;
ball.top = ball.y - ball.radius;
ball.bottom = ball.y + ball.radius;
ball.left = ball.x - ball.radius;
ball.right = ball.x + ball.radius;
return paddle.right > ball.left && paddle.top < ball.bottom && paddle.left < ball.right && paddle.bottom > ball.top;
}
function resetGame() {
player.score = 0;
robot.score = 0;
resetBall();
}
function update() {
ball.x += ball.speedX;
ball.y += ball.speedY;
if (ball.y - ball.radius < 0 || ball.y + ball.radius > canvas.height) {
ball.speedY = -ball.speedY;
}
// Player movement
player.y += player.speed;
if(player.y < 0) player.y = 0;
if(player.y + player.height > canvas.height) player.y = canvas.height - player.height;
let playerOrRobot = (ball.x < canvas.width / 2) ? player : robot;
if (collisionDetection(playerOrRobot, ball)) {
let collidePoint = ball.y - (playerOrRobot.y + playerOrRobot.height / 2);
collidePoint = collidePoint / (playerOrRobot.height / 2);
let angleRad = (Math.PI / 4) * collidePoint;
let direction = (ball.x < canvas.width / 2) ? 1 : -1;
ball.speedX = direction * ball.speed * Math.cos(angleRad);
ball.speedY = ball.speed * Math.sin(angleRad);
ball.speed += 0.2;
}
// Score Update
if (ball.x - ball.radius < 0) {
robot.score++;
if(robot.score == 5) {
robot.winCount++;
alert("Robot wins!");
resetGame();
} else {
resetBall();
}
} else if (ball.x + ball.radius > canvas.width) {
player.score++;
if(player.score == 5) {
player.winCount++;
alert("Player wins!");
resetGame();
} else {
resetBall();
}
}
// Robot movement
robot.y += ((ball.y - (robot.y + robot.height / 2))) * 0.1;
if(robot.y < 0) robot.y = 0;
if(robot.y + robot.height > canvas.height) robot.y = canvas.height - robot.height;
}
function drawDashedLine(pattern) {
context.beginPath();
context.setLineDash(pattern);
context.moveTo(canvas.width / 2, 0);
context.lineTo(canvas.width / 2, canvas.height);
context.strokeStyle = '#FFF';
context.stroke();
context.closePath();
context.setLineDash([]);
}
function draw() {
drawRect(0, 0, canvas.width, canvas.height, '#000');
drawRect(player.x, player.y, player.width, player.height, '#FFF');
drawRect(robot.x, robot.y, robot.width, robot.height, '#FFF');
drawCircle(ball.x, ball.y, ball.radius, '#FFF');
drawScore(player.score, canvas.width / 4, canvas.height / 5);
drawScore(robot.score, 3 * canvas.width / 4, canvas.height / 5);
drawDashedLine([10, 5]);
drawScore("Player Wins: " + player.winCount, 10, 25);
drawScore("Robot Wins: " + robot.winCount, canvas.width - 130, 25);
}
function game() {
update();
draw();
}
setInterval(game, 1000/60);
document.addEventListener('keydown', function(event) {
if (event.key === "ArrowUp") player.speed = -7;
if (event.key === "ArrowDown") player.speed = 7;
});
document.addEventListener('keyup', function(event) {
if (event.key === "ArrowUp" || event.key === "ArrowDown") player.speed = 0;
});