Solucionado (ver solução)
Solucionado
(ver solução)
1
resposta

[Dúvida] Raquete do oponente não aparecendo, e colisão não acontecendo.

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;
  }
}
1 resposta
solução!

Oi, dev! Tudo bem?

Para resolvermos o problema relacionado à aparição da raquete devemos nos atentar à função racketVisibility(), note que ela recebe x e y como parâmetro, porém não os usa dentro do seu escopo. Devemos então trocar xRacket e yRacket, que são a localização da nossa raquete, por x e y, que receberão o valor que for passado dentro dos parâmetros e aplicarão na função. Dessa forma, a função poderá ser usada para desenhar tanto a nossa raquete quanto a do oponente, devendo ficar da seguinte forma:

function racketVisibility(x, y) {
  rect(x, y, racketLength, racketHeight);
}

Recomendo também, que para uma melhor localização da raquete do oponente, troquemos let xfoeRacket = 595 por let xfoeRacket = 585.

Em relação à colisão, seguindo o mesmo raciocínio explicado acima, na função racketCollidelib(x, y) devemos trocar xRacket e yRacket pelos valores recebidos no parametro, que são x e y. Deixaremos, então, a função da seguinte maneira:

function racketCollidelib(x, y) {
  hit = collideRectCircle(
    x,
    y,
    racketLength,
    racketHeight,
    xBall,
    yBall,
    ballRadius
  );
  if (hit) {
    ballMovespeedx *= -1;
  }
}

Por fim, em relação à pontuação, basta trocarmos os valores dentro da função getScore() de 590 e 10 por 592 e 8, deixando-a da seguinte forma:

function getScore() {
  if (xBall > 592) {
    myPoints += 1;
  }
  if (xBall < 8) {
    foePoints += 1;
  }
}

Espero ter ajudado na resolução do poblema! Caso tenha restado alguma dúvida, sinta-se à vontade em comunicar, estou à disposição!

Um forte abraço e bons estudos!

Caso este post tenha te ajudado, por favor, marcar como solucionado ✓