Tetris - Logical Logic

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(); 
+4
source share
1 answer

Tetris is a game that does not require collision control on its own, because everything is limited by the grid, and you can always get the coordinates of the filled squares. Some Tetris variants implement an β€œinstant drop” in which pressing the down (up) button in your case instantly places the current part where it lands at the bottom. Personally, I prefer another option, which should accelerate (significantly, to the point) the speed of the fall of the part.

In any case, I would recommend calling the setFinalPosition() method every time a piece moves (sideways or rotates), which will store the location of the current part if the user does not take any action or drops the piece. Then, if you want to make an β€œinstant drop”, you can just call getFinalPosition() and put the piece there. You can also use the getFinalPosition() method whenever a piece moves even without user action, and just check if the current position matches the end position. If this happens, stop the piece.

Note that regardless of whether you are worried about collisions, look ahead to where the part will end if there is no user action (or if the user ejects the piece), this is the way to go.

+5
source

Source: https://habr.com/ru/post/1481223/


All Articles