Solucionado (ver solução)
Solucionado
(ver solução)
3
respostas

Jogo completo. Segue o código.

//about the Ball
let xBoll = 100;
let yBoll = 100;
let diameter = 30;
let radius = diameter / 2;
let speedXBoll = 6;
let speedYBoll = 5;

//about the Racket
let xRacket = 5;
let yRacket = 150;
let widthRacket = 10;
let lengthRacket = 90;
let hit = false;

//about the Opponent
let xOpponent = 584;
let yOpponent = 150;
let speedYOpponent;
let errorOpponent = 0;

//Game score
let scorePlayer = 0;
let scoreOpponent = 0;

//Game sound
let soundRacket;
let soundScore;
let soundTrack;

//game arena
function setup() {
  createCanvas(600, 400);
  soundTrack.loop();
}

//master function
function draw() {
  background(0);
  showBall();
  moveBall();
  collisionBall();
  showRacket(xRacket, yRacket);
  moveRacket();
  collisionRacket(xRacket, yRacket);
  showRacket(xOpponent, yOpponent);
  moveOpponent();
  collisionRacket(xOpponent, yOpponent);
  gameScore();
  scoring();
}

//control functions Ball
function showBall(){
  fill(color(127,255,0));
  circle(xBoll, yBoll, diameter);
}

function moveBall(){
  xBoll += speedXBoll;
  yBoll += speedYBoll;
}

function collisionBall(){
  if (xBoll + radius > width || xBoll - radius < 0) {
    speedXBoll *= -1;
  }
  if (yBoll + radius > height || yBoll - radius < 0) {
    speedYBoll *= -1;
  }
}

//control functions Racket
function showRacket(x,y){
  rect(x, y, widthRacket, lengthRacket);
}

function moveRacket(){
  if (keyIsDown(UP_ARROW)) {
    yRacket -= 10;
  }
  if (keyIsDown(DOWN_ARROW)) {
    yRacket += 10;
  }
}

//Our solution - not to use
function collisionRacket(){
  if (xBoll - radius <  + widthRacket && yBoll - radius <yRacket+lengthRacket && yBoll + radius > yRacket){

  }
}

//Library solution to collision
function collisionRacket(x,y){
  hit = collideRectCircle(x,y,widthRacket,lengthRacket,xBoll,yBoll,radius);
  if(hit){
    speedXBoll *= -1;
    soundRacket.play();
  }
}

function moveOpponent(){
  speedYOpponent = yBoll - yOpponent - lengthRacket/2-30;
  yOpponent += speedYOpponent;
  calcErrorOpponent();
}

function gameScore(){
  textAlign(CENTER);
  textSize(20);
  fill(color(255, 40, 0));
  rect(130,8,40,25);
  fill(255);
  text(scorePlayer, 150, 26);
  fill(color(255, 40, 0));
  rect(430, 8, 40, 25);
  fill(255);
  text(scoreOpponent, 450, 26);
}

function scoring(){
  if (xBoll > 585){
  scorePlayer += 1;
    soundScore.play();
  }
  if (xBoll < 15){
  scoreOpponent += 1;
    soundScore.play();
  }
}

function preload(){
  soundTrack = loadSound("trilha.mp3");
  soundRacket = loadSound("raquetada.mp3");
  soundScore = loadSound("ponto.mp3");
}

function calcErrorOpponent() {
  if (scoreOpponent >= scorePlayer) {
    errorOpponent += 1;
    if (errorOpponent >= 39){
    errorOpponent = 40;
    }
  } else {
    errorOpponent -= 1;
    if (errorOpponent <= 35){
    errorOpponent = 35;
    }
  }
}
3 respostas
solução!

Parabéns, Eliezer!!!

Bons estudos e vamos em frente!!!

Oi Eliezer,

Obrigada por partilhar, pois assim consegui verificar o que estava fazendo de errado ;)

Força Daiane, com determinação a gente avança! Abraços.