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
- g2048游戏3-android
- g2048游戏1-android
- g2048游戏2-android
- android 3D 游戏实现
- Android游戏
- android游戏
- android 3D游戏开发 - 游戏种类名称介绍
- Android游戏开发:游戏框架的搭建(3)
- Android游戏开发:游戏框架的搭建(3)
- Android游戏开发:游戏框架的搭建(3)
- android游戏开发之我的小小游戏3——俄罗斯方块游戏3
- android游戏开发之我的小小游戏3——俄罗斯方块游戏4
- android游戏开发之我的小小游戏3——俄罗斯方块游戏5
- android游戏开发自学笔记3-3
- android游戏个人经验3[转]
- Android 3D游戏开发(一)
- Android 3D游戏开发(二)
- Android 3D游戏开发(三)
- debtorpmandrpmtodeb
- uclinux很久前笔记10
- 文件上传和下载——文件下载
- HDU2504-又见GCD
- linux命令行(2)---文件处理和权限管理命令
- g2048游戏3-android
- 计算机组成及其功能
- 大数
- 数字图像处理(1)- 透视变换
- uclinux很久前笔记11
- JsBridge最详细的解析,高版本webview的evaluateJavascript
- A100 正确使用递归和灵活使用if
- Hdu 5193 Go to movies Ⅱ(带删除数插入数的逆序数对,块状链表)
- OSGi - 第01章环境准备