我正在制作俄罗斯方块,但我不知道如何才能使一块硬下落。
我尝试向hardDrop()
Board 类添加一个方法,该方法将从底行迭代并检查开放空间......
板级:
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;
}
件类:
然后在这里,对于每一件我都会将有效添加dropRow
到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);
}
当我按下时,即使整个底部为空,这块似乎只是跳过了几行......所以它应该移动到那个位置。
有什么建议么?
谢谢!
编辑 -
我已经更新了我的计时器,所以它接受一个值......如果val == 1
,这意味着即时下降被击中......所以它设置间隔 = 10 毫秒......
如果val != 1
,则通过 case 语句。
由于某种原因,一旦timer(1)
设置,即使我更改间隔也不会变回来......
板级:
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 类:您看到我将硬降1
作为参数发送timer()
...然后将其设置为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();