【Android 2D 游戏开发(4)】——俄罗斯方块(手势操作)

效果:
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
核心代码:
Block.java

public class Block{

    public static final int[][][] PIXEL_L = new int[][][]{{{0, 1, 0}, {0, 1, 0},{0,1,1}},{{0, 0, 0}, {1, 1, 1},{1,0,0}},{{1, 1, 0}, {0, 1, 0},{0,1,0}},{{0, 0, 1}, {1, 1, 1},{0,0,0}}};
    public static final int[][][] PIXEL_J = new int[][][]{{{0, 1, 0}, {0, 1, 0},{1,1,0}},{{1, 0, 0}, {1, 1, 1},{0,0,0}},{{0, 1, 1}, {0, 1, 0},{0,1,0}},{{0, 0, 0}, {1, 1, 1},{0,0,1}}};
    public static final int[][][] PIXEL_O = new int[][][]{{{1, 1, 0}, {1, 1, 0},{0,0,0}},{{1, 1, 0}, {1, 1, 0},{0,0,0}},{{1, 1, 0}, {1, 1, 0},{0,0,0}},{{1, 1, 0}, {1, 1, 0},{0,0,0}}};
    public static final int[][][] PIXEL_I = new int[][][]{{{0, 1, 0}, {0, 1, 0},{0,1,0}},{{0, 0, 0}, {1, 1, 1},{0,0,0}},{{0, 1, 0}, {0, 1, 0},{0,1,0}},{{0, 0, 0}, {1, 1, 1},{0,0,0}}};
    public static final int[][][] PIXEL_2 = new int[][][]{{{1, 1, 0}, {0, 1, 0},{0,1,1}},{{0, 0, 1}, {1, 1, 1},{1,0,0}},{{1, 1, 0}, {0, 1, 0},{0,1,1}},{{0, 0, 1}, {1, 1, 1},{1,0,0}}};
    public static final int[][][] PIXEL_5 = new int[][][]{{{0, 1, 1}, {0, 1, 0},{1,1,0}},{{1, 0, 0}, {1, 1, 1},{0,0,1}},{{0, 1, 1}, {0, 1, 0},{1,1,0}},{{1, 0, 0}, {1, 1, 1},{0,0,1}}};
    public static final int[][][] PIXEL_z = new int[][][]{{{1, 1, 0}, {0, 1, 1},{0,0,0}},{{0, 0, 1}, {0, 1, 1},{0,1,0}},{{0, 0, 0}, {1, 1, 0},{0,1,1}},{{0, 1, 0}, {1, 1, 0},{1,0,0}}};
    public static final int[][][] PIXEL_DIAN = new int[][][]{{{0, 0, 0}, {0, 1, 0},{0,0,0}},{{0, 0, 0}, {0, 1, 0},{0,0,0}},{{0, 0, 0}, {0, 1, 0},{0,0,0}},{{0, 0, 0}, {0, 1, 0},{0,0,0}}};
    public static final int[][][] PIXEL_GONG = new int[][][]{{{1, 1, 1}, {0, 1, 0},{1,1,1}},{{1, 0, 1}, {1, 1, 1},{1,0,1}},{{1, 1, 1}, {0, 1, 0},{1,1,1}},{{1, 0, 1}, {1, 1, 1},{1,0,1}}};
    public static final int[][][] PIXEL_T = new int[][][]{{{1, 1, 1}, {0, 1, 0},{0,0,0}},{{0, 0, 1}, {0, 1, 1},{0,0,1}},{{0, 0, 0}, {0, 1, 0},{1,1,1}},{{1, 0, 0}, {1, 1, 0},{1,0,0}}};
    public static final int[][][] PIXEL_x = new int[][][]{{{1, 0, 1}, {0, 1, 0},{1,0,1}},{{1, 0, 1}, {0, 1, 0},{1,0,1}},{{1, 0, 1}, {0, 1, 0},{1,0,1}},{{1, 0, 1}, {0, 1, 0},{1,0,1}}};
    public static final int[][][] PIXEL_2DIAN = new int[][][]{{{0, 1, 0}, {0, 1, 0},{0,0,0}},{{0, 0, 0}, {0, 1, 1},{0,0,0}},{{0, 0, 0}, {0, 1, 0},{0,1,0}},{{0, 0, 0}, {1, 1, 0},{0,0,0}}};
    public static final int[][][] PIXEL_10 = new int[][][]{{{0, 1, 0}, {1, 1, 1},{0,1,0}},{{0, 1, 0}, {1, 1, 1},{0,1,0}},{{0, 1, 0}, {1, 1, 1},{0,1,0}},{{0, 1, 0}, {1, 1, 1},{0,1,0}}};
    public static final int[][][][] PIXEL_ALL = new int[][][][]{PIXEL_L,PIXEL_J,PIXEL_O,PIXEL_I,PIXEL_5,PIXEL_z,PIXEL_DIAN,PIXEL_GONG,PIXEL_T,PIXEL_x,PIXEL_2DIAN,PIXEL_10,PIXEL_L,PIXEL_J,PIXEL_O,PIXEL_I,PIXEL_z,PIXEL_DIAN,PIXEL_2DIAN,PIXEL_DIAN,PIXEL_DIAN};
    ArrayList<int[][]> blockList = new ArrayList<>();
    public int[][] pixel;//记录当前block像素数组
    public int x,y;

