Make a few rectangles fall

Err hello,
i have no idea if this is the right website be i need to figure out how of make a few rectangles fall from the bottom of the screen in a consistent loop but they fall at different or random locations every time the come on screen.
take a look at my code:

//variables 
float xmovement ;
float whereIsIt ;
float one = -60; 
float two = -150 ; 
float three = -110; 
float four = -200 ;
float five = -130 ; 
float six = -200 ;
float seven = -450; 
float eight = -200;
float nine = -300;
int Xposition;
//setup 
void setup() { 
  size (370, 500) ;
  background (255) ;
  xmovement = -20 ;
  Xposition=250;
}

//rectangles 
void draw () { 

  frameRate (100) ;
  fill (0) ;
  noStroke() ;
  rectMode (CENTER) ;
  rect (185, 250, 350, 480) ;
  fill (255) ;
  rect (mouseX, 460, 35, 10) ;


  // falling obstackles 
  rect (150, xmovement, 9, 23) ;
  rect (Xposition, one, 9, 23) ;
  rect (250, two, 9, 23) ;
  rect (100, three, 9, 23) ;
  rect (270, four, 9, 23) ;
  rect (300, five, 9, 23) ;
  rect (90, six, 9, 23) ;
  rect (40, seven, 9, 23) ;
  rect (160, eight, 9, 23) ;
  rect (230, nine, 9, 23) ;


  // logic 
  xmovement = xmovement + random(1, 6) ;
  one = one + 4 ;
  two = two + 3 ;
  three  = three + 4.5;
  four = four + 2;
  five = five + 5 ;
  six = six + 2;
  seven = seven + 5;
  eight = eight + 4; 
  nine = nine + 2.8;

  } 
}

You need a class Rectangle and an ArrayList see Daniel Shiffman videos on ArrayList:

Hi Processingn00b,

Can you please format your code in your previous message?

In order to do that, you need to click on the pen icon in the bottom right of your post to enter edit mode.
Now you can select your code and press the </> icon in the text editor toolbar.
Be sure that you selected all your code and hit ctrl + t inside of processing IDE to properly indent the code.

Thank you :slight_smile:

Oh dear. Mine are going sideways. Oh dear. This is no good at all.

// What is a Rectangle?
class Rectangle {
  // It has a position.
  float x, y;
  // It has a velocity.
  float vx, vy;
  // It has a size.
  float w, h;
  // We can make one.
  Rectangle() {
    // Set it's size.
    w = 9;
    h = 23;
    // Set its starting position.
    new_position(); // Also sets velocities!
  }
  void new_position() {
    y = random(w, width-w);
    x = random(-height, -h);
    vx = random(2, 5);
    vy = 0;
  }
  // What happens when you draw it?
  void draw() {
    // It moves.
    simulate();
    // It gets drawn.
    render();
  }
  void simulate() {
    x+=vx;
    y+=vy;
    // Has it fallen off the screen?
    if ( y > height ) {
      // Reset its position.
      new_position();
    }
  }
  void render() {
    rect(x, y, w, h);
  }
}

// -----

// Have some objects.
Rectangle[] rects = new Rectangle[10];

void setup() {
  size (370, 500) ;
  // Setup the rectangles.
  for (int i = 0; i < rects.length; rects[i++] = new Rectangle() );
}

void draw () {
  background(0);
  // Draw them.
  for ( Rectangle r : rects ) r.draw();
}

if you are trying to make the rectangles move down, you should change the new_position() method. The vy variable should be random, and the vx should be zero. Like this:

  void new_position() {
    y = random(w, width-w);
    x = random(-height, -h);
    vx = 0;
    vy = random(2, 5);
  }

:relieved: Well at least OP doesn’t have to fix that now… :crazy_face:

2 Likes

sorry i think i said this the wrong way but i want the rectangles that are moving down to be in a loop moving downwards and i don’t know how to do that, so when it goes off screen it starts at the top and moves downwards and repeats that cycle over and over again.

instead of calling new_position when (y > height), just set y to 0. That should work.