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

Raquete não está na mesma linha que a bolinha

A raquete não está ficando na mesma linha que a bolinha e eu não estou entendendo o motivo

let xCentro = 400; let yCentro = 300; let diametro = 20; let velocidadeX = 5; let velocidadeY = 5; let raio = diametro/2; let xRetangulo = 15; let yRetangulo = 300; let larguraRetangulo = 10; let alturaRetangulo = 80;

function setup() { createCanvas(800, 600); }

function draw() { background(0); desenhoBolinha(); // movimentoBolinha(); colisaoBolinha(); rect(xRetangulo,yRetangulo,larguraRetangulo,alturaRetangulo)

}

function desenhoBolinha(){ circle(xCentro,yCentro,diametro); } function movimentoBolinha(){ xCentro += velocidadeX; yCentro += velocidadeY; } function colisaoBolinha(){ if (xCentro + raio > width || xCentro - raio < 0 ){velocidadeX *= -1; } if (yCentro + raio > height || yCentro - raio < 0){velocidadeY *= -1;} }

3 respostas

Oi, Gabriel

Vc tem alguma dúvida?

a raquete não está na mesma linha que a bolinha e eu não estou entendendo o motivo

solução!

Gabriel,

Para centralizar a Raquete no eixo Y

/* 
O "Y" da Raquete tem que igual a metade a altura (height = 600) da tela do jogo, 
menos a metade da altura da Raquete (alturaRetangulo = 80)

yRetangulo = height/2 - alturaRetangulo/2;
yRetangulo = 300 - 40;
*/

let yRetangulo = 260; 

"sketch.js"


let xCentro = 400;
let yCentro = 300;
let diametro = 20;
let velocidadeX = 5;
let velocidadeY = 5;
let raio = diametro / 2;
let xRetangulo = 15;
let yRetangulo = 260;
let larguraRetangulo = 10;
let alturaRetangulo = 80;

function setup() {
  createCanvas(800, 600);
}

function draw() {
  background(0);
  desenhoBolinha();
  // movimentoBolinha();
  colisaoBolinha();
  rect(xRetangulo, yRetangulo, larguraRetangulo, alturaRetangulo);
}

function desenhoBolinha() {
  circle(xCentro, yCentro, diametro);
}

function movimentoBolinha() {
  xCentro += velocidadeX;
  yCentro += velocidadeY;
}

function colisaoBolinha() {
  if (xCentro + raio > width || xCentro - raio < 0) {
    velocidadeX *= -1;
  }
  if (yCentro + raio > height || yCentro - raio < 0) {
    velocidadeY *= -1;
  }
}

Ou:


let xCentro = 400;
let yCentro = 300;
let diametro = 20;
let velocidadeX = 5;
let velocidadeY = 5;
let raio = diametro / 2;
let xRetangulo = 15;
let yRetangulo;
let larguraRetangulo = 10;
let alturaRetangulo = 80;

function setup() {
  createCanvas(800, 600);
  yRetangulo = height/2 - alturaRetangulo/2;
}

function draw() {
  background(0);
  desenhoBolinha();
  // movimentoBolinha();
  colisaoBolinha();
  rect(xRetangulo, yRetangulo, larguraRetangulo, alturaRetangulo);
}

function desenhoBolinha() {
  circle(xCentro, yCentro, diametro);
}

function movimentoBolinha() {
  xCentro += velocidadeX;
  yCentro += velocidadeY;
}

function colisaoBolinha() {
  if (xCentro + raio > width || xCentro - raio < 0) {
    velocidadeX *= -1;
  }
  if (yCentro + raio > height || yCentro - raio < 0) {
    velocidadeY *= -1;
  }
}

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software