# Make a few rectangles fall

**URL:** https://discourse.processing.org/t/make-a-few-rectangles-fall/3360
**Category:** Coding Questions
**Created:** [September 8, 2018, 6:25am UTC](https://discourse.processing.org/t/make-a-few-rectangles-fall/3360 "2018-09-08T06:25:41Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Processingn00b](https://avatars.discourse-cdn.com/v4/letter/p/3ec8ea/32.png) [@Processingn00b](https://discourse.processing.org/u/Processingn00b)
#### Post date: [September 8, 2018, 6:25am UTC](https://discourse.processing.org/t/make-a-few-rectangles-fall/3360/1 "2018-09-08T06:25:42Z")

</div>

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:

```auto
//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;

  } 
}

```

---

<div class="post-metadata">

### Author: ![BrokenCode](https://avatars.discourse-cdn.com/v4/letter/b/9de0a6/32.png) [@BrokenCode](https://discourse.processing.org/u/BrokenCode)
#### Post date: [September 8, 2018, 7:56am UTC](https://discourse.processing.org/t/make-a-few-rectangles-fall/3360/2 "2018-09-08T07:56:24Z")

</div>

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

[![](https://img.youtube.com/vi/HnSJZ4qTcwY/hqdefault.jpg "4.2: ArrayLists in Processing - The Nature of Code") ](https://www.youtube.com/watch?v=HnSJZ4qTcwY)

---

<div class="post-metadata">

### Author: ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)
#### Post date: [September 8, 2018, 7:59am UTC](https://discourse.processing.org/t/make-a-few-rectangles-fall/3360/3 "2018-09-08T07:59:37Z")

</div>

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 🙂

---

<div class="post-metadata">

### Author: ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)
#### Post date: [September 8, 2018, 8:36am UTC](https://discourse.processing.org/t/make-a-few-rectangles-fall/3360/4 "2018-09-08T08:36:50Z")

</div>

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

```auto
// 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();
}

```

---

<div class="post-metadata">

### Author: ![Quillbert1](https://avatars.discourse-cdn.com/v4/letter/q/f475e1/32.png) [@Quillbert1](https://discourse.processing.org/u/Quillbert1)
#### Post date: [September 8, 2018, 4:34pm UTC](https://discourse.processing.org/t/make-a-few-rectangles-fall/3360/5 "2018-09-08T16:34:47Z")

</div>

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:

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

```

---

<div class="post-metadata">

### Author: ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)
#### Post date: [September 8, 2018, 4:48pm UTC](https://discourse.processing.org/t/make-a-few-rectangles-fall/3360/6 "2018-09-08T16:48:41Z")

</div>

😌 Well at least OP doesn’t have to fix that now… 🤪

---

<div class="post-metadata">

### Author: ![Processingn00b](https://avatars.discourse-cdn.com/v4/letter/p/3ec8ea/32.png) [@Processingn00b](https://discourse.processing.org/u/Processingn00b)
#### Post date: [September 10, 2018, 1:49am UTC](https://discourse.processing.org/t/make-a-few-rectangles-fall/3360/7 "2018-09-10T01:49:30Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Quillbert1](https://avatars.discourse-cdn.com/v4/letter/q/f475e1/32.png) [@Quillbert1](https://discourse.processing.org/u/Quillbert1)
#### Post date: [September 10, 2018, 3:18am UTC](https://discourse.processing.org/t/make-a-few-rectangles-fall/3360/8 "2018-09-10T03:18:44Z")

</div>

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