【转】【2048强势来袭】第四期:游戏滑动上下左右相加的逻辑

Hello,我们又见啦,这篇我们将来分析一下游戏2048滑动上下左右相加逻辑的实现。

1. 添加一个数组

我们需要借助数组,来实现这个逻辑功能。在HelloWorldScene.h中添加一个数组声明:

  1. //创建一个卡片二位数组
  2. CardSprite* cardArr[4][4]; 

2. 把数字添加到数组中

还记得我们在创建卡片函数createCardSprite函数中得到的卡片card吗?现在,我们就把这个卡片加入到数组中:

  1. //把卡片加入到数组中
  2. cardArr[i][j] = card; 

3. 添加具体的逻辑

(1)向左滑动函数的逻辑代码:

  1. bool HelloWorld::doLeft(){ 
  2. bool isdo = false; 
  3. for (int y = 0; y < 4; y++) { 
  4. for (int x = 0; x < 4; x++) { 
  5. for (int x1 = x + 1; x1 < 4; x1++) { 
  6. if (cardArr[x1][y]->getNumber() > 0) { 
  7. if (cardArr[x][y]->getNumber() <= 0) { 
  8.                         cardArr[x][y]->setNumber(cardArr[x1][y]->getNumber()); 
  9.                         cardArr[x1][y]->setNumber(0); 
  10.                         x–; 
  11.                         isdo = true; 
  12.                     }else if(cardArr[x][y]->getNumber() == cardArr[x1][y]->getNumber()){ 
  13.                         cardArr[x][y]->setNumber(cardArr[x][y]->getNumber()*2); 
  14.                         cardArr[x1][y]->setNumber(0); 
  15.                         isdo = true; 
  16.                     } 
  17. break; 
  18.                 } 
  19.             } 
  20.         } 
  21.     } 
  22. return isdo; 

如果右边的数等于左边的数,那么左边的数乘以2,然后把右边的数设置为0,也就变为了空。

另外三个方向滑动的逻辑代码,也类似。

(2)向右滑动函数的逻辑代码:

  1. bool HelloWorld::doRight(){ 
  2. bool isdo = false; 
  3. for (int y = 0; y < 4; y++) { 
  4. for (int x = 3; x >= 0; x–) { 
  5. for (int x1 = x – 1; x1 >= 0; x1–) { 
  6. if (cardArr[x1][y]->getNumber() > 0) { 
  7. if (cardArr[x][y]->getNumber() <= 0) { 
  8.                         cardArr[x][y]->setNumber(cardArr[x1][y]->getNumber()); 
  9.                         cardArr[x1][y]->setNumber(0); 
  10.                         x++; 
  11.                         isdo = true; 
  12.                     }else if(cardArr[x][y]->getNumber() == cardArr[x1][y]->getNumber()){ 
  13.                         cardArr[x][y]->setNumber(cardArr[x][y]->getNumber()*2); 
  14.                         cardArr[x1][y]->setNumber(0); 
  15.                         isdo = true; 
  16.                     } 
  17. break; 
  18.                 } 
  19.             } 
  20.         } 
  21.     } 
  22. return isdo; 

(3)向下滑动函数的逻辑代码:

  1. bool HelloWorld::doDown(){ 
  2. bool isdo = false; 
  3. for (int x = 0; x < 4; x++) { 
  4. for (int y = 0; y < 4; y++) { 
  5. for (int y1 = y + 1; y1 < 4; y1++) { 
  6. if (cardArr[x][y1]->getNumber() > 0) { 
  7. if (cardArr[x][y]->getNumber() <= 0) { 
  8.                         cardArr[x][y]->setNumber(cardArr[x][y1]->getNumber()); 
  9.                         cardArr[x][y1]->setNumber(0); 
  10.                         y–; 
  11.                         isdo = true; 
  12.                     }else if(cardArr[x][y]->getNumber() == cardArr[x][y1]->getNumber()){ 
  13.                         cardArr[x][y]->setNumber(cardArr[x][y]->getNumber()*2); 
  14.                         cardArr[x][y1]->setNumber(0); 
  15.                         isdo = true; 
  16.                     } 
  17. break; 
  18.                 } 
  19.             } 
  20.         } 
  21.     } 
  22. return isdo; 

(4)向上滑动函数的逻辑代码:

  1. bool HelloWorld::doUp(){ 
  2. bool isdo = false; 
  3. for (int x = 0; x < 4; x++) { 
  4. for (int y = 3; y >= 0; y–) { 
  5. for (int y1 = y – 1; y1 >= 0; y1–) { 
  6. if (cardArr[x][y1]->getNumber() > 0) { 
  7. if (cardArr[x][y]->getNumber() <= 0) { 
  8.                         cardArr[x][y]->setNumber(cardArr[x][y1]->getNumber()); 
  9.                         cardArr[x][y1]->setNumber(0); 
  10.                         y++; 
  11.                         isdo = true; 
  12.                     }else if(cardArr[x][y]->getNumber() == cardArr[x][y1]->getNumber()){ 
  13.                         cardArr[x][y]->setNumber(cardArr[x][y]->getNumber()*2); 
  14.                         cardArr[x][y1]->setNumber(0); 
  15.                         isdo = true; 
  16.                     } 
  17. break; 
  18.                 } 
  19.             } 
  20.         } 
  21.     } 
  22. return isdo; 

4. 玩游戏

运行代码后,效果如下:

image

接下来我们来玩一下游戏,试试

(1)向左滑动,变成下图:

image

(2)向上滑动,变成上图:

image

(3)向右滑动,变成下图:

image

(4)向下滑动,变成下图:

image

嘿嘿,是不是觉得不错呢?赶紧动起手来,自己也搞一个吧!

最后要特别感谢一下帅印。

原文地址:http://blog.csdn.net/legendof1991/article/details/23772661

共有 0 条评论

Top