Stacker game: how do I loop the rectangles

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…

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

Hello!

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

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

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?

2 Likes