Collisions/ End Game

edited March 2018 in Questions about Code

I got somewhat far with my code, in my eyes, and I have one slight problem. I wanted to make my game end when my white character guy touches any of the fast moving red rectangles. The rectangles were supposed to move side to side, but what I have now will do. I also wanted to collect coins or some sort of rewards, (like pac-man) and then the game will finish. However, since I haven't been able to figure out the whole overlap/distance thing I'm not even sure if that is possible. Does any one have an example to explain this to me, or what I have to add to make this work? I've been trying for a whole week and nothing has worked yet. Thank you.

PFont title;
int menu;
int sub_cnt;
int sub_cntt;
int sub_cnttt;
int earthsize = 30;
int earth = 200;
int x=0;
int y=0;
float a,b, s=8;
boolean fireNow = false;
float xPos = 600;
float xxPos = 600;
float xxxPos = 600;
float xxxxPos = 600;
int rad = 40;
int xdirection = 1;
int ydirection = 1;
float xspeed = 2.8; 
float yspeed= 2.2;
float yPos;







//float rectX;
//float rectY;




void setup () {
  size(700, 700);
  title = createFont ("font", 28, true);
  menu = 1;
  background(0, 0, 30);


}

void draw () {

  if (menu == 1) {
    ellipse (random (width), random (height), random (4), random (4));
    textAlign(CENTER);
    textSize(112);
    text ("SEPTUA", 350, 300);
    textSize(30);
    text("press any key to start", 350, 325);
  }
  if (menu == 2) {
    background (0, 0, 30);
    //earth color
    fill(25, 54, 76);
    ellipse(earth, earth + 98,earthsize, earthsize);
    //rectangle (text box)
    fill(0, 0, 60);
    rect (0, 500, 700, 700);
    //text inside the box
    textSize(17);
    fill(255, 255, 255);
    String msg = "IN THE YEAR 3064, EARTH HAS BEEN CONSUMED BY A PLAGUE.";
    text (msg.substring(0,constrain(int(sub_cnt/5),0,msg.length())), 300, 560);
    sub_cnt++;
    if( sub_cnt == 12*msg.length() ){
      menu = 3;
    }
  }
  if ( menu == 3 ) {
    background(0,0,30);
    //earth animation (how it expands)
    fill(25, 54, 76);
    earthsize = earthsize + 1;
    ellipse(earth, earth+98, earthsize, earthsize);
    //text box
    fill(0,0,60);
    rect (0,500,700,700);
    textSize (17);
    fill(255, 255, 255);
    String msg = "THE HUMAN POPULATION HAS DECREASED BY 64%" ;
    text (msg.substring(0, constrain(int(sub_cntt/5), 0,msg.length())), 250, 560);
    sub_cntt++;
    if (sub_cntt == 12*msg.length () ){
      menu= 4;
    }
  }

  if (menu ==4) {

      background(0);
      text("You're going on a mission to retrieve the cure", width/2, height/2);
      text("Press Z to continue", width/2, height/2-20);
      if(keyPressed==true){
        if (key=='z' || key == 'Z'){
          menu = 6;
        }
      }
      if (keyPressed == true){
      menu= 5;
      }
    }




    if (menu ==5){
     ellipse (random (width), random (height), random (4), random (4));
      background (0, 0, 30);
       fill(220,220,220);
      rect(0, 400, 700, 300);
      //this next code is our sprite....
      fill(255, 255, 255);
      rect(x, y, 40, 40);

      //this next code will be our enemy

      //enemy # 1
      fill(160, 0, 0);
     // xPos = xPos - 1;
      rect(xPos - 20, 200, 40, 40);
      //enemy # 2
      fill(160, 0, 0);
      rect(xxPos - 20, 300, 40, 40);

      fill(160, 0, 0);
      rect(xxxPos-20, 400, 40, 40);

      fill(160, 0, 0);
      rect(xxxxPos-20, 500, 40, 40);




     // xPos = xPos + (xspeed * xdirection);
     // yPos = yPos + (yspeed * ydirection);
     // if(xPos> width-rad|| xPos < rad){
     //  xdirection = -1;
    //  }

    //  if (yPos > height-rad || yPos < rad) {
   // ydirection *= -1;
  //}

  if (xPos<700){
    xPos= xPos +10;
  }
  else if (xPos-30>600){
    xPos=xPos *-5;
  }

   if (xxPos<700){
   xxPos=xxPos +10;
  }
  else if (xPos-40>200){
    xxPos=xxPos *-5;

  }

       if (xxxPos<700){
  xxxPos=xxxPos+10;
  }
  else if (xxxPos-30>200);
   xxxPos=xxxPos*-5;

  }

  if (xxxxPos<700){
    xxxxPos=xxxxPos+10;
  }
  else if(xxxxPos-30>200){
    xxxxPos=xxxxPos*-5;
  }












      if (fireNow==true){
        if(a < height && b < width){
          fill(0,191,255);
          rect(a, b, 45, 10);
          a = a+x;
          b = b+(s);
        } else {
          fireNow = false;

        }


      }
    }




void keyPressed() {
  if (menu == 1) {
    menu = 2;
    sub_cnt = 0;
  }
  if (menu == 3) {
    menu = 4;
    sub_cnt = 0;
 }
 if(menu == 4) {
   menu = 5;
   sub_cnt = 0;
 }




   if (key == CODED) {
     if (keyCode == UP) {
       y-= 4;
     } else if (keyCode == DOWN) {
       y += 4;
     }
     else if (keyCode ==LEFT) {
       x-= 4;
     }
     else if (keyCode ==RIGHT) {
       x+= 4;
     }
     else if (keyCode == SHIFT) {
       y -= 60;
}
}


    }



void keyReleased() {
    if (key ==TAB){
      fireNow=true;
      a=x;
      b=y;
    }
  }
Tagged:

Answers

Sign In or Register to comment.