Tetris: How to change boolean of Cell grid after each drop of shape?

edited December 2015 in Questions about Code

Hello, this is my first post on openProcessing and I was wondering if I could get some help or advice on how to proceed with my Tetris game. I am making this game as a final project and I understand that it probably would have been wiser to start off with a boolean 2d array for the grid, but I found cells to be a lot easier to handle. I am still slightly a noob, so bear with me.

So I got the grid, I got the shapes, I got them to fall, the only issue now is whenever the shapes fall down, I need them to initiate the cell that they're in to true (grid[][].onOff = true) but switch the grid cell back to false after it has dropped more. This will allow for the stacking effect of blocks. Thanks in advance!

I think the problem is in the brickfall() function. The if() statement that allows it to move is true from the start, but how to change it individually?

//Creates the rows and columns of the grid
int cols;
int rows;
//Declaring a 2d array using the Cell class
Cell[][] grid;
Cell [] shape;
Cell [] shape2;

int turnTime;

int unit = 25;
int unitX, unitY;

void setup() {
  size(10*unit, 20*unit);

  //Rows and columns for the 2d array
  cols = 10;
  rows = 20;

  //array is utilizing the rows and cols with Grid object
  grid = new Cell[cols][rows];
  //instantiating the rows(i) and the cols(j) giving them values
  for (int i=0; i<cols; i++) {
    for (int j=0; j<rows; j++) {
      //array with i&j is equal to the new Grid being declared
      grid[i][j] = new Cell(i, j, 25, false);
    }
  }
  shape = new Cell[2];
  for (int u=0; u<shape.length; u++) {
    shape[0] = new Cell(2, 0, 25, true);
    shape[1] = new Cell(3, 0, 25, true);
  }
  shape2 = new Cell[2];
  for (int h=0; h<shape2.length; h++) {
    shape2[0] = new Cell(2, 0, 25, true);
    shape2[1] = new Cell(3, 0, 25, true);
  }
}

void draw() {
  background(0);
  //for loops for the array to draw
  for (int i=0; i<cols; i++) {
    for (int j=0; j<rows; j++) {
      //Displays grid 2d array
      grid[i][j].display();
    }
  }
  for (int u=0; u<shape.length; u++) {
    shape[u].display2();
    shape2[u].display2();
  }
  brickFall();
}

void keyPressed() {
  for (int i=9; i<=9; i++) {
    for (int j=19; j<=19; j++) {
      for (int u=0; u<shape.length; u++) {
        if (key == CODED) {
          if (keyCode == LEFT && shape[u].x > 0 && grid[i][j].onOff == false) {
            shape[u].x--;
          }
          if (keyCode == RIGHT && shape[u].x < 225 && grid[i][j].onOff == false) {
            shape[u].x++;
          }
        }
      }
    }
  }
}

void brickFall() {
  if (millis() > turnTime+200) {
    turnTime = millis();
    for (int i=9; i<=9; i++) {
      for (int j=19; j<=19; j++) {
        for (int u=0; u<shape.length; u++) {
          //shape[u] = grid[i][j];
          if ( grid[i][j].onOff == false) {
            shape[u].y++;
            if (shape[u].y >= 20) {
              shape[u].y--;
              grid[2][19].onOff=true;
              shape2[u].y++;
            }
          }
        }
      }
    }
  }
}

//Class that creates cells which will create the grid for tetris
class Cell {
  int x, y;
  int cellSize;//Size of cell
  int w, h;
  boolean onOff= false;
  color fillColor = color(0);//Variable to connect shape array to Cell and modify the color of each shape

  //Constructor currently in use
  Cell(int _x, int _y, int _cellSize, boolean _onOff) {
    x=_x;
    y=_y;
    cellSize=_cellSize;
    onOff=_onOff;
  }

  Cell(int _x, int _y, int _w, int _h, boolean _onOff) {
    x=_x;
    y=_y;
    w=_w;
    h=_h;
    onOff=_onOff;
  }

  //Method for the display of each cell
  void display() {
    //noFill();
    fill(fillColor);
    stroke(255);
    rect(x*25, y*25, cellSize, cellSize);//cell size = 30
  }

  void display2() {
    fill(255);
    stroke(0);
    rect(x*25, y*25, cellSize, cellSize);//cell size = 30
  }
}
Sign In or Register to comment.