Meu código está bem diferente do original, em função das customizações que fiz. Mas não consigo entender porque a raquete do oponente desaparece quando insiro a chance de errar.
Consegui verificar que tem a ver com a variável da chance de errar, que eu chamei de "errorProbability". Quando eu a retiro, a raquete volta a se comportar normalmente, por exemplo.
Já alterei os nome, pois achei que poderiam ser algum nome de variável própria da plataforma e estaria em conflito, mas mesmo nomeando da forma como foi sugerida na aula o erro se mantem.
Link pro sketch https://editor.p5js.org/regisncoelho/sketches/XKgMKxBHK Código do jogo abaixo
let hit = false;
var xBall = 300;
var yBall = 200;
let dBall = 40;
let radius = dBall/2;
let speed = 8
let speedXBall = speed;
let speedYBall = speed;
let speedYPlayerTwo;
let xRacketOne = 5;
let yRacketOne = 250;
let wRacket = 15;
let hRacket = 120;
let xRacketTwo;
let yRacketTwo = 250;
let scorePlayerOne = 0;
let scorePlayerTwo = 0
let strike;
let point;
let soundtrack;
let errorProbability;
function preload() {
soundtrack = loadSound("trilha.mp3");
point = loadSound("point.mp3");
strike = loadSound("strike.wav")
}
function setup() {
createCanvas(windowWidth, windowHeight)
// soundtrack.loop();
}
function draw() {
let xRacketTwo = windowWidth-windowHeight/35;
background('#2577AB');
fill(255);
stroke(0);
rect((windowWidth/2) - 4, 0, 8, windowHeight);
stroke(255);
line (0, windowHeight/2, windowWidth, windowHeight/2);
showBall();
moveBall();
checkTouchBorder();
showRackets(xRacketOne, yRacketOne);
moveRacketOne();
checkTouchRackets(xRacketOne, yRacketOne);
checkTouchRackets(xRacketTwo, yRacketTwo);
showRackets(xRacketTwo, yRacketTwo);
moveRacketTwo();
showScores();
}
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
}
function showBall (){
fill('#FFD75D');
stroke(0);
circle(xBall,yBall,dBall)
}
function moveBall() {
xBall += speedXBall;
yBall += speedYBall
}
function moveRacketTwo () {
speedYPlayerTwo = yBall - yRacketTwo - hRacket/2 - 30;
yRacketTwo += speedYPlayerTwo + errorProbability
calculateError()
}
function checkTouchBorder() {
if (xBall + radius > width) {
speedXBall *= -1;
scorePlayerOne += 1;
point.play();
}
if (xBall + radius < 0 ){
speedXBall *= -1;
scorePlayerTwo += 1;
point.play();
}
if (yBall + radius > height ||
yBall - radius< 0){
speedYBall *= -1;
}
}
function showRackets(x, y){
fill('#FD6864');
rect (x, y, wRacket, hRacket, 20);
}
function moveRacketOne(){
if (keyIsDown (UP_ARROW)) {
if (yRacketOne > 0){
yRacketOne -= 10;
}
}
if (keyIsDown (DOWN_ARROW)) {
if (yRacketOne + hRacket < height){
yRacketOne += 10;
}
}
}
function checkTouchRackets(x, y) {
hit = collideRectCircle(x,
y,
wRacket,
hRacket,
xBall,
yBall,
radius);
if (hit){
speedXBall *= -1;
strike.play();
}
}
function showScores() {
// rect(windowWidth/2 - windowWidth/17, windowHeight/34, windowHeight/15, windowHeight/20)
fill(255)
textSize(25)
textAlign(CENTER)
noStroke();
text (scorePlayerOne, windowWidth/2 - windowWidth/20, windowHeight/15)
text (scorePlayerTwo, windowWidth/2 + windowWidth/22, windowHeight/15)
}
function calculateError() {
if (scorePlayerTwo >= scorePlayerOne) {
errorProbability += 1;
if (errorProbability >= 39){
errorProbability = 40
}
}
else {
errorProbability -= 1;
if (errorProbability <= 35){
errorProbability = 35
}
}
}