How to reset a loop and better edge detection

Trying to make my game where all the balls go into the bottom right "basket then their x,y cord is reset 0. Or maybe the loop restarts, I’m really not sure how to approach it. Also, my collision is based on the distance of the ball from my “character” or mouseX and fixed Y cord. I can’t figure out collision using PVectors. Here are my main game loop and my Ball class, I also have classes for the world and my character if needed.

Ball b1;
Bill c;
World w;
Ball[] balls =  new Ball[5]; 
float score =0;
void setup() {
  size(640,480);
  w = new World();
  c = new Bill();
  
  for(int i = 0; i < 5; i++) {
    balls[i] = new Ball();
  }
}

void draw() {
w.sky();
w.ground();

c.display();
for(int i = 0; i < 5; i++) {
    if (score < 10){
    balls[i].move();
    balls[i].check();
    balls[i].display();
    } else { i = i - 1;}
    }
c.cube();
w.basket();


}

class Ball {
  
  PVector loc, vel;
  float rad = 20;
  float billX = mouseX  + 60;
  float rectheight = 100;
  float rectwidth = 30;
  float billY =  398;
  float test;
  float score = 0;
  
Ball() {
  loc = new PVector(random(20,50),random(20,30));
  vel = new PVector(2,random(1,3));
}
 
 void display() {
  stroke(255,0,0);
  fill(255,0,0);
  ellipse(loc.x,loc.y,rad,rad);
}

void move() {
  loc.add(vel);
 
  
}

void check(){
  test = dist(loc.x + vel.x,loc.y +vel.y ,mouseX,400);
  if (loc.x < 0 + 10 || loc.x > width - rad) {
    vel.x *= -1;
    }

if(loc.y < 0 + rad|| loc.y > height - 35) {
    vel.y *= -1;
    }
    
if(test < 10 ) { 
  //vel.x = vel.x * -1;
  vel.y = vel.y * -0.95;
}

if (loc.x > 540 && loc.y > 380) {
        loc.x = 0;
        loc.y = 0;
        vel.x =2;
        vel.y =4;
        score +=6;
        }

}
}

please copy your above ( bad formatted ) code
back to a new processing 3.5.3 IDE sketch and run it.

if it not run repair above code posting ( regarding format </> button ) and code content.

repeat until it runs.

Hello,

and welcome to the forum!

To really help you, we need a full running code that we can run on our computers. So we need all your classes. Or you make a small version of the program without the world and character bits that we don’t need.

It would also be nice if you could post your entire code in one go and not in separate parts.

While I looked at your post I made a small Demo that shows how you could make a Game Over screen when the score of 10 is reached.
That’s because you wrote

“maybe the loop restarts, I’m really not sure how to approach it”.

Maybe a Game over screen is a good idea.

(The var score here is a global integer variable that gets incremented when a ball hits a screen border.)

Regards, Chrisir


// Demo for game and game over screen 

Ball[] balls = new Ball[5];
int score = 0;

void setup() {
  size(640, 480);

  for (int i = 0; i < 5; i++) {
    balls[i] = new Ball();
  }//for
}

void draw() {

  // we got 2 main situations: 
  if (score < 10) { // ------------------------------ 1
    // GAME 
    background(0);

    for (int i = 0; i < 5; i++) {
      if (score < 10) {
        balls[i].move();
        balls[i].check();
        balls[i].display();
      } else { 
        // i = i - 1;
        break;
      }
    }//for 
    // show score
    // fill(255, 0, 0); 
    fill(255, 255, 2); 
    text(score, width-44, 23);
    text("Demo for game and game over screen", 23, 23);
  } else {          // ------------------------------ 2
    // Game over
    background(0); 
    fill(255, 2, 2); 
    textAlign(CENTER); 
    text("YOU WON!", width/2, height/2-77); 
    text("Do you want to play again?\n(y/n)", width/2, height/2);
    textAlign(LEFT); 
    strokeWeight(44);
    noFill(); 
    stroke(255, 0, 0);
    rect(0, 0, width, height); 
    strokeWeight(1);
  }
}

void keyPressed() {
  if (score < 10) {
    // GAME
    // ignore
  } else {
    // Game over
    if (key=='y') {
      // Yes : reset
      score=0;
      for (int i = 0; i < 5; i++) {
        balls[i] = new Ball();
      }//for
    } else if (key=='n') {
      // NO : leave the program 
      exit(); 
      return;
    }
  }//else
}

// =========================================================

class Ball {

  PVector loc, vel;
  float rad = 20;
  float billX = mouseX + 60;
  float rectheight = 100;
  float rectwidth = 30;
  float billY = 398;
  float test;
  // float score = 0;

  Ball() {
    loc = new PVector(random(20, 50), random(20, 30));
    vel = new PVector(2, random(1, 3));
  }

  void display() {
    stroke(255, 0, 0); // RED 
    fill(255, 0, 0);
    ellipse(loc.x, loc.y, rad, rad);
  }

  void move() {
    loc.add(vel);
  }

  void check() {
    test = dist(loc.x + vel.x, loc.y +vel.y, 
      mouseX, 400);

    if (loc.x < 0 + 10 || loc.x > width - rad) {
      vel.x *= -1;
      score++;
    }

    if (loc.y < 0 + rad|| loc.y > height - 35) {
      vel.y *= -1;
      score++;
    }

    if (test < 10 ) {
      //vel.x = vel.x * -1;
      vel.y = vel.y * -0.95;
    }

    if (loc.x > 540 && loc.y > 380) {
      loc.x = 0;
      loc.y = 0;
      vel.x =2;
      vel.y =4;
      score +=6;
    }
  }
}//class 
//
1 Like