//variaveis da Bola
let xBola = 300;
let yBola = 200;
let dBola = 24;
let rBola = dBola / 2;
//velocidade da Bola
let vXBola = 6;
let vYBola = 6;
//dimensoes das Raquetes
let wRaquete = 10; //comprimento
let hRaquete = 90; //altura
//variaveis da Raquete do Jogador
let xRaqueteP = 5;
let yRaqueteP = 150;
//variaveis da Raquete do Oponente
let chanceErro = 0;
let xRaqueteO = 585;
let yRaqueteO = 150;
let vYRaqueteO;
//placar
let pontosP = 0;
let pontosO = 0;
//soundboard
let raquetadaSfx;
let pontoSfx;
let trilha;
let acertou = false;
function preload(){
trilha = loadSound("trilha.mp3");
pontoSfx = loadSound("ponto.mp3");
raquetadaSfx = loadSound("raquetada.mp3");
}
function setup() {
createCanvas(600, 400);
trilha.loop(0.1, 1, 0.5);
}
function draw() {
background(0);
bola();
colisaoBorda();
moveBola();
raquete(xRaqueteP, yRaqueteP);
moveRaqueteP();
colisaoRaquete();
raquete (xRaqueteO, yRaqueteO);
moveRaqueteO();
mostraPlacar();
contaPontos();
}
function bola() {
fill (0,255,0);
circle(xBola, yBola, dBola);
}
function moveBola() {
xBola += vXBola;
yBola += vYBola;
}
function colisaoBorda(){
if (xBola + rBola > width || xBola - rBola < 0){
vXBola *= -1; }
if (yBola + rBola > height || yBola - rBola < 0){
vYBola *= -1; }
}
function raquete(x, y) {
fill (0,255,0);
rect (x, y, wRaquete, hRaquete);
}
function moveRaqueteP() {
if (keyIsDown(UP_ARROW) && yRaqueteP != 0) {
yRaqueteP -= 10;
}
if (keyIsDown(DOWN_ARROW) && yRaqueteP + hRaquete != 400) {
yRaqueteP += 10;
}
}
function colisaoRaquete() {
acertou =
collideRectCircle(xRaqueteP, yRaqueteP, wRaquete, hRaquete, xBola, yBola, rBola) ||
collideRectCircle(xRaqueteO, yRaqueteO, wRaquete, hRaquete, xBola, yBola, rBola);
if (acertou) {
vXBola *= -1;
raquetadaSfx.play();
}
}
function moveRaqueteO() {
vYRaqueteO = yBola - yRaqueteO - wRaquete / 2 - 30;
yRaqueteO += vYRaqueteO + chanceErro;
calculaChanceErro();
}
function mostraPlacar(){
textSize(16);
textAlign(CENTER);
fill (0,255,0);
rect (150, 10, 40, 20, 10, 5, 5, 10);
fill (0);
text (pontosP, 170, 26);
fill (0,255,0);
rect(450, 10, 40, 20, 5, 10, 10, 5);
fill (0);
text(pontosO, 470, 26);
}
function contaPontos(){
if (xBola > 590){
pontosP += 1;
pontoSfx.play(0.1, 1, 0.5);
}
if (xBola < 10){
pontosO += 1;
pontoSfx.play(0.1, 1, 0.5);
}
}
function calculaChanceErro(){
if (pontosO >= pontosP) {
chanceErro += 1;
if (chanceErro >= 39){
chanceErro = 40;
}
} else {
chanceErro -= 1;
if (chanceErro <= 35){
chanceErro = 35;
}
}
}