俄罗斯方块 - 修复硬掉落碰撞
Tetris - fix hard drop collision
我正在编写一种方法来处理俄罗斯方块的方向,并且对编写硬下降的逻辑感到困惑。只要按下一个键,就会调用 move 方法。
public void move(Direction direction) {
if (canMove(direction)) {
switch (direction) {
case DOWN:
row = row + 1;
break;
case LEFT:
col = col - 1;
break;
case RIGHT:
col = col + 1;
break;
case DROP:
for(int i = row; i < Grid.HEIGHT; i ++){
if(!grid.isSet(i,col)){
row = row + 1;
}
}
break;
}
}
}
我的想法是找到最远的开放 space 方块可以硬掉的地方,它会重复向下行直到它碰到那个 space 硬掉。
Edit: This is my canMove method and I've changed my case drop, instant drop does work, however, there seems to be a problem with collision when drop key is used. I dont quite understand why
public boolean canMove(Direction direction) {
if (!ableToMove)
return false;
boolean move = true;
// if the given direction is blocked, we can't move
// remember to check the edges of the grid
switch (direction) {
case DOWN:
if (row == (Grid.HEIGHT - 1) || grid.isSet(row + 1, col))
move = false;
break;
case DROP:
if (row == (Grid.HEIGHT - 1) || grid.isSet(row + 1, col))
move = false;
break;
// currently doesn't support checking LEFT or RIGHT
// MODIFY so that it correctly returns if it can move left or right
case LEFT:
if (col == (0) || grid.isSet(row, col-1))
move = false;
break;
case RIGHT:
if (row == (Grid.WIDTH - 1) || grid.isSet(row, col+1))
move = false;
break;
}
return move;
}
DROP
情况下所需的更改是
case DROP:
while(canMove(DOWN)){
row = row + 1;
}
break;
我正在编写一种方法来处理俄罗斯方块的方向,并且对编写硬下降的逻辑感到困惑。只要按下一个键,就会调用 move 方法。
public void move(Direction direction) {
if (canMove(direction)) {
switch (direction) {
case DOWN:
row = row + 1;
break;
case LEFT:
col = col - 1;
break;
case RIGHT:
col = col + 1;
break;
case DROP:
for(int i = row; i < Grid.HEIGHT; i ++){
if(!grid.isSet(i,col)){
row = row + 1;
}
}
break;
}
}
}
我的想法是找到最远的开放 space 方块可以硬掉的地方,它会重复向下行直到它碰到那个 space 硬掉。
Edit: This is my canMove method and I've changed my case drop, instant drop does work, however, there seems to be a problem with collision when drop key is used. I dont quite understand why
public boolean canMove(Direction direction) {
if (!ableToMove)
return false;
boolean move = true;
// if the given direction is blocked, we can't move
// remember to check the edges of the grid
switch (direction) {
case DOWN:
if (row == (Grid.HEIGHT - 1) || grid.isSet(row + 1, col))
move = false;
break;
case DROP:
if (row == (Grid.HEIGHT - 1) || grid.isSet(row + 1, col))
move = false;
break;
// currently doesn't support checking LEFT or RIGHT
// MODIFY so that it correctly returns if it can move left or right
case LEFT:
if (col == (0) || grid.isSet(row, col-1))
move = false;
break;
case RIGHT:
if (row == (Grid.WIDTH - 1) || grid.isSet(row, col+1))
move = false;
break;
}
return move;
}
DROP
情况下所需的更改是
case DROP:
while(canMove(DOWN)){
row = row + 1;
}
break;