1
resposta

[Projeto] Quero remover objeto quando colidir com a bolinha

Como base no jogo de pong do Proff Guilherme Lima, eu tentei fazer um jogo parecido com Breakout 1976 do Atari. O meu problema é não conseguir remover a bola quando a bolinha colide com ela, tentei por conta própria usar array e splice mas n consegui. Essa bola gigante é só pra teste, meu objetivo é conseguir remover ela.

Tambem consegui fazer com que a raquete não fique passando da parede, quem quiser dar uma olhada esta escrito na função "RacketMovemant"

ESTE É O MEU CODIGO POR ENQUANTO.

//Ball Variables
let xBall = 250;
let yBall = 400;
let BallDiameter = 28;
let Radius = BallDiameter/2;
let BallSpeedX = 5;
let BallSpeedY = 5;

//HitBall
let HitBall = [0]

//Racket Variables
let xRacket = 140;
let yRacket = 600;
let RacketWidth = 80;
let RacketHeight = 16;

function setup() {
  createCanvas(360, 640);
}

function draw() {
  background(0);
  Ball();
  BallMovemant();
  BallBorderColision();
  Racket();
  RacketMovemant();
  RacketWithBallColision()
  HitBalls();
  HitBallDestroy();

}

function Ball(){
  circle (xBall, yBall, BallDiameter);
}

function BallMovemant(){
  yBall += BallSpeedY;
  xBall += BallSpeedX;
}

function BallBorderColision(){  
  if (yBall+Radius>height||yBall-Radius<0){BallSpeedY *= -1;}
  if (xBall+Radius>width||xBall-Radius<0){BallSpeedX *= -1;}
}

function Racket(){
  rect(xRacket, yRacket, RacketWidth, RacketHeight);
}

function RacketMovemant(){
  if (keyIsDown(LEFT_ARROW)) xRacket -= 10;{
    if (keyIsDown(RIGHT_ARROW)) xRacket += 10;{
      if (xRacket+RacketWidth>=width){xRacket = 365-RacketWidth}{
        if (xRacket+RacketWidth<=0+RacketWidth){xRacket = -5}
      }
    }
  }
}

function RacketWithBallColision(){
  if(yBall+Radius>=yRacket+RacketHeight&&xBall+Radius<=xRacket+RacketWidth&&xBall+Radius>=xRacket){BallSpeedY *= -1;}
}

function HitBalls(){
    HitBall[0] = circle(200,200,100);

}

function HitBallDestroy(){
  if (xBall+Radius>=HitBall||yBall+Radius>=HitBall){HitBall.splice(0,1)}
}
1 resposta

Olá, Hugo! Tudo bem?

Acho que entendi seu objetivo.

Primeiro você precisa criar uma colisão entre as duas bolas, igual como fez com a borda e a raquete.

Aqui você encontra um exemplo para implementação: https://p5js.org/examples/motion-circle-collision.html

Depois você pode testar com as funções hide() e show() do p5.js também: https://p5js.org/reference/#/p5.Element/hide

Fico à disposição

Um abraço e bons estudos!