# Help with catcher game

**URL:** <https://discourse.processing.org/t/help-with-catcher-game/16715>\
**Category:** Coding Questions\
**Created:** [December 29, 2019, 7:30pm UTC](https://discourse.processing.org/t/help-with-catcher-game/16715 "2019-12-29T19:30:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Clay1](https://avatars.discourse-cdn.com/v4/letter/c/e47774/32.png) [@Clay1](https://discourse.processing.org/u/Clay1)\
**Post date:** [December 29, 2019, 7:30pm UTC](https://discourse.processing.org/t/help-with-catcher-game/16715/1 "2019-12-29T19:30:25Z")

</div>

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);  
}  
}

---

<div class="post-metadata">

**Author:** ![JoseMY](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josemy/32/2016_2.png) [@JoseMY](https://discourse.processing.org/u/JoseMY)\
**Post date:** [December 29, 2019, 8:47pm UTC](https://discourse.processing.org/t/help-with-catcher-game/16715/2 "2019-12-29T20:47:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Tiemen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tiemen/32/5477_2.png) [@Tiemen](https://discourse.processing.org/u/Tiemen)\
**Post date:** [December 29, 2019, 8:48pm UTC](https://discourse.processing.org/t/help-with-catcher-game/16715/3 "2019-12-29T20:48:57Z")

</div>

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:

```auto
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](https://processing.org/reference/background_.html). `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](https://processing.org/reference/for.html) page for more info.

---

<div class="post-metadata">

**Author:** ![Clay1](https://avatars.discourse-cdn.com/v4/letter/c/e47774/32.png) [@Clay1](https://discourse.processing.org/u/Clay1)\
**Post date:** [December 31, 2019, 2:24pm UTC](https://discourse.processing.org/t/help-with-catcher-game/16715/4 "2019-12-31T14:24:14Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Tiemen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tiemen/32/5477_2.png) [@Tiemen](https://discourse.processing.org/u/Tiemen)\
**Post date:** [December 31, 2019, 8:22pm UTC](https://discourse.processing.org/t/help-with-catcher-game/16715/5 "2019-12-31T20:22:36Z")

</div>

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 🙂

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

```auto
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._

---

<div class="post-metadata">

**Author:** ![Clay1](https://avatars.discourse-cdn.com/v4/letter/c/e47774/32.png) [@Clay1](https://discourse.processing.org/u/Clay1)\
**Post date:** [January 7, 2020, 11:08pm UTC](https://discourse.processing.org/t/help-with-catcher-game/16715/6 "2020-01-07T23:08:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Tiemen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tiemen/32/5477_2.png) [@Tiemen](https://discourse.processing.org/u/Tiemen)\
**Post date:** [January 8, 2020, 11:36am UTC](https://discourse.processing.org/t/help-with-catcher-game/16715/7 "2020-01-08T11:36:19Z")

</div>

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:

```auto
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:

```auto
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:

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

```

Hope that answers your questions! 🙂
