//variaveis bolinha
let xBolinha = 300;
let yBolinha = 200;
let diamBolinha = 15;
let raio = diamBolinha / 2;
//propriedades da minha raquete
let xRaquete = 5;
let yRaquete = 150;
let raqueteComprimento = 10;
let raqueteAltura = 70;
 //propriedades da raquete oponente
let xRaqueteOponente = 585;
let yRaqueteOponente = 150;
let velocidadeYOponente;
//velocidade bolinha
let velocidadeX = 2;
let velocidadeY = 2;
// placar do jogo
let meusPontos = 0;
let pontosOponente = 0;
//tamanho da tela do jogo
function setup() {
  createCanvas(600, 400);
}
function draw() {
  background(0);
  mostraBolinha();
  movimentoBolinha();
  verificaColisaoBorda();
  mostraRaquete(xRaquete, yRaquete);
  mostraRaquete(xRaqueteOponente, yRaqueteOponente)
  colisaoMinhaRaquete();
  movimentaMinhaRaquete();
  movimentaRaqueteOponente();
  colisaoRaqueteOponente();
  verificaMeusPontos();
  verificaPontosOponente();
  incluiPlacar();
}
function mostraBolinha(){
  circle(xBolinha, yBolinha, diamBolinha);
}
function movimentoBolinha(){
  xBolinha += velocidadeX;
  yBolinha += velocidadeY;
}
function verificaColisaoBorda(){
  if (xBolinha + raio > width || xBolinha - raio < 0){
    velocidadeX *= -1
  }
  if (yBolinha + raio > height || yBolinha - raio < 0){ 
    velocidadeY *= -1
  }
}
function mostraRaquete(x, y){
  rect(x,y,raqueteComprimento,raqueteAltura)
}
 function movimentaMinhaRaquete(){
   if (keyIsDown(UP_ARROW)){
    yRaquete -= 10;
  }
  if (keyIsDown(DOWN_ARROW)){
    yRaquete += 10;
  }
 }
function colisaoMinhaRaquete(){
  if (xBolinha - raio < xRaquete + raqueteComprimento && yBolinha - raio < yRaquete + raqueteAltura && yBolinha + raio > yRaquete){
    velocidadeX *= -1
  }
}
function movimentaRaqueteOponente(){
  velocidadeYOponente = yBolinha - yRaqueteOponente - raqueteComprimento / 2 -30;
  yRaqueteOponente += velocidadeYOponente;
}
function colisaoRaqueteOponente(){
  if (xBolinha + raio > xRaqueteOponente && yBolinha - raio < yRaqueteOponente + raqueteAltura && yBolinha + raio > yRaqueteOponente){
    velocidadeX *= -1
  }
}
  function verificaMeusPontos(){
    if (xBolinha + raio > width){
      meusPontos += 1
    }
}
function verificaPontosOponente(){
  if (xBolinha - raio < 0){
    pontosOponente += 1
  }
}
function incluiPlacar(){
  fill(255)
  text(meusPontos, 278, 26);
  text(pontosOponente, 321, 26)
}