I am building a tetris and am confused about how I can make a piece of a heavy drop.
I tried adding the hardDrop() method to the Board class, which will hardDrop() over from the bottom line and check open spaces ...
Board Class:
public int hardDrop() { int hardDropRow = 0; for(int row = totalRows-1; row > 0; row--) { for(int col = 0; col < grid[row].length; col++) { if (grid[row][col] != null) { hardDropRow = row; return hardDropRow; } } } return hardDropRow; }
Item Class:
Then here for each part I will add dropRow to getRow() :
//Hard drop if (keycode == KeyEvent.VK_UP) { //get next valid coordinates nearest to bottom //hard drop needs to which row the piece needs to drop to int dropRow; dropRow = board.hardDrop(); for (int i = 0; i < tile.length; i++) { calcNewPosition(tile[i].getRow()+dropRow, tile[i].getCol(), i); }
When I click, the part just skips a few lines, although the whole bottom is zero ... so it should just go to that place.
Any suggestions?
Thanks!
EDIT -
I updated my timer, so it takes on the value ... if val == 1 , that means the instant drop has been deleted ... so it sets the interval = 10 milliseconds ...
If val != 1 , it goes through the case statement.
For some reason, after setting timer(1) , the interval will not change even if I change it ...
Board Class:
public void timer (int val) { int interval; if (val == 1) { interval = 1; } else { switch (level) { //each level increases drop speed by .10 seconds case 1: interval = 700; break; case 2: interval = 600; break; case 3: interval = 500; break; case 4: interval = 400; break; case 5: interval = 300; break; default: interval = 800; break; } } Timer t = new Timer(interval, new ActionListener() { public void actionPerformed(ActionEvent e) { if (pause && gameWon){ message = "You've won!"; } else if (pause) { seconds++; } else { seconds = 0; newPiece.autoMove(); repaint(); } } }); t.start(); }
Piece class: You see that I am sending 1 as the timer() parameter for a hard fall ... Then set it with something other than 1 ...
//hard drop if (keycode == KeyEvent.VK_UP) { board.timer(1); for (int i = 0; i < tile.length; i++) { calcNewPosition(tile[i].getRow()+1, tile[i].getCol(), i); } clearCurrPosition(); for (int i = 0; i < tile.length; i++) { board.checkEndGame(tile[i].getRow(), tile[i].getCol()); } board.timer(0); board.checkBottomFull(); if (isCollision()) board.createNewPiece(); move();