Game of popping polka dots on the screen

Asked

Viewed 377 times

2

I only posted the JS, I think it’s enough, I’m still new to JS and programming so I know almost nothing of the commands, I want to compare the position of the balls and the area they appear so that they do not "be born" one inside the other, with what I can do this comparison inside the if?

function addBola () {
    var bola = document.createElement("div");
    bola.setAttribute("class", "bola");
    var p1 = Math.floor(Math.random() * 1000);
    var p2 = Math.floor(Math.random() * 500);

    var corBolinha = Math.floor(Math.random() * 999);

    bola.setAttribute("style", "left:" + p1 + "px; top:" + p2 +
    "px; background-color: #" + corBolinha);

    bola.setAttribute("onmousedown", "estourar(this)");

    if(/*comparação vai aqui!!!*/) {
        return false;
    }else {
        document.body.appendChild(bola);

    }
}

function estourar(elemento) {
    document.body.removeChild(elemento)
    document.getElementById("placar").innerHTML++;
}

function errar(missed) {
    document.getElementById("missClick").innerHTML++;
}

function iniciar() {
    document.getElementById("placar").innerHTML = 0;
    document.getElementById("missClick").innerHTML = 0;
    setInterval(addBola, 600);
}
  • what are the dimensions of the ball? width, height or radius?

  • Div is 50px by 50px Radius 25 to let the div round

2 answers

4


One idea would be to have access to the coordinates (left and top) of each ball.

You can have a collection with all divs bolas using:

var bolasDivs = document.getElementsByClassName('bola');

To have coordinates in an array you can use:

var bolasCoordenadas = Array.prototype.map.call(bolasDivs, b => ({

    left: b.getBoundingClientRect().left + document.documentElement.scrollLeft,
    top: b.getBoundingClientRect().top + document.documentElement.scrollTop,

}));

Now inside a do...while will receive coordinates until this new coordinate is 'available'..

var p1, p2;

do {
    p1 = Math.floor(Math.random() * 1000);
    p2 = Math.floor(Math.random() * 500);
}
while (bolasCoordenadas.find(b => Math.sqrt((b.left - p1) * (b.left - p1) + (b.top - p2) * (b.top - p2)) < 50) !== undefined)

You can see the collision between circles in this link.

Link uteis

See working in :https://jsfiddle.net/0b8f34no/

  • while (ballsCoordenadas.find(b => Math.sqrt((b.left - P1) * (b.left - P1) + (b.top - P2) * (b.top - P2)) < 50) !== Undefined) can explain what this does?

  • I don’t know why but your code breaks when I scroll, then they enter each other after that!

  • @Ricardogava, edited the answer, it seems that adding document.body.scrollLeft and top solves the problem, this line is to calculate the collision between circles as it is in the link, the find returns the first item/object when it finds and undefined when you don’t find..

  • @Richy document.documentElement.scrollLeft

3

Create an array and add objects with 3 information:

{p1, p2, id_da_bola}

The p1 is position X, the p2 position Y and the id_da_boa an id to identify the ball.

Use a for to go through the array objects looking for an X and Y position that is between the range of the position returned by the variables p1 and p2, that is, it can override an existing element in the array.

Within the for you use a if to make that check. If meet means that the element to be inserted will override an existing one, then you change the state of a control variable (called flag) not to insert the element.

When the ball goes burst, you will remove from the array the object that has the id of the ball, releasing that space for a new ball. This way, never a ball will occupy the same space of another, see:

var bolaPos = [], bolaId = 0;
function addBola () {
    var bola = document.createElement("div");
    bola.setAttribute("class", "bola");
    var p1 = Math.floor(Math.random() * 1000);
    var p2 = Math.floor(Math.random() * 500);

   var flag;
   for(var x=0; x < bolaPos.length; x++){
      if( ((p1 > bolaPos[x].p1-50 && p1 < bolaPos[x].p1) || (p1 < bolaPos[x].p1+50 && p1 >= bolaPos[x].p1))
         &&
         ((p2 > bolaPos[x].p2-50 && p2 < bolaPos[x].p2) || (p2 < bolaPos[x].p2+50 && p2 >= bolaPos[x].p2)) ){
            flag = true;
            break;
      }
   }

    if(flag) {
        return;
    }else {
       bolaId++;
       bolaPos.push({ p1, p2, bolaId: "b_"+bolaId });
       var corBolinha = Math.floor(Math.random() * 999);
   
       bola.setAttribute("style", "left:" + p1 + "px; top:" + p2 +
       "px; background-color: #" + corBolinha);
      bola.id = "b_"+bolaId;
       bola.setAttribute("onmousedown", "estourar(this)");
        document.body.appendChild(bola);

    }
}

function estourar(elemento) {
    document.body.removeChild(elemento)
    document.getElementById("placar").innerHTML++;

   var b_id = elemento.id;
   bolaPos = bolaPos.filter(function(e){
     return e.bolaId != b_id;
   });

}

function errar(missed) {
    document.getElementById("missClick").innerHTML++;
}

function iniciar() {
    document.getElementById("placar").innerHTML = 0;
    document.getElementById("missClick").innerHTML = 0;
    setInterval(addBola, 600);
}
iniciar()
.bola{
   width: 50px;
   height: 50px;
   position: absolute;
   border-radius: 25px;
}
<span id="placar"></span>
<span id="missClick"></span>

  • Vlw for help Sam, but this code is too complex for me who is beginner, I can barely read his code and understand what he is doing, I gave preference to the other because it is a code more "simplified", hugs!

  • 1

    It’s not complex. Take a good look, it’s your own code only working.

  • I don’t understand....

  • 1

    If it’s me. I only said that the code is not difficult to understand and the operator should take a closer look because the answer is based on the code of the question.

  • 2

    Ah tah. Now I get rs.. Easy, buddy. If the other code works well, that’s what matters. Abs!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.