Help with catcher game

I am trying to make a catcher game (im a beginner) and some how the balls that fall are making a line and are not delating so, you don’t se a ball faling but a thick line.
Can somebody help me out and look at my code?
If you can help me improve it and help me that is also appreciated.
Thank you!

Catcher myCatcher;
Ball[] fallingBall;

void setup() {
size(800, 800);
frameRate(25);
myCatcher = new Catcher();
smooth();

fallingBall = new Ball[17];
fallingBall[0] = new Ball();
fallingBall[1] = new Ball();
fallingBall[2] = new Ball();
fallingBall[3] = new Ball();
fallingBall[4] = new Ball();
fallingBall[5] = new Ball();
fallingBall[6] = new Ball();
fallingBall[7] = new Ball();
fallingBall[8] = new Ball();
fallingBall[9] = new Ball();
fallingBall[10] = new Ball();
fallingBall[11] = new Ball();
fallingBall[12] = new Ball();
fallingBall[13] = new Ball();
fallingBall[14] = new Ball();
fallingBall[15] = new Ball();
fallingBall[16] = new Ball();
}

void draw() {
int a;
for (a = 0; a < 17; a++) {
fallingBall[a].display();
fallingBall[a].fall();

if ((abs(mouseY - fallingBall[a].bodyY) < 20) && (abs(mouseX - fallingBall[a].bodyX) < 20)) {
 fallingBall[a].caught();
  println(a + "caught");
}

}
myCatcher.display();
}

class Ball {
float bodyX;
float bodyY;
float i;
float s;
float d;
float speed;
float opacity;
float bodyR;
//float outlineRed;
//float outlineGreen;
float bodyColor;
boolean intersect;

Ball (){
bodyX = int(random(width));
bodyY = int(random(height/2));
i= random(23, 27);
s= random(28, 31);
d= random(8, 11);
bodyR = random(30, 40);
speed = random(1, 3);
opacity = 200;
//outlineRed = random(80, 255);
//outlineGreen = random(0, 180);
bodyColor = random(0, 40);
}

void display(){
ellipseMode(CENTER);
rectMode(CENTER);

//ball
stroke(100, 100, 0);
fill(bodyColor);
ellipse(bodyX, bodyY, bodyR, bodyR);

}

void fall(){
if (bodyY > 820){
bodyY = random(-40, -10);
bodyX = random(width);
opacity = 200;

}
else {
bodyY+= speed;
}
}

// If the spider is caught
void caught() {
bodyY = 820;
opacity = 0;

}
}

class Catcher{
float r; //radius
float x = mouseX;
float y = mouseY;

Catcher(){
r = 20;
}

void display(){
stroke(0);
fill(150);
ellipse(mouseX, mouseY, r, r);
}
}

At start of draw(), add a background(color) to clear the frame.
(Color) can be any color

Hi there Clay, welcome to the forum.

Could you please edit your post and format your code using the < /> Preformatted text button? This way code will be formatted properly, like this:

int x, y;

void setup() {
  size(1920, 1080);
  // rest of the code
}

void draw() {
  // rest of the code
}

Your problem of the balls leaving a trace can easily be solved by adding a background. void draw() keeps looping over and over, and unless you ‘paint’ over it the outcome of previous frames will remain visible.

Good to see you’re already trying out classes as a beginner. What I would suggest to do differently is to use a for loop instead to create those 17 class instances at the start. Another suggestion it to declare int a inside the following for loop instead. See the for page for more info.

How do i make a for loop of the 17 balls? And can i do it so that i counts until like 150? So i ca show the points?

Can you please help me
Thank you!

I won’t give you give you the answer, because I think it’s good that you learn to solve this yourself. I will however provide you with the appropriate information to help you on your way :slight_smile:

If we take a look at your sketch again you did the following:

fallingBall[0] = new Ball();
fallingBall[1] = new Ball();
fallingBall[2] = new Ball();
// +14 more

Whenever you notice that your code is repeating itself, it’s good practise to ask yourself how you could automate that process. This is one of the great benefits of coding– it can automate things so you don’t have to do it. So the question for to solve this issue would be: If you compare the similar lines of code, what part is different? And how could you replace this automatically?

ps. If you’re stuck and you really can’t figure it out, explain me how far you got with my questions and I’ll give you another hint.

I really cant figure it out, isnt the loop going to look like the one that is already in de code?
for (a = 0; a < 17; a++) {
And how do i make it so that i count endlessly until je lose and miss a ball. Because now it counts to 17

You’re close! You forgot to declare the variable type of a. If you tried that exact line inside your void setup it won’t work, because int a is a local variable inside void draw. Quick example of the difference between global and local variables:

int globalVariable; // this variable is now useable throughout the entire sketch

void setup() {
  size(400, 400);
  int localVariable1; // this variable is only useable inside 'void setup'
}

void draw() {
  int localVariable2; // this variable is only useable inside 'void draw'
}

When working with for loops, it’s common to reserve the letter i, j, and k for those. Inside void setup you could do the following to make 17 Ball instances:

for (int i = 0; i < 17; i++) {
  fallingBall[i] = new Ball();
  println(i); // you can use a 'println' to double check how many times it loops
}

Another, safer option is to let your sketch check how many it should make, rather than you manually inserting the number. This is another example of automating your sketch; you only have to type 17 at one location and your sketch takes care of the rest:

for (int i = 0; i < fallingBall.length; i++) {
  fallingBall[i] = new Ball();
}

Hope that answers your questions! :slight_smile:

2 Likes