# Catching images

**URL:** <https://discourse.processing.org/t/catching-images/30565>\
**Category:** Coding Questions\
**Created:** [June 8, 2021, 8:20am UTC](https://discourse.processing.org/t/catching-images/30565 "2021-06-08T08:20:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![processinggg](https://avatars.discourse-cdn.com/v4/letter/p/d2c977/32.png) [@processinggg](https://discourse.processing.org/u/processinggg)\
**Post date:** [June 8, 2021, 8:20am UTC](https://discourse.processing.org/t/catching-images/30565/1 "2021-06-08T08:20:52Z")

</div>

Hello,  
I have difficulty with following code in Processing, I don’t know how to make the bar catch the images/objects that are falling from above. Here is the code

float x = 100;  
float y = 500;

Balken balken; //One Bar object  
Timer timer;  
Ball[] ball; //An array of ball objects  
int score = 0; //Score Variable

void setup() {  
size(800, 600);  
smooth();  
frameRate(30);  
textSize(15);

balken = new Balken(200);  
ball = new Ball[1000]; //Create 1000 spots in the array  
timer = new Timer(300);  
timer.start();  
}

void draw() {  
background(0);

// Display the catcher  
balken.display(); //Balken display

// Check the timer  
if (timer.isFinished()) {  
// Deal with raindrops  
// Initialize one drop  
ball[score] = new Ball();  
// Increment totalDrops  
score ++ ;  
// If we hit the end of the array  
if (score \>= ball.length) {  
score = 0; // Start over  
}  
timer.start();  
}

// Move and display all drops  
for (int i = 0; i \< score; i++ ) {  
ball[i].move();  
ball[i].display();  
if (balken.intersect(ball[i])) {  
ball[i].caught();  
}  
}  
}

//class bar  
class Balken {

float xSpeed = 0; //Speed in direction X  
float ySpeed = 0; //Speed in direction Y

float b, h; //Width and Height  
color col; // color  
float x, y; // location

Balken(float tempR) {  
b = tempR;  
h = 10;  
col = color(50, 129, 129);  
fill(col);  
x = 100;  
y = 500;  
}

void setLocation(float x1, float y1) { //float tempX/Y  
x = x1; //tempX  
y = y1; //temp Y  
}

void display() {  
stroke(0);  
fill(col);

```
if ((keyPressed == true) && (key == CODED)) {
  if (keyCode == LEFT) {
    x--;
  } else if (keyCode == RIGHT) {
    x++;
  }
}

  rect(x, y, b, h); //Draw bar 

xSpeed *= 0.9; //Drag, so the square doesn't fly off the screen 
ySpeed *= 0.9;

```

}

// A function that returns true or false based on  
// if the catcher intersects a raindrop  
boolean intersect(Ball d) {  
// Calculate distance  
float distance = dist(x, y, d.x, d.y);

```
// Compare distance to sum of radii
if (distance < x + d.x) { 
  return true;
} else {
  return false;
}

```

}  
}

//class ball

class Ball {

float x; //X-Koordinate  
float y; //Y-Koordinate  
float r; // Radius 10

int yDir = 1; //In which direction does it go to?  
float y\_speed = random(1, 7);

int score = 0; //Score variable  
int leben = 5; //Life,health variable  
boolean lost = false; //Display if you lost the game

color c; // Define color ‘c’

PImage image;

Ball() {  
image = loadImage(“Goldmuenze.png”);  
r = 10; // All raindrops are the same size  
x = random(width); // Start with a random x location  
y = -r\*4; // Start a little above the window  
y\_speed = random(1, 5); // Pick a random speed  
c = color(255, 129, 129); //Color  
}

// Move the Ball down  
void move() {  
// Increment by speed  
y+=y\_speed;  
}

//Check if it hits the bottom  
boolean Bodenerreicht() {  
//If we go a little beyond the bottom  
if (y \> height + r\*4) {  
return true;  
} else {  
return false;  
}  
}

//Display the ball  
void display() {  
//display the ball  
fill(c);  
noStroke();  
for(int i = 2; i \< r; i++) {  
image(image, x, y, 50, 50);  
}  
}

//If the drop is caught  
void caught(){  
//Stop it from moving by setting speed equal to zero  
y\_speed = 0;  
//Set the location to somewhere way off-screen  
y= -1000;

}  
}

//class timer

class Timer {

int savedTime; // When Timer started  
int totalTime; // How long Timer should last

Timer(int tempTotalTime) {  
totalTime = tempTotalTime;  
}

// Starting the timer  
void start() {  
// When the timer starts it stores the current time in milliseconds.  
savedTime = millis();  
}

// The function isFinished() returns true if 5,000 ms have passed.  
// The work of the timer is farmed out to this method.  
boolean isFinished() {  
// Check how much time has passed  
int passedTime = millis()- savedTime;  
if (passedTime \> totalTime) {  
return true;  
} else {  
return false;  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [June 8, 2021, 8:34am UTC](https://discourse.processing.org/t/catching-images/30565/2 "2021-06-08T08:34:28Z")

</div>

basically for loop over your balls

for each ball check, if it’s x-position is inside the Balken:

```auto

boolean intersect(Ball d) {

    if(d.x > x && d.y < x+b) /// here is another typo
        return true;
        else return false; // EDITED  
    
}

```

---

<div class="post-metadata">

**Author:** ![processinggg](https://avatars.discourse-cdn.com/v4/letter/p/d2c977/32.png) [@processinggg](https://discourse.processing.org/u/processinggg)\
**Post date:** [June 8, 2021, 8:38am UTC](https://discourse.processing.org/t/catching-images/30565/3 "2021-06-08T08:38:14Z")

</div>

The bar still can’t catch the coins, can you pls take another look?

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [June 8, 2021, 8:56am UTC](https://discourse.processing.org/t/catching-images/30565/4 "2021-06-08T08:56:00Z")

</div>

Oh, I had a typo!!!

Also added check for y-pos (assuming diameter is 50)

```auto
boolean intersect(Ball d) {

    if ((d.x > x && d.x < x+b) && 
      ( dist(d.y, 0, y, 0) < 54 || d.y>=y-22 ) ) { 
      return true;
    } else return false;
  }

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [June 8, 2021, 8:59am UTC](https://discourse.processing.org/t/catching-images/30565/5 "2021-06-08T08:59:38Z")

</div>

instead of

```auto
image("image", 
        x, y, 50, 50);

```

better say `image("image", x, y);`

and in the constructor add:

```auto
  Ball() {
    image = loadImage("Goldmuenze.png");
    image.resize(50, 0);

```

this way you save some time (for the processor) because

- resize is done only once (in the constructor) and
- not throughout (like when you use `image()` with 5 parameters, the processor resizes the image every time)

Hey, and welcome to the forum!

Great to have you here!

Warm regards,

Chrisir 😉

---

<div class="post-metadata">

**Author:** ![processinggg](https://avatars.discourse-cdn.com/v4/letter/p/d2c977/32.png) [@processinggg](https://discourse.processing.org/u/processinggg)\
**Post date:** [June 8, 2021, 9:11am UTC](https://discourse.processing.org/t/catching-images/30565/6 "2021-06-08T09:11:20Z")

</div>

Hey! Thanks a lot! 🙂