    public Block(){
        Random random = new Random();
        int n = random.nextInt(PIXEL_ALL.length);
        for (int i = 0;i < PIXEL_ALL[n].length;i++){
            blockList.add(PIXEL_ALL[n][i]);
        }
        pixel = blockList.get(0);
    }

    //判断x0,y0位置是否能放置当前方块
    public boolean toPosition(int x0,int y0){
        for (int y = 0;y < 3;y++){
            for (int x = 0;x < 3;x++){
                if (pixel[y][x]== 1){
                    if (GameView.tiles[x0+x][y0+y].getType() == Tile.TYPE_DEAD){
                        return false;
                    }
                }
            }
        }
        return true;
    }

    //将当前方块所有的1变成0
    public void removePosition(int x0,int y0){
        for (int y = 0;y < 3;y++){
            for (int x = 0;x < 3;x++){
                if (pixel[y][x]== 1){
                    GameView.tiles[x0+x][y0+y].setType(Tile.TYPE_NULL);
                }
            }
        }
    }

    //将当前方块放在x0,y0位置
    public void addPosition(int x0,int y0){
        for (int y = 0;y < 3;y++){
            for (int x = 0;x < 3;x++){
                if (pixel[y][x]== 1){
                    GameView.tiles[x0+x][y0+y].setType(Tile.TYPE_ALIVE);
                }
            }
        }
    }

    //方块落到地面时,将方块的状态变为静止
    public void stopPosition(int x0,int y0){
        for (int y = 0;y < 3;y++){
            for (int x = 0;x < 3;x++){
                if (pixel[y][x]== 1){
                    GameView.tiles[x0+x][y0+y].setType(Tile.TYPE_DEAD);
                }
            }
        }
    }

    //旋转
    public void spin (){
        pixel = blockList.get(1);
        if (toPosition(x,y)){
            pixel = blockList.get(0);
            removePosition(x,y);
            pixel = blockList.get(1);
            int[][] tempPixel = blockList.get(0);
            blockList.remove(0);
            blockList.add(tempPixel);
        }else {
            pixel = blockList.get(0);
        }
    }

    //加速下落
    public void speed(){
        while (moveDown());
    }

    //左移
    public boolean moveLeft(){
        if (toPosition(x-1,y)){
            removePosition(x,y);
            addPosition(x-1,y);
            x = x-1;
            return true;
        }else {
            return false;
        }
    }

    //右移
    public boolean moveRight(){
        if (toPosition(x+1,y)){
            removePosition(x,y);
            addPosition(x+1,y);
            x = x+1;
            return true;
        }else {
            return false;
        }
    }

    //下落
    public boolean moveDown(){
        if (toPosition(x,y+1)){
            removePosition(x,y);
            addPosition(x,y+1);
            y = y+1;
            return true;
        }else {
            stopPosition(x,y);
            return false;
        }
    }
}

GameView.java

/**
*省略部分代码
**/
    private void playingLogic(){

          if(!block.moveDown()){
              clear();
              block = nextBlock;
              nextBlock = new Block();
              if (block.toPosition(5,0)){
                  block.addPosition(5,0);
                  block.x = 5;
                  block.y = 0;
              }else{
                  gameState = GameState.Over;
              }
          }
    }

    //消除可消除的行
    private void clear(){
        for (int y = 0;y < MAX_Y;y++){
            if (judgeFull(y)){
                clearLine(y);
                score+=100;
            }
        }
    }

    //判断y行是否可消除
    private boolean judgeFull(int y){
        for (int x = 1;x < MAX_X-2;x++){
            if (tiles[x][y].getType() != Tile.TYPE_DEAD)
                return false;
        }
        return true;
    }

    //消除y0行
    private void clearLine(int y0){
        for (int y = y0;y>0;y--){
            for (int x = 1;x < MAX_X-2;x++){
                tiles[x][y].setType(tiles[x][y-1].getType());
            }
        }
    }

说实话,这个逻辑还是挺复杂的,用了我好几个晚上才写完。想用小游戏练习的,可以试试这个俄罗斯方块。
一个建议:当你找不到一个方法去直接解决一个问题时,那就多用几个方法,将功能细分,再组合到一起,逻辑瞬间就清晰了。

源码:http://pan.baidu.com/s/1ciFcmU

欢迎关注我的微信公众号:xinshouit
更新会在里面通知,有其他资源也会放在里面

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值