Tetris: How to erase line of blocks and have them drop?

edited December 2015 in How To...

Hi guys, I am making a makeshift tetris game using cells as a 2darray with booleans and shapes that are also created from the cell class using 1d arrays. I would like to have a general idea or direction in which to go in that's not too complex but that would eliminate shapes within the row that is completed.

Tagged:

Answers

  • Post your code

    when you have an 2D array in that you show the board in (the tetris field) you just copy all lines to the line below

    So it looks like all lines fall down one line

    The bottom line just gets overwritten from the line above it

  • So I got multiple issues happening. Whenever the second shape starts falling, it somehow detects blocks in the middle of the grid and stops. I suspect the problem lies within the keyPressed(). The clearLine() is my attempt at scanning the board to see how many columns are filled. Someone recommended I set a counter that detects when 10 columns are filled. If 10 are filled in whatever row, turn all to false. Unfortunately I can't even test the clearLine because the second block won't even make it to the bottom if the first block is moved. Also how do I make it so that when the first block falls and hits the bottom, the keyPressed controls switches to the second block. Sorry for the onslaught of questions. Any help or direction would be highly appreciated.

    //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, 1, 25, true);
        shape[1] = new Cell(3, 1, 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();
      clearLine();
    }
    
    void keyPressed() {
      for (int u=0; u<shape.length; u++) {
        if (key == CODED) {
          if (keyCode == LEFT && shape[u].x > 0 ) {
            shape[u].x--;
          }
          if (keyCode == RIGHT && shape[u].x < 9 ) {
            shape[u].x++;
          }
        }
      }
    }
    
    void brickFall() {
      if (millis() > turnTime+200) {
        turnTime = millis();
        for (int u=0; u<shape.length; u++) {
          if (shape[u].y < 19) {
            if (grid[shape[u].x][shape[u].y+1].onOff == false) {
              grid[shape[u].x][shape[u].y].onOff = false;
              shape[u].y++;
              grid[shape[u].x][shape[u].y].onOff = true;
            }
          }
          if (shape[u].y >= 19) {
            if (grid[shape2[u].x][shape2[u].y+1].onOff == false) {
              grid[shape2[u].x][shape2[u].y].onOff = false;
              shape2[u].y++;
              grid[shape2[u].x][shape2[u].y].onOff = true;
            }
          }
        }
      }
    }
    
    void clearLine() {
      boolean isFilled = false;
      int colCounter;
      for (int u=0; u<shape.length; u++) {
        for (int i=0; i<cols; i++) {
          for (int j=0; j<rows; j++) {
            //for(int u=0; u<shape.length; u--) {
            if (grid[i][j].onOff == true) {
              colCounter=+1;
              println(colCounter);
            }
          }
        }
      }
    }
    
    //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 
      int speed = 1;
    
      //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.