我也来开发2048之终极奥义

本次教程跟之前隔了不少时间哈,有点忘记了的建议先看看前面的熟悉下,今天我准备把这个2048给结束了,拖了这么久。


按照惯例,我们已经把准备工作都做好了,今天这一部分信息量比较大,也是整个游戏的核心所在,所以我准备分功能来讲,最后大家结合源码来看就不会感觉太吃力了。


1、初始化游戏

初始化的时候,我们要干嘛呢,首先要看配置,配置了几行,然后先画好面板,然后要给在面板上随机生成2个数字Item,这涉及到2个方法,一个是初始化面板,一个是添加随机数字

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. private void initGameView(int cardSize) {  
  2.     removeAllViews();  
  3.     GameItem card;  
  4.     for (int i = 0; i < gameLines; i++) {  
  5.         for (int j = 0; j < gameLines; j++) {  
  6.         card = new GameItem(getContext(), 0);  
  7.         addView(card, cardSize, cardSize);  
  8.         // 初始化GameMatrix全部为0 空格List为所有  
  9.         gameMatrix[i][j] = card;  
  10.         blanks.add(new Point(i, j));  
  11.         }  
  12.     }  
  13.     // 添加随机数字  
  14.     addRandomNum();  
  15.     addRandomNum();  
  16.     }  

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. private void addRandomNum() {  
  2.     getBlanks();  
  3.     if (blanks.size() > 0) {  
  4.         int randomNum = (int) (Math.random() * blanks.size());  
  5.         Point randomPoint = blanks.get(randomNum);  
  6.         gameMatrix[randomPoint.x][randomPoint.y].setNum(Math.random() > 0.2d ? 2 : 4);  
  7.         Game.getGameActivity().getAnimationLayer().animcreate(gameMatrix[randomPoint.x][randomPoint.y]);  
  8.     }  
  9.     }  

在判断生成2、4的时候,我们使用了通常产生范围随机数的方法,同时指定2和4的比例在1:4,当然这个大家可以根据需要更改


2、具体参数等初始化

这个比较简单了,直接上代码,大家应该都看得懂

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. private void initGameMatrix() {  
  2.     // 初始化矩阵  
  3.     removeAllViews();  
  4.     scoreHistory = 0;  
  5.     Config.Scroe = 0;  
  6.     Config.GameLines = Config.sp.getInt(Config.KEY_GameLines, 4);  
  7.     gameLines = Config.GameLines;  
  8.     gameMatrix = new GameItem[gameLines][gameLines];  
  9.     gameMatrixHistory = new int[gameLines][gameLines];  
  10.     calList = new ArrayList<Integer>();  
  11.     blanks = new ArrayList<Point>();  
  12.     highScore = Config.sp.getInt(Config.KEY_HighScore, 0);  
  13.     setColumnCount(gameLines);  
  14.     setRowCount(gameLines);  
  15.     setOnTouchListener(this);  
  16.     // 初始化View参数  
  17.     DisplayMetrics metrics = new DisplayMetrics();  
  18.     WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);  
  19.     Display display = wm.getDefaultDisplay();  
  20.     display.getMetrics(metrics);  
  21.     Config.ItemSize = metrics.widthPixels / Config.GameLines;  
  22.     initGameView(Config.ItemSize);  
  23.     }  

这部分最好还是结合源码看比较好,注释清晰


3、触摸事件

触摸的时候判断4个方向,这个基本也是通用的写法了,不多说了

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. public boolean onTouch(View v, MotionEvent event) {  
  2.     switch (event.getAction()) {  
  3.     case MotionEvent.ACTION_DOWN:  
  4.         saveHistoryMatrix();  
  5.         startX = (int) event.getX();  
  6.         startY = (int) event.getY();  
  7.         break;  
  8.     case MotionEvent.ACTION_MOVE:  
  9.         break;  
  10.     case MotionEvent.ACTION_UP:  
  11.         endX = (int) event.getX();  
  12.         endY = (int) event.getY();  
  13.         judgeDirection(endX - startX, endY - startY);  
  14.         if (isMoved()) {  
  15.         addRandomNum();  
  16.         // 修改显示分数  
  17.         Game.getGameActivity().setScore(Config.Scroe, 0);  
  18.         }  
  19.         int result = checkCompleted();  
  20.         if (result == 0) {  
  21.         if (Config.Scroe > highScore) {  
  22.             Editor editor = Config.sp.edit();  
  23.             editor.putInt(Config.KEY_HighScore, Config.Scroe);  
  24.             editor.commit();  
  25.         }  
  26.         Toast.makeText(getContext(), "lose", Toast.LENGTH_LONG).show();  
  27.         Config.Scroe = 0;  
  28.         } else if (result == 2) {  
  29.         Toast.makeText(getContext(), "success", Toast.LENGTH_LONG).show();  
  30.         Config.Scroe = 0;  
  31.         }  
  32.         break;  
  33.     default:  
  34.         break;  
  35.     }  
  36.     return true;  
  37.     }  
其中判断偏移的方法

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. private void judgeDirection(int offsetX, int offsetY) {  
  2.     if (Math.abs(offsetX) > Math.abs(offsetY)) {  
  3.         if (offsetX > 10) {  
  4.         swipeRight();  
  5.         } else {  
  6.         swipeLeft();  
  7.         }  
  8.     } else {  
  9.         if (offsetY > 10) {  
  10.         swipeDown();  
  11.         } else {  
  12.         swipeUp();  
  13.         }  
  14.     }  
  15.     }  

下面我们来讲2048的终极奥义了,就是算法的实现,具体的算法在第一篇中已经讲解了大概的过程, 点我去看 ,下面我们选一个方向来讲如何实现

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. private void swipeLeft() {  
  2.     for (int i = 0; i < gameLines; i++) {  
  3.         for (int j = 0; j < gameLines; j++) {  
  4.         int currentNum = gameMatrix[i][j].getNum();  
  5.         if (currentNum != 0) {  
  6.             if (keyItemNum == -1) {  
  7.             keyItemNum = currentNum;  
  8.             } else {  
  9.             if (keyItemNum == currentNum) {  
  10.                 calList.add(keyItemNum * 2);  
  11.                 Config.Scroe += keyItemNum * 2;  
  12.                 keyItemNum = -1;  
  13.             } else {  
  14.                 calList.add(keyItemNum);  
  15.                 keyItemNum = currentNum;  
  16.             }  
  17.             }  
  18.         } else {  
  19.             continue;  
  20.         }  
  21.         }  
  22.         if (keyItemNum != -1) {  
  23.         calList.add(keyItemNum);  
  24.         }  
  25.         // 改变Item值  
  26.         for (int j = 0; j < calList.size(); j++) {  
  27.         gameMatrix[i][j].setNum(calList.get(j));  
  28.         }  
  29.         for (int m = calList.size(); m < gameLines; m++) {  
  30.         gameMatrix[i][m].setNum(0);  
  31.         }  
  32.         // 重置行参数  
  33.         keyItemNum = -1;  
  34.         calList.clear();  
  35.     }  
  36.     }  

概括来说,就是选取基准,挨个比较,重新排列

代码很清晰,大家看看就知道了,关键是如何总结出这个算法。


4、下面就是判断游戏什么时候需要新加入一个数字Item

当当前的数字矩阵结构域上次的结构发生差别的时候,我们就要add一个新的数字Item了

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. private boolean isMoved() {  
  2.     for (int i = 0; i < gameLines; i++) {  
  3.         for (int j = 0; j < gameLines; j++) {  
  4.         if (gameMatrixHistory[i][j] != gameMatrix[i][j].getNum()) {  
  5.             return true;  
  6.         }  
  7.         }  
  8.     }  
  9.     return false;  
  10.     }  

这个地方我们使用了一个历史矩阵来存储上一次的数字矩阵


5、最后就是如何判断结束了

如果当前还有空格的Item,则必定没有结束,若相邻的数字都没有相同的数字,则必定结束,若出现配置的Goal,则赢了

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. private int checkCompleted() {  
  2.     getBlanks();  
  3.     if (blanks.size() == 0) {  
  4.         for (int i = 0; i < gameLines; i++) {  
  5.         for (int j = 0; j < gameLines; j++) {  
  6.             if (j < gameLines - 1) {  
  7.             if (gameMatrix[i][j].getNum() == gameMatrix[i][j + 1].getNum()) {  
  8.                 return 1;  
  9.             }  
  10.             }  
  11.             if (i < gameLines - 1) {  
  12.             if (gameMatrix[i][j].getNum() == gameMatrix[i + 1][j].getNum()) {  
  13.                 return 1;  
  14.             }  
  15.             }  
  16.         }  
  17.         }  
  18.         return 0;  
  19.     }  
  20.     for (int i = 0; i < gameLines; i++) {  
  21.         for (int j = 0; j < gameLines; j++) {  
  22.         if (gameMatrix[i][j].getNum() == 2048) {  
  23.             return 2;  
  24.         }  
  25.         }  
  26.     }  
  27.     return 1;  
  28.     }  

下面放出一些最终的图片:



由于刚换了工作,上下班时间倍增,所以写代码的时间也少了,其实关于这个游戏我还有很多想法没做,这个版本的2048,比网上的版本,多了撤销上次移动功能,多了可以定制游戏维数的功能,多了配置目标值的功能,我这个版本的2048,配置要求极低,相比cocos2dx版本的来说,极大的降低了电量消耗,可配置性更强。这些都是我在玩的过程中,觉得不爽的地方,然后改进的,还有一些想法,目前还没有去做,平时工作去做android的framework了,应用层也就研究的少了,希望大家能改进我的代码,做出更好的2048。

1、添加debug功能,也称作弊后门,我原来是想,当手指滑动超过多少距离后,调用一个新方法,设置添加的随机数的位置和大小,这个用我现在的代码是很好实现的,只要把addRandom方法改下,写一个debugRandom方法就OK了

2、分享功能,这个用ShareSdk就可以了,玩游戏嘛,就是要大家一起玩才好玩,无社交不游戏

3、更换2、4、8、16……数字的背景,这个网上很多了,我们也可以自定义一套背景,这个实现也是比较简单的,只要把GameItem这个类里面的Item的背景添加一个set方法就ok了

以上,终了

以下,刷代码

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. package com.xys.game2048.view;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import android.content.Context;  
  7. import android.content.SharedPreferences.Editor;  
  8. import android.graphics.Point;  
  9. import android.util.AttributeSet;  
  10. import android.util.DisplayMetrics;  
  11. import android.view.Display;  
  12. import android.view.MotionEvent;  
  13. import android.view.View;  
  14. import android.view.View.OnTouchListener;  
  15. import android.view.WindowManager;  
  16. import android.widget.GridLayout;  
  17. import android.widget.Toast;  
  18.   
  19. import com.xys.game2048.activity.Game;  
  20. import com.xys.game2048.bean.GameItem;  
  21. import com.xys.game2048.config.Config;  
  22.   
  23. public class GameView extends GridLayout implements OnTouchListener {  
  24.   
  25.     // GameView对应矩阵  
  26.     private GameItem[][] gameMatrix;  
  27.     // 空格List  
  28.     private List<Point> blanks;  
  29.     // 矩阵行列数  
  30.     private int gameLines;  
  31.     // 记录坐标  
  32.     private int startX, startY, endX, endY;  
  33.     // 辅助数组  
  34.     private List<Integer> calList;  
  35.     private int keyItemNum = -1;  
  36.     // 历史记录数组  
  37.     private int[][] gameMatrixHistory;  
  38.     // 历史记录分数  
  39.     private int scoreHistory;  
  40.     // 最高记录  
  41.     private int highScore;  
  42.   
  43.     public GameView(Context context) {  
  44.     super(context);  
  45.     initGameMatrix();  
  46.     }  
  47.   
  48.     public GameView(Context context, AttributeSet attrs) {  
  49.     super(context, attrs);  
  50.     initGameMatrix();  
  51.     }  
  52.   
  53.     public void startGame() {  
  54.     initGameMatrix();  
  55.     initGameView(Config.ItemSize);  
  56.     }  
  57.   
  58.     private void initGameView(int cardSize) {  
  59.     removeAllViews();  
  60.     GameItem card;  
  61.     for (int i = 0; i < gameLines; i++) {  
  62.         for (int j = 0; j < gameLines; j++) {  
  63.         card = new GameItem(getContext(), 0);  
  64.         addView(card, cardSize, cardSize);  
  65.         // 初始化GameMatrix全部为0 空格List为所有  
  66.         gameMatrix[i][j] = card;  
  67.         blanks.add(new Point(i, j));  
  68.         }  
  69.     }  
  70.     // 添加随机数字  
  71.     addRandomNum();  
  72.     addRandomNum();  
  73.     }  
  74.   
  75.     /** 
  76.      * 撤销上次移动 
  77.      */  
  78.     public void revertGame() {  
  79.     if (gameMatrixHistory.length != 0) {  
  80.         Game.getGameActivity().setScore(scoreHistory, 0);  
  81.         Config.Scroe = scoreHistory;  
  82.         for (int i = 0; i < gameLines; i++) {  
  83.         for (int j = 0; j < gameLines; j++) {  
  84.             gameMatrix[i][j].setNum(gameMatrixHistory[i][j]);  
  85.         }  
  86.         }  
  87.     }  
  88.     }  
  89.   
  90.     /** 
  91.      * 添加随机数字 
  92.      */  
  93.     private void addRandomNum() {  
  94.     getBlanks();  
  95.     if (blanks.size() > 0) {  
  96.         int randomNum = (int) (Math.random() * blanks.size());  
  97.         Point randomPoint = blanks.get(randomNum);  
  98.         gameMatrix[randomPoint.x][randomPoint.y].setNum(Math.random() > 0.2d ? 2 : 4);  
  99.         Game.getGameActivity().getAnimationLayer().animcreate(gameMatrix[randomPoint.x][randomPoint.y]);  
  100.     }  
  101.     }  
  102.   
  103.     /** 
  104.      * 获取空格Item数组 
  105.      */  
  106.     private void getBlanks() {  
  107.     blanks.clear();  
  108.     for (int i = 0; i < gameLines; i++) {  
  109.         for (int j = 0; j < gameLines; j++) {  
  110.         if (gameMatrix[i][j].getNum() == 0) {  
  111.             blanks.add(new Point(i, j));  
  112.         }  
  113.         }  
  114.     }  
  115.     }  
  116.   
  117.     /** 
  118.      * 初始化View 
  119.      */  
  120.     private void initGameMatrix() {  
  121.     // 初始化矩阵  
  122.     removeAllViews();  
  123.     scoreHistory = 0;  
  124.     Config.Scroe = 0;  
  125.     Config.GameLines = Config.sp.getInt(Config.KEY_GameLines, 4);  
  126.     gameLines = Config.GameLines;  
  127.     gameMatrix = new GameItem[gameLines][gameLines];  
  128.     gameMatrixHistory = new int[gameLines][gameLines];  
  129.     calList = new ArrayList<Integer>();  
  130.     blanks = new ArrayList<Point>();  
  131.     highScore = Config.sp.getInt(Config.KEY_HighScore, 0);  
  132.     setColumnCount(gameLines);  
  133.     setRowCount(gameLines);  
  134.     setOnTouchListener(this);  
  135.     // 初始化View参数  
  136.     DisplayMetrics metrics = new DisplayMetrics();  
  137.     WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);  
  138.     Display display = wm.getDefaultDisplay();  
  139.     display.getMetrics(metrics);  
  140.     Config.ItemSize = metrics.widthPixels / Config.GameLines;  
  141.     initGameView(Config.ItemSize);  
  142.     }  
  143.   
  144.     @Override  
  145.     public boolean onTouch(View v, MotionEvent event) {  
  146.     switch (event.getAction()) {  
  147.     case MotionEvent.ACTION_DOWN:  
  148.         saveHistoryMatrix();  
  149.         startX = (int) event.getX();  
  150.         startY = (int) event.getY();  
  151.         break;  
  152.     case MotionEvent.ACTION_MOVE:  
  153.         break;  
  154.     case MotionEvent.ACTION_UP:  
  155.         endX = (int) event.getX();  
  156.         endY = (int) event.getY();  
  157.         judgeDirection(endX - startX, endY - startY);  
  158.         if (isMoved()) {  
  159.         addRandomNum();  
  160.         // 修改显示分数  
  161.         Game.getGameActivity().setScore(Config.Scroe, 0);  
  162.         }  
  163.         int result = checkCompleted();  
  164.         if (result == 0) {  
  165.         if (Config.Scroe > highScore) {  
  166.             Editor editor = Config.sp.edit();  
  167.             editor.putInt(Config.KEY_HighScore, Config.Scroe);  
  168.             editor.commit();  
  169.         }  
  170.         Toast.makeText(getContext(), "lose", Toast.LENGTH_LONG).show();  
  171.         Config.Scroe = 0;  
  172.         } else if (result == 2) {  
  173.         Toast.makeText(getContext(), "success", Toast.LENGTH_LONG).show();  
  174.         Config.Scroe = 0;  
  175.         }  
  176.         break;  
  177.     default:  
  178.         break;  
  179.     }  
  180.     return true;  
  181.     }  
  182.   
  183.     /** 
  184.      * 保存历史记录 
  185.      */  
  186.     private void saveHistoryMatrix() {  
  187.     scoreHistory = Config.Scroe;  
  188.     for (int i = 0; i < gameLines; i++) {  
  189.         for (int j = 0; j < gameLines; j++) {  
  190.         gameMatrixHistory[i][j] = gameMatrix[i][j].getNum();  
  191.         }  
  192.     }  
  193.     }  
  194.   
  195.     /** 
  196.      * 根据偏移量判断移动方向 
  197.      *  
  198.      * @param offsetX 
  199.      * @param offsetY 
  200.      */  
  201.     private void judgeDirection(int offsetX, int offsetY) {  
  202.     if (Math.abs(offsetX) > Math.abs(offsetY)) {  
  203.         if (offsetX > 10) {  
  204.         swipeRight();  
  205.         } else {  
  206.         swipeLeft();  
  207.         }  
  208.     } else {  
  209.         if (offsetY > 10) {  
  210.         swipeDown();  
  211.         } else {  
  212.         swipeUp();  
  213.         }  
  214.     }  
  215.     }  
  216.   
  217.     /** 
  218.      * 判断是否结束 
  219.      *  
  220.      * @return 0:结束 1:正常 2:成功 
  221.      */  
  222.     private int checkCompleted() {  
  223.     getBlanks();  
  224.     if (blanks.size() == 0) {  
  225.         for (int i = 0; i < gameLines; i++) {  
  226.         for (int j = 0; j < gameLines; j++) {  
  227.             if (j < gameLines - 1) {  
  228.             if (gameMatrix[i][j].getNum() == gameMatrix[i][j + 1].getNum()) {  
  229.                 return 1;  
  230.             }  
  231.             }  
  232.             if (i < gameLines - 1) {  
  233.             if (gameMatrix[i][j].getNum() == gameMatrix[i + 1][j].getNum()) {  
  234.                 return 1;  
  235.             }  
  236.             }  
  237.         }  
  238.         }  
  239.         return 0;  
  240.     }  
  241.     for (int i = 0; i < gameLines; i++) {  
  242.         for (int j = 0; j < gameLines; j++) {  
  243.         if (gameMatrix[i][j].getNum() == 2048) {  
  244.             return 2;  
  245.         }  
  246.         }  
  247.     }  
  248.     return 1;  
  249.     }  
  250.   
  251.     /** 
  252.      * 判断是否移动过(是否需要新增Item) 
  253.      *  
  254.      * @return 
  255.      */  
  256.     private boolean isMoved() {  
  257.     for (int i = 0; i < gameLines; i++) {  
  258.         for (int j = 0; j < gameLines; j++) {  
  259.         if (gameMatrixHistory[i][j] != gameMatrix[i][j].getNum()) {  
  260.             return true;  
  261.         }  
  262.         }  
  263.     }  
  264.     return false;  
  265.     }  
  266.   
  267.     /** 
  268.      * 滑动事件:上 
  269.      */  
  270.     private void swipeUp() {  
  271.     for (int i = 0; i < gameLines; i++) {  
  272.         for (int j = 0; j < gameLines; j++) {  
  273.         int currentNum = gameMatrix[j][i].getNum();  
  274.         if (currentNum != 0) {  
  275.             if (keyItemNum == -1) {  
  276.             keyItemNum = currentNum;  
  277.             } else {  
  278.             if (keyItemNum == currentNum) {  
  279.                 calList.add(keyItemNum * 2);  
  280.                 Config.Scroe += keyItemNum * 2;  
  281.                 keyItemNum = -1;  
  282.             } else {  
  283.                 calList.add(keyItemNum);  
  284.                 keyItemNum = currentNum;  
  285.             }  
  286.             }  
  287.         } else {  
  288.             continue;  
  289.         }  
  290.         }  
  291.         if (keyItemNum != -1) {  
  292.         calList.add(keyItemNum);  
  293.         }  
  294.         // 改变Item值  
  295.         for (int j = 0; j < calList.size(); j++) {  
  296.         gameMatrix[j][i].setNum(calList.get(j));  
  297.         }  
  298.         for (int m = calList.size(); m < gameLines; m++) {  
  299.         gameMatrix[m][i].setNum(0);  
  300.         }  
  301.         // 重置行参数  
  302.         keyItemNum = -1;  
  303.         calList.clear();  
  304.     }  
  305.     }  
  306.   
  307.     /** 
  308.      * 滑动事件:下 
  309.      */  
  310.     private void swipeDown() {  
  311.     for (int i = gameLines - 1; i >= 0; i--) {  
  312.         for (int j = gameLines - 1; j >= 0; j--) {  
  313.         int currentNum = gameMatrix[j][i].getNum();  
  314.         if (currentNum != 0) {  
  315.             if (keyItemNum == -1) {  
  316.             keyItemNum = currentNum;  
  317.             } else {  
  318.             if (keyItemNum == currentNum) {  
  319.                 calList.add(keyItemNum * 2);  
  320.                 Config.Scroe += keyItemNum * 2;  
  321.                 keyItemNum = -1;  
  322.             } else {  
  323.                 calList.add(keyItemNum);  
  324.                 keyItemNum = currentNum;  
  325.             }  
  326.             }  
  327.         } else {  
  328.             continue;  
  329.         }  
  330.         }  
  331.         if (keyItemNum != -1) {  
  332.         calList.add(keyItemNum);  
  333.         }  
  334.         // 改变Item值  
  335.         for (int j = 0; j < gameLines - calList.size(); j++) {  
  336.         gameMatrix[j][i].setNum(0);  
  337.         }  
  338.         int index = calList.size() - 1;  
  339.         for (int m = gameLines - calList.size(); m < gameLines; m++) {  
  340.         gameMatrix[m][i].setNum(calList.get(index));  
  341.         index--;  
  342.         }  
  343.         // 重置行参数  
  344.         keyItemNum = -1;  
  345.         calList.clear();  
  346.         index = 0;  
  347.     }  
  348.     }  
  349.   
  350.     /** 
  351.      * 滑动事件:左 
  352.      */  
  353.     private void swipeLeft() {  
  354.     for (int i = 0; i < gameLines; i++) {  
  355.         for (int j = 0; j < gameLines; j++) {  
  356.         int currentNum = gameMatrix[i][j].getNum();  
  357.         if (currentNum != 0) {  
  358.             if (keyItemNum == -1) {  
  359.             keyItemNum = currentNum;  
  360.             } else {  
  361.             if (keyItemNum == currentNum) {  
  362.                 calList.add(keyItemNum * 2);  
  363.                 Config.Scroe += keyItemNum * 2;  
  364.                 keyItemNum = -1;  
  365.             } else {  
  366.                 calList.add(keyItemNum);  
  367.                 keyItemNum = currentNum;  
  368.             }  
  369.             }  
  370.         } else {  
  371.             continue;  
  372.         }  
  373.         }  
  374.         if (keyItemNum != -1) {  
  375.         calList.add(keyItemNum);  
  376.         }  
  377.         // 改变Item值  
  378.         for (int j = 0; j < calList.size(); j++) {  
  379.         gameMatrix[i][j].setNum(calList.get(j));  
  380.         }  
  381.         for (int m = calList.size(); m < gameLines; m++) {  
  382.         gameMatrix[i][m].setNum(0);  
  383.         }  
  384.         // 重置行参数  
  385.         keyItemNum = -1;  
  386.         calList.clear();  
  387.     }  
  388.     }  
  389.   
  390.     /** 
  391.      * 滑动事件:右 
  392.      */  
  393.     private void swipeRight() {  
  394.     for (int i = gameLines - 1; i >= 0; i--) {  
  395.         for (int j = gameLines - 1; j >= 0; j--) {  
  396.         int currentNum = gameMatrix[i][j].getNum();  
  397.         if (currentNum != 0) {  
  398.             if (keyItemNum == -1) {  
  399.             keyItemNum = currentNum;  
  400.             } else {  
  401.             if (keyItemNum == currentNum) {  
  402.                 calList.add(keyItemNum * 2);  
  403.                 Config.Scroe += keyItemNum * 2;  
  404.                 keyItemNum = -1;  
  405.             } else {  
  406.                 calList.add(keyItemNum);  
  407.                 keyItemNum = currentNum;  
  408.             }  
  409.             }  
  410.         } else {  
  411.             continue;  
  412.         }  
  413.         }  
  414.         if (keyItemNum != -1) {  
  415.         calList.add(keyItemNum);  
  416.         }  
  417.         // 改变Item值  
  418.         for (int j = 0; j < gameLines - calList.size(); j++) {  
  419.         gameMatrix[i][j].setNum(0);  
  420.         }  
  421.         int index = calList.size() - 1;  
  422.         for (int m = gameLines - calList.size(); m < gameLines; m++) {  
  423.         gameMatrix[i][m].setNum(calList.get(index));  
  424.         index--;  
  425.         }  
  426.         // 重置行参数  
  427.         keyItemNum = -1;  
  428.         calList.clear();  
  429.         index = 0;  
  430.     }  
  431.     }  
  432. }  



PS 需要源码的请留言


PS 需要源码的请留言



FROM: http://blog.csdn.net/eclipsexys/article/details/24582957

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值