g2048游戏3-android

来源:互联网 发布:二手软件app软件哪个好 编辑:程序博客网 时间:2024/06/08 20:13

g2048游戏3-android

接着上一篇g2048游戏2-android

g2048数据逻辑和手势监听

手势监听:上下左右滑动
数据逻辑处理:

  • 每滑动一次,所有的数字方块都会往滑动的方向靠拢
  • 相同数字的方块在靠拢、相撞时会相加
  • 系统在空白的地方乱数出现一个2或4数字方块

手势监听:上下左右滑动

使用手势监听中的onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)方法,其中e1表示手指“按下”时的事件,e2表示手指“抬起”时的事件;velocityX、velocityY代表手指“拖过”动作在横向、纵向上的速度

public class InputListener implements GestureDetector.OnGestureListener {    public final static String TAG = "InputListener";    private MainView mView;    public InputListener(MainView view) {        mView = view;    }    @Override    public boolean onDown(MotionEvent motionEvent) {        return false;    }    @Override    public void onShowPress(MotionEvent motionEvent) {    }    @Override    public boolean onSingleTapUp(MotionEvent motionEvent) {        return false;    }    @Override    public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {        return false;    }    @Override    public void onLongPress(MotionEvent motionEvent) {    }    @Override    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {        float minDistance = 120;        float minVelocity = 200;        if (e2.getX() - e1.getX() > minDistance && Math.abs(velocityX) > minVelocity) {            Log.d(TAG, "MOVE_RIGHT");            mView.game.move(mView.game.MOVE_RIGHT);        } else if (e1.getX() - e2.getX() > minDistance && Math.abs(velocityX) > minVelocity) {            Log.d(TAG, "MOVE_LEFT");            mView.game.move(mView.game.MOVE_LEFT);        } else if (e2.getY() - e1.getY() > minDistance && Math.abs(velocityY) > minVelocity) {            Log.d(TAG, "MOVE_DOWN");            mView.game.move(mView.game.MOVE_DOWN);        } else if (e1.getY() - e2.getY() > minDistance && Math.abs(velocityY) > minVelocity) {            Log.d(TAG, "MOVE_UP");            mView.game.move(mView.game.MOVE_UP);        }        return false;    }}

数据逻辑处理

向上滑动
向下滑动

for (int i = 0; i < numSquaresX; i++) {    for (int j = 0; j < numSquaresY; j++) {        handleLogic(i, j, vector, count);    }}

向上滑动
这里写图片描述

for (int i = 0; i < numSquaresX; i++) {    for (int j = numSquaresY - 1; j >= 0; j--) {        handleLogic(i, j, vector, count);    }}

向左滑动
这里写图片描述

for (int j = 0; j < numSquaresY; j++) {    for (int i = 0; i < numSquaresX; i++) {        handleLogic(i, j, vector, count);    }}

向右滑动
这里写图片描述

for (int j = 0; j < numSquaresY; j++) {    for (int i = numSquaresX - 1; i >= 0; i--) {        handleLogic(i, j, vector, count);    }}

查找方向

    private Cell getVector(int direction) {        Cell[] map = {                new Cell(0, 1), // up:0 向下找                new Cell(0, -1), // down:1 向上找                new Cell(1, 0), // left:2 向右找                new Cell(-1, 0) // right:3 向左找        };        return map[direction];    }
public class MainGame {    public final static String TAG = "MainGame";    //Game state    public static final int MOVE_UP = 0;    public static final int MOVE_DOWN = 1;    public static final int MOVE_LEFT = 2;    public static final int MOVE_RIGHT = 3;    public Grid grid = null;    final int numSquaresX = 4;    final int numSquaresY = 4;    private int startNum = 2;    private MainView mView;    public MainGame(MainView view) {        mView = view;    }    public void newGame() {        if (mView.game.grid == null) {            mView.game.grid = new Grid(numSquaresX, numSquaresY);        } else {            mView.game.grid.clearGrid();        }        addStartContent();        mView.invalidate();    }    private void addStartContent() {        for (int xx = 0; xx < startNum; xx++) {            this.addRandomContent();        }    }    private void addRandomContent() {        if (grid.isCellsAvailable()) {            int value = Math.random() < 0.8 ? 2 : 4;            CellData cell = new CellData(mView.game.grid.randomAvailableCell(), value);            grid.insertData(cell);        }    }    //move up=0 ro down=1 or left=2 or right=3    public void move(int direction) {        Cell vector = getVector(direction);        int count = 0;        if (direction == MOVE_UP || direction == MOVE_DOWN) {            count = numSquaresY;        } else {            count = numSquaresX;        }        switch (direction) {            case MOVE_UP: {                for (int i = 0; i < numSquaresX; i++) {                    for (int j = 0; j < numSquaresY; j++) {                        handleLogic(i, j, vector, count);                    }                }                break;            }            case MOVE_DOWN: {                for (int i = 0; i < numSquaresX; i++) {                    for (int j = numSquaresY - 1; j >= 0; j--) {                        handleLogic(i, j, vector, count);                    }                }                break;            }            case MOVE_LEFT: {                for (int j = 0; j < numSquaresY; j++) {                    for (int i = 0; i < numSquaresX; i++) {                        handleLogic(i, j, vector, count);                    }                }                break;            }            case MOVE_RIGHT: {                for (int j = 0; j < numSquaresY; j++) {                    for (int i = numSquaresX - 1; i >= 0; i--) {                        handleLogic(i, j, vector, count);                    }                }                break;            }        }        addRandomContent();        mView.invalidate();    }    //获得移动矢量    private Cell getVector(int direction) {        Cell[] map = {                new Cell(0, 1), // up:0 向下找                new Cell(0, -1), // down:1 向上找                new Cell(1, 0), // left:2 向右找                new Cell(-1, 0) // right:3 向左找        };        return map[direction];    }    private CellData findNextCell(int x, int y, Cell vector, int count) {        for (int i = 0; i < count; i++) {            if (!grid.isCellInBounds(x, y)) {                break;            }            if (null != grid.getCellContent(x, y)) {                return grid.getCellContent(x, y);            }            x = x + vector.getX();            y = y + vector.getY();        }        return null;    }    private void handleLogic(int i, int j, Cell vector, int count) {        CellData curData = null;        CellData next = null;        curData = findNextCell(i, j, vector, count);        if (null != curData) {            next = findNextCell(curData.getX() + vector.getX(), curData.getY() + vector.getY(), vector, count);        }        if (null != curData && null != next) {            if (curData.getValue() == next.getValue()) {                grid.removeData(curData);                grid.removeData(next);                grid.insertData(new CellData(i, j, curData.getValue() + next.getValue()));            } else {                grid.removeData(curData);                grid.insertData(new CellData(i, j, curData.getValue()));            }        } else if (null != curData && null == next) {            grid.removeData(curData);            grid.insertData(new CellData(i, j, curData.getValue()));        }    }}

该g2048程序遗留问题

  • 1、需要添加分数记录、重新启动游戏等
  • 2、需要添加游戏通关和游戏结束逻辑处理
  • 3、滑动时,没有移动数据,需要添加判断,在空白的地方不生成2或4数字方块

程序下载

0 0
原创粉丝点击