Catching images

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

basically for loop over your balls

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


boolean intersect(Ball d) {

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

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

Oh, I had a typo!!!

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

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

instead of

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

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

and in the constructor add:

  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 :wink:

Hey! Thanks a lot! :slight_smile:

1 Like