# Stacker game: how do I loop the rectangles

**URL:** <https://discourse.processing.org/t/stacker-game-how-do-i-loop-the-rectangles/21341>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [May 27, 2020, 11:06am UTC](https://discourse.processing.org/t/stacker-game-how-do-i-loop-the-rectangles/21341 "2020-05-27T11:06:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![justkayy](https://avatars.discourse-cdn.com/v4/letter/j/b38774/32.png) [@justkayy](https://discourse.processing.org/u/justkayy)\
**Post date:** [May 27, 2020, 11:06am UTC](https://discourse.processing.org/t/stacker-game-how-do-i-loop-the-rectangles/21341/1 "2020-05-27T11:06:38Z")

</div>

So the aim is to:  
**Add a background for the game in the form of a grid. The grid “cell” must be 90 pixels in size in both dimensions when the screen is 1280x720. Note: Do not draw your grid with rectangles or squares, draw it with lines only. We will not adjust the size of your screen when marking this assignment, it only needs to work for 1280x720.**

**Instead of drawing just one square that moves back and forth, have 4 squares in a vertical stack move back and forth. They should move as one and when any of them hit the edge of the window, all of them change direction. NB: You must use a loop to avoid repeating the same code 4 times when drawing 4 squares. Squares should have a black border and be orange (the same orange as assignment one).**

**The most useful module is loops.**

However, my block keeps going down, it wont move back and forth fast…  
I’m also unsure how to loop more rectangles…

```auto
float counter;
float sizeW;
float sizeH;
float dir;
boolean paused;
float x = 0;
float y = 0;
float spacing = 90;

void setup() {
  size(1280, 720);
  counter = 0;
  dir = (float)height/720;
  sizeW = (float)width/14.2;
  sizeH = (float)height/8;
  paused = true;
} 

void draw() {

  background(240);
  stroke(0);
  strokeWeight(1);
  fill(255, 147, 79);
  
    

  // current level
  int steppedPos = (int)(counter/sizeH+0.5);
  rect(0, steppedPos*sizeH, sizeW, sizeH*4);

  // movement
  if (paused) {
    counter = counter + dir;
    if (counter + sizeH > height || counter < 0) {
      dir = dir * -1;
      counter = counter + dir;
    }
    x=90;

    while (x < width) {
      line(x, 0, x, height);
      x = x + spacing;
    }

    y=90;
    while (y<height) {
      line(0, y, width, y);
      y = y + spacing;
    }
    
  }
}

void keyPressed() {
  if (key == ' '){
  paused = !paused;
  }
}

```

---

<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:** [May 27, 2020, 1:55pm UTC](https://discourse.processing.org/t/stacker-game-how-do-i-loop-the-rectangles/21341/2 "2020-05-27T13:55:15Z")

</div>

Hello!

> [@justkayy](#):
>
> you must use a loop to avoid repeating the same code 4 times when drawing 4 squares.

at the moment you draw not 4 normal rects but one over-long rect.

please use a for-loop up to 4 instead and draw 4 times a normal sized rect. I haven’t done this.

Your `paused` variable was wrong. I corrected this

Hey, and welcome to the processing community, great to have you here!

Warm regards,

Chrisir

```auto
float counter=0;
float sizeW;
float sizeH;
float dir;
boolean paused = false; // !!!!!!!!!!!!!!

void setup() {
  size(1280, 720);

  dir = (float)height/720;
  sizeW = (float)width/14.2;
  sizeH = (float)height/8;
}

void draw() {
  background(240);

  stroke(0);
  strokeWeight(1);
  fill(255, 147, 79);

  // show grid
  showGridLines();

  // current level
  int steppedPos = (int)(counter/sizeH+0.5);
  rect(0, steppedPos*sizeH, sizeW, sizeH*4);

  // movement
  if (! paused) { // !!!!!! The ! means NOT 
    counter = counter + dir;
    if (counter + (4*sizeH) > height-2 || counter < 0) {
      dir = dir * -1;
      // counter = counter + dir;
    }
  }
}//func 

// ---------------------------------------------------------------------------

void showGridLines() {
  float x = 0;
  float y = 0;
  float spacing = 90;

  x=90;
  while (x < width) {
    line(x, 0, x, height);
    x = x + spacing;
  }

  y=90;
  while (y<height) {
    line(0, y, width, y);
    y = y + spacing;
  }
}

void keyPressed() {
  if (key == ' ') {
    paused = 
      !paused;
  }
}

```

---

<div class="post-metadata">

**Author:** ![altmattr](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/altmattr/32/6987_2.png) [@altmattr](https://discourse.processing.org/u/altmattr)\
**Post date:** [May 29, 2020, 4:49am UTC](https://discourse.processing.org/t/stacker-game-how-do-i-loop-the-rectangles/21341/3 "2020-05-29T04:49:09Z")

</div>

You will find all the answers in the lecture I gave on Monday. I can’t link to it because it’s only for COMP1000 students but that includes you right?
