O oponente nao consegue pontuar quando a bola bate na parte superior do meu lado da parede, apenas na parte inferior os pontos sao ad
Code:
//ball's variables
let xBall = 300;
let yBall = 200;
let dBall = 17; //ball´s diameter
//speed ball variables
let speedxBall = 5; //ball´s speed is 5 for the right side
let speedyBall = 5;
let radius = dBall /2; //Diameter is radius 2 times
//racket´s variables (weight e height)
let xRacket = 5;
let yRacket = 155;
let wRacket = 5;
let hRacket = 90;
let crashed = false;
// opponent´s racket
let xRacketOp = 590;
let yRacketOp = 150;
let speedyOp;
let hit = false
//score game
let myPoints = 0;
let opPoints = 0;
//sounds game
let raquetada;
let ponto;
let trilha;
//error chances
let errorChance = 0;
function preload(){
trilha = loadSound("trilha.mp3");
ponto = loadSound("ponto.mp3");
raquetada = loadSound("raquetada.mp3");
}
function setup() {
createCanvas(600, 400);
trilha.loop();
}
function draw() {
background(0);
showingBall();
movingBall();
collisionBall();
showRacket(xRacket, yRacket);
movingRacket();
collisionRacket();
checkHitRacket(xRacket,yRacket);
showRacket(xRacketOp, yRacketOp);
movingRacketOp();
checkHitRacket(xRacketOp, yRacketOp);
showUpScore();
score();
}
function showingBall(){
circle(xBall,yBall,dBall); //creat ball
}
function movingBall(){
xBall += speedxBall; // or xBall+xBall=speedxBall; (right side)
yBall += speedyBall; // (left side)
}
function collisionBall(){
if (xBall + radius > width ||
xBall - radius < 0){
speedxBall *= -1;
}
if (yBall + radius > height ||
yBall - radius < 0){
speedyBall *= -1
}
}
function showRacket(x,y){
rect(x, y, wRacket, hRacket);
}
function movingRacket(){
if (keyIsDown(UP_ARROW)){
yRacket -= 10;
}
if (keyIsDown(DOWN_ARROW)){
yRacket += 10;
}
}
function checkHitRacket(){
if (xBall - radius < xRacket + wRacket && yBall - radius < yRacket + hRacket && yBall + radius > yRacket){
speedxBall *= -1;
}
}
function checkHitRacket(x,y){
crashed = collideRectCircle (x, y, wRacket, hRacket, xBall, yBall, radius);
if (crashed){
speedxBall *= -1;
raquetada.play();
}
}
function movingRacketOp(){
if (keyIsDown(87)){
yRacketOp -= 10;
}
if (keyIsDown(83)){
yRacketOp += 10;
}
}
function calculateErrorChance(){
if (opPoints >= myPoints) {
errorChance +=1
if (errorChance >= 39){
errorChance = 40
}
} else {
errorChance -= 1
if (errorChance <=35){
errorChance = 35
}
}
}
function showUpScore(){
stroke(color(240,128,128));
textAlign(CENTER);
textSize(17);
fill (color(219,112,147));
rect(150,10, 40, 20);
fill (255);
text(myPoints, 170, 26);
fill (color(219,112,147));
rect(450, 10, 40, 20);
fill(255);
text(opPoints, 470, 26);
}
function score(){
if (xBall > 590){
myPoints += 1;
ponto.play();
}
if (xBall < 10){
opPoints += 1;
ponto.play();
}
}