俄罗斯方块 - 修复硬跌落碰撞

时间:2017-04-15 20:53:38

标签: java tetris

我正在编写一种方法来处理俄罗斯方块的方向,并且对编写硬降的逻辑感到困惑。只要按下一个键,就会调用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;
        }
    }
}

我的想法是找到最大的开放空间,这个区块可以很难掉落,然后它会重复下行,直到它撞到那个空间才能下降。

  

编辑:这是我的canMove方法,我已经改变了我的案例丢失,即时丢弃确实有效,但是,当使用drop key时,似乎存在碰撞问题。我不太明白为什么

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;
}

1 个答案:

答案 0 :(得分:0)

DROP案例中所需的更改

case DROP:
        while(canMove(DOWN)){
            row =  row + 1;  
        }
        break;