Também tem o problema de cada colisão com a borda gerar 3 pontos, em vez de um só.
O mais estranho é que o código das raquetes parece estar certo (?). Tanto que até a raquete da esquerda aparece.
https://editor.p5js.org/459garlic/sketches/5wqLwGSRe
//your racket
let xRacket = 5;
let yRacket = 150;
let racketLength = 10;
let racketHeight = 90;
let racketSpeed = 10;
//foe racket
let xfoeRacket = 595;
let yfoeRacket = 150;
let foeRacketspeed;
//collision library
let hit = false;
//ball info
let xBall = 300;
let yBall = 200;
let ballDiameter = 15;
let ballRadius = ballDiameter / 2;
//ball movement
let ballMovespeedx = 2;
let ballMovespeedy = 2;
//score
let myPoints = 0;
let foePoints = 0;
function setup() {
createCanvas(600, 400);
}
function draw() {
background(0);
ballVisibility(0);
ballMovement(0);
ballCollision(0);
foeRacketmovement(0);
racketVisibility(xRacket, yRacket);
racketVisibility(xfoeRacket, yfoeRacket);
racketMovement(0);
//racketCollision(0);
racketCollidelib(xRacket, yRacket);
racketCollidelib(xfoeRacket, yfoeRacket);
showScore(0);
getScore(0);
}
function ballVisibility() {
circle(xBall, yBall, ballDiameter);
}
function ballMovement() {
xBall += ballMovespeedx;
yBall += ballMovespeedy;
}
function ballCollision() {
if (xBall + ballRadius > width || xBall - ballRadius < 0) {
ballMovespeedx *= -1;
}
if (yBall + ballRadius > height || yBall - ballRadius < 0) {
ballMovespeedy *= -1;
}
}
function racketVisibility(x, y) {
rect(xRacket, yRacket, racketLength, racketHeight);
}
function racketMovement() {
if (keyIsDown(UP_ARROW)) {
yRacket -= racketSpeed;
}
if (keyIsDown(DOWN_ARROW)) {
yRacket += racketSpeed;
}
}
function foeRacketmovement() {
foeRacketspeed = yBall - yfoeRacket;
yfoeRacket += foeRacketspeed;
}
//function racketCollision() {
// if (
// xBall - ballRadius < xRacket + racketLength &&
// yBall - ballRadius < yRacket + racketHeight &&
// yBall + ballRadius > yRacket
// ) {
// ballMovespeedx *= -1;
// }
//}
function racketCollidelib(x, y) {
hit = collideRectCircle(
xRacket,
yRacket,
racketLength,
racketHeight,
xBall,
yBall,
ballRadius
);
if (hit) {
ballMovespeedx *= -1;
}
}
function showScore() {
fill(255);
text(myPoints, 278, 26);
text(foePoints, 321, 28);
}
function getScore() {
if (xBall > 590) {
myPoints += 1;
}
if (xBall < 10) {
foePoints += 1;
}
}