Make an object disappear upon collision

edited February 2017 in Questions about Code

Hello friends:

I'm a starter (I really mean starter, I have little or no clue about programming at all), and our teacher assigned us this homework in which we basically have to make 2 snakes (more like 2 independent squares) to go move around using wasd and the arrow keys and eat coins that are generated randomly. I have pretty much all I want except the part where the coins disappear.

So the question is: How do I make the coins disappear when one of the snakes collide with them?

Additional info: -I know the snakes don't move fluidly and since we are beginners the teacher told us it was okay. -There are some things in Spanish because it's my native language but I hope you won't have issues with it.

Here's my code until now:

Snake1 serpiente1;
Snake2 serpiente2;

Bol uno;
Bol dos;
Bol tres;
Bol cuatro;
Bol cinco;
Bol seis;
Bol siete;
Bol ocho;
Bol nueve;
Bol diez;


void setup(){
  size(600,600);
  serpiente1=new Snake1();
  serpiente2=new Snake2();

  uno= new Bol ();
  dos= new Bol ();
  tres= new Bol ();
  cuatro= new Bol ();
  cinco= new Bol ();
  seis= new Bol ();
  siete= new Bol ();
  ocho= new Bol ();
  nueve= new Bol ();
  diez= new Bol ();

}

  void draw (){
    background(255);``
    serpiente1.moverSerpiente();
    serpiente1.dibujaSerpiente(); 
    serpiente2.moverSerpiente();
    serpiente2.dibujaSerpiente();
    uno.dibujaMoneda();
    dos.dibujaMoneda();
    tres.dibujaMoneda();
    cuatro.dibujaMoneda();
    cinco.dibujaMoneda();
    seis.dibujaMoneda();
    siete.dibujaMoneda();
    ocho.dibujaMoneda();
    nueve.dibujaMoneda();
    diez.dibujaMoneda();


}

class Snake1{
float posx;
float posy;
float ancho;
float alto;
color c;
//constructor
Snake1 (){
  posx=30;
  posy=30;
  ancho=25;
  alto=25;
  c=color(random(255),random(255),random(255));
}

void moverSerpiente(){
  if (keyCode == UP){
    posy=posy-3;
    if(posy<0)
    posy=600;
  }
  else if (keyCode == DOWN){
    posy=posy+3;
    if(posy>600)
    posy=0;
  }
  else if (keyCode == RIGHT){
    posx=posx+3;
    if(posx>600)
    posx=0;
  }
  else if (keyCode == LEFT){
    posx=posx-3;
    if(posx<0)
    posx=600;
  }
}

  void dibujaSerpiente(){
    fill(c);
    rect(posx, posy, ancho, alto);
  }
}
class Snake2{
float posx;
float posy;
float ancho;
float alto;
color c;

//constructor
Snake2 (){
  posx=550;
  posy=550;
  ancho=25;
  alto=25;
  c=color(random(255),random(255),random(255));
}

void moverSerpiente(){
  if (key == 'w'){
    posy=posy-3;
    if(posy<0)
    posy=600;
  }
  else if (key == 's'){
    posy=posy+3;
     if(posy>600)
    posy=0;
  }
  else if (key == 'd'){
    posx=posx+3;
    if(posx>600)
    posx=0;
  }
  else if (key == 'a'){
    posx=posx-3;
     if(posx<0)
    posx=600;
  }
}

  void dibujaSerpiente(){
    fill(c);
    rect(posx, posy, ancho, alto);
  }
}

class Bol{
  float posx;
  float posy;
  float ancho;
  float alto;

  Bol(){
    posx=random(570);
    posy=random(570);
    ancho=15;
    alto=15;
  }

  void dibujaMoneda(){
    fill(250,250,0);
    ellipse(posx,posy,ancho,alto);
  }
}

I hope you guys can help me out! Thanks.

Answers

  • Answer ✓

    There are two tutorials in the Processing site that you will find very useful. They are https://processing.org/tutorials/arrays/ and https://processing.org/tutorials/objects/

    After you wrote your code, you will notice you have defined two classes, snake1 and snake2. Those two classes have the same nature. A challenge for you would be to use one class to instantiate two "serpientes". When you do that, you have mastered one of the concepts of OOP.

    In regards to your question about removing the coins, there are two things you need to do. First, in your coin class, you need to include a boolean variable. Let's call it visibleFlag. In your function dibujaMoneda(), you will only draw the moneda if the visibleFlag is true. For the second part, you need to implement a detection collision algorithm in your sketch. The trivial algorithm is based on circles. Your case is made of circles and squares so it is a bit more sophisticated. For the trivial case, you compare the distance between the center of the snake and the coin. You could use the dist() function provided by Processing. You compare this distance to the sum of the coin's radius and the snake's side length (actually half of this value). If the distance is larger, it means they do not overlap. Otherwise, the snake partially overlaps the coin and this coin should be turned off using the flag implemented in the first part.

    This post has more info: https://forum.processing.org/two/discussion/comment/88089/#Comment_88089

    Kf

  • thanks a lot kfrajer! you are a lifesaver. I'll try it out and let you know if I have any more questions.

  • edited February 2017

    I managed to make a simple collision method, but it only works if the distance is positive. void dibujaMoneda(){ fill(250,250,0); ellipse(posx,posy,r2,r2); }

                  void colisionMoneda(){
                    if(abs( dist(serpiente1.posx, serpiente1.posy, posx, posy))<12.5){
                    posx=610;
                    posy=610;
                    }
                   if(abs( dist(serpiente2.posx, serpiente2.posy, posx, posy))<12.5){
                    posx=610;
                    posy=610;
                   }
                  }
                }
    

    I thought absolute value would fix the problem but it didn't, is my logic wrong?, what am I missing here?

  • Answer ✓

    @Akkadian

    For the collision detection, I will recommend you use rectmode(CENTER) as explained here: https://processing.org/reference/rectMode_.html

    You should rewrite the conditional in your previous post as:

    if( dist(serpiente1.posx, serpiente1.posy, posx, posy) <(snake.ancho/2 + ancho)){...}

    where snake.ancho/2 is used because the snake is a square and we need half of its length in this calculation. The second ancho in the conditional refers to the radius of the coin. One remark here is to give proper names to your variables. In the case of your coin class, instead of using ancho and alto, you should define radius as the coins are circular. This will make your code easier to follow.

    Kf

  • That was it! finally have what I was looking for. Thanks a lot for all your help kfrajer.

Sign In or Register to comment.