cocos2d_x_07_游戏_别踩白块儿

前端之家收集整理的这篇文章主要介绍了cocos2d_x_07_游戏_别踩白块儿前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

最终效果图:

环境版本:cocos2d-x-3.3beta0 没有使用到物理引擎
游戏场景
  1. //
  2. // WhiteSquareScene.h
  3. // 01_cocos2d-x
  4. //
  5. // Created by beyond on 14-10-7.
  6. //
  7. //
  8.  
  9. #ifndef ___1_cocos2d_x__WhiteSquareScene__
  10. #define ___1_cocos2d_x__WhiteSquareScene__
  11.  
  12. #include "Square.h"
  13. #include "EndLine.h"
  14.  
  15. class EndLine;
  16.  
  17. USING_NS_CC;
  18.  
  19.  
  20. class WhiteSquareScene : public cocos2d::Layer
  21. {
  22. private:
  23. // 屏幕尺寸
  24. Size winSize;
  25. // 临时累积 当前 总共添加了多少行 (当到了50行时,添加结束行)
  26. int tempTotalLine;
  27. // 是否 正在 显示 结束行
  28. bool isEndLineShowing;
  29. // 计时器标签
  30. Label *timerLabel;
  31. // 用一个Node 包装起来 所有的东东,方便进行整体控制
  32. Node *_gameLayer;
  33. // 标记 是否正在计时
  34. bool isTimerRunning;
  35. // 游戏开始时的时间戳
  36. long startTime;
  37. // 结束行
  38. EndLine * currentEndLine;
  39. public:
  40. // static create() 方法 内部调用init
  41. CREATE_FUNC(WhiteSquareScene);
  42. virtual bool init();
  43. // 供外界调用(导演)
  44. static cocos2d::Scene* createScene();
  45. // 添加开始行
  46. void addStartLine();
  47. // 添加结束行
  48. void addEndLine();
  49. // 添加 正常的游戏行,最下面是起始行,正常行的 行号 从 1 开始 2、3等等
  50. // 之所以在 添加正常的行时,要传入 行号作为参数,是因为,行号 决定 了该正常行 在屏幕中的Y值
  51. void addNormalLine(int lineIndex);
  52. // 添加 监听当前 Layer的侦听器,如果 点击的是正常游戏行的第一行,就开始游戏
  53. void addLayerTouchHandler();
  54. // 游戏控制 游戏初始化
  55. void initGame();
  56. // 游戏层 整体下移
  57. void gameLayerMoveDown();
  58. void startTimer();
  59. void stopTimer();
  60.  
  61. virtual void update(float dt);
  62. };
  63.  
  64.  
  65. #endif /* defined(___1_cocos2d_x__WhiteSquareScene__) */


  1. //
  2. // WhiteSquareScene.cpp
  3. // 01_cocos2d-x
  4. //
  5. // Created by beyond on 14-10-7.
  6. //
  7. //
  8.  
  9. #include "WhiteSquareScene.h"
  10.  
  11. #define kSquareWidth winSize.width/4
  12. #define kSquareHeight winSize.height/4
  13.  
  14. USING_NS_CC;
  15.  
  16. Scene* WhiteSquareScene::createScene()
  17. {
  18. auto scene = Scene::create();
  19. // create方法 内部会调用init方法
  20. auto layer = WhiteSquareScene::create();
  21. scene->addChild(layer);
  22. return scene;
  23. }
  24. #pragma mark - 添加行(开始行、正常的游戏行、结束行)
  25. // 添加 开始行 占屏幕底部的 四分之一,不用显示文字
  26. void WhiteSquareScene::addStartLine()
  27. {
  28. auto b = Square::createWithArgs(Color3B::YELLOW,Size(winSize.width,kSquareHeight),"",20,Color4B::BLACK);
  29. // 添加到游戏层,方便管理
  30. _gameLayer->addChild(b);
  31. // 绑定 其所在的 行号;开始行 是0,正常行 是1、2、3
  32. b->setLineIndex(0);
  33. }
  34. // 添加 黑白相间的行 (该行中 黑色只有一个,且随机出现)
  35. // 之所以在 添加正常的行时,行号 决定 了该正常行 在屏幕中的Y值
  36. void WhiteSquareScene::addNormalLine(int lineIndex)
  37. {
  38. // 每添加一个游戏行,用成员变量 记住,当到50行时,就添加结束行
  39. tempTotalLine++;
  40. Square *b;
  41. // 正常行中 黑色只有一个,且随机出现
  42. int blackIndex = rand()%4;
  43. // 创建4个 小方块
  44. for (int i=0; i<4; i++)
  45. {
  46. Color3B color = blackIndex==i?Color3B::BLACK:Color3B::WHITE;
  47. // 宽度和高度 均为屏幕的四分之一,减去1是为了 有1个缝隙
  48. b = Square::createWithArgs(color,Size(kSquareWidth - 1,kSquareHeight - 1),Color4B::BLACK);
  49. // 添加到游戏层,方便管理
  50. _gameLayer->addChild(b);
  51. // 重要~~~最下面是起始行,正常行的 行号 从 1 开始 2、3等等
  52. b->setPosition(i * kSquareWidth,lineIndex * kSquareHeight);
  53. // 绑定 其所在的 行号
  54. // 每一个方块 自己记住自己是在 哪一个正常的行里,因为行号 决定 了Square的Y值
  55. b->setLineIndex(lineIndex);
  56. }
  57. }
  58. // 添加 结束行 铺满全屏幕的 绿色的方块,还要显示文字
  59. void WhiteSquareScene::addEndLine()
  60. {
  61. auto b = EndLine::createWithContext(this);
  62. // 绑定 其所在的 行号;开始行 是0,正常行 是1、2、3;结束行是 4
  63. b->setLineIndex(4);
  64. b->setPositionY(b->getLineIndex() * kSquareHeight);
  65. // 添加到游戏层,方便管理
  66. _gameLayer->addChild(b);
  67. currentEndLine = b;
  68. }
  69.  
  70. #pragma mark - 初始化
  71. // 初始化
  72. bool WhiteSquareScene::init()
  73. {
  74. // 父类的初始化
  75. if ( !Layer::init() ) return false;
  76. // 重要~~~~用当前 的时间戳 作为 随机数的种子
  77. srand(time(NULL));
  78. // 屏幕大小
  79. winSize = Director::getInstance()->getVisibleSize();
  80. // 用一个Node 包装起来 所有的东东,方便进行整体控制
  81. _gameLayer = Node::create();
  82. addChild(_gameLayer);
  83. // 添加一个计时器标签
  84. timerLabel = Label::create();
  85. timerLabel->setTextColor(Color4B::BLUE);
  86. timerLabel->setSystemFontSize(48);
  87. timerLabel->setPosition(winSize.width/2,winSize.height-100);
  88. addChild(timerLabel);
  89. // 游戏初始化
  90. initGame();
  91. // 添加 监听当前 Layer的侦听器,就开始游戏
  92. addLayerTouchHandler();
  93. return true;
  94. }
  95. #pragma mark - Layer触摸监听
  96. // 添加 监听当前 Layer的侦听器,就开始游戏
  97. void WhiteSquareScene::addLayerTouchHandler()
  98. {
  99. // 单点触摸
  100. auto listener = EventListenerTouchOneByOne::create();
  101. listener->onTouchBegan = [this](Touch* t,Event* e)
  102. {
  103. auto squareArr = Square::getSquareArr();
  104. Square *s;
  105. // 遍历 所有的 方块 对象 数组,取出每一个方块
  106. for (auto it = squareArr->begin(); it!=squareArr->end(); it++)
  107. {
  108. // 解引用 取出每一个方块
  109. s = *it;
  110. // 如果 方块的行号是1 表示是正常行中的第一行;
  111. // 并且 正常行的第1行中的某个方块 被触摸了
  112. // 并且 被点击的还是黑块;开始计时
  113. if (s->getLineIndex()==1&&
  114. s->getBoundingBox().containsPoint(t->getLocation()))
  115. {
  116. // 黑色
  117. if (s->getColor()==Color3B::BLACK) {
  118. // 第1次点击黑块,且计时器没有开始,那么才要开始计时
  119. if (!isTimerRunning) {
  120. this->startTimer();
  121. }
  122. // 黑色被点击之后 变成灰色
  123. s->setColor(Color3B::GRAY);
  124. // 执行整体下移 动画
  125. this->gameLayerMoveDown();
  126. }else if(s->getColor()==Color3B::GREEN){
  127. // 点击了绿色,即 endLine
  128. // 整体下移 并且停止计时
  129. this->gameLayerMoveDown();
  130. this->stopTimer();
  131. }else{
  132. // 变成红色 警示
  133. s->setColor(Color3B::RED);
  134.  
  135. // 其他情况 不小心 点击到了 白块;弹出消息框
  136. MessageBox("你点错了","点错了");
  137. this->initGame();
  138. }
  139. // 重要~~~跳转循环,不用再遍历 SquareArr了
  140. break;
  141. }
  142. }
  143. return false;
  144. };
  145. // 向事件分发器注册侦听器,侦听整个Layer
  146. Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraPHPriority(listener,this);
  147. }
  148. #pragma mark - 游戏控制
  149. // 游戏控制 开始游戏
  150. void WhiteSquareScene::initGame()
  151. {
  152. //init
  153. stopTimer();
  154. // 开始时 清空 用于记录 当前游戏行数的 变量
  155. tempTotalLine = 0;
  156. // 是否 正在 显示 结束行
  157. // 只有 没显示时 才要显示 (只显示一次)
  158. isEndLineShowing = false;
  159. isTimerRunning = false;
  160. currentEndLine = NULL;
  161. timerLabel->setString("0.000000");
  162. // 每次重新开始游戏,清空方块对象数组
  163. Square::removeAllSquares();
  164. // 添加 开始行
  165. addStartLine();
  166. // 添加 正常的游戏行
  167. addNormalLine(1);
  168. addNormalLine(2);
  169. addNormalLine(3);
  170. }
  171. // 游戏层 整体下移
  172. void WhiteSquareScene::gameLayerMoveDown()
  173. {
  174. // 临时累积 当前 总共添加了多少行 (当到了50行时,添加结束行)
  175. if (tempTotalLine<50) {
  176. // 添加 正常行的第4行;由于 起始行号是0 ;正常行号是从1开始;因此 4表示在屏幕最上方 外边
  177. addNormalLine(4);
  178. }else if(!isEndLineShowing){
  179. // 是否 正在 显示 结束行
  180. // 只有 没显示时 才要显示 (只显示一次)
  181. addEndLine();
  182. isEndLineShowing = true;
  183. }
  184. // 对所有的方块 进行遍历,让所有的方块 执行 下移操作
  185. auto squareArr = Square::getSquareArr();
  186. // 使用迭代器 对 对象数组进行 遍历
  187. for (auto it = squareArr->begin(); it!=squareArr->end(); it++) {
  188. // 解引用 获得每一个方块;让方块自己执行 moveDown操作
  189. (*it)->moveDown();
  190. }
  191. if (currentEndLine!=NULL) {
  192. // 最后一行的行号是1时,也要下移
  193. if (currentEndLine->getLineIndex()==1) {
  194. // Game end
  195. // 整体下移
  196. gameLayerMoveDown();
  197. // 停止计时器
  198. stopTimer();
  199. }
  200. }
  201. }
  202.  
  203. #pragma mark - 时钟方法
  204. // 计时控制
  205. void WhiteSquareScene::update(float dt)
  206. {
  207. // 不断地获取时间,计算用时
  208. long timeInterval = clock()-startTime;
  209. // 设置计时器标签 微秒转成秒 6次方
  210. std::string str = StringUtils::format("%g",((double)timeInterval)/1000000);
  211. timerLabel->setString(str);
  212. }
  213. // 开始计时
  214. void WhiteSquareScene::startTimer()
  215. {
  216. if (!isTimerRunning) {
  217. scheduleUpdate();
  218. // 游戏开始时的时间戳
  219. startTime = clock();
  220. isTimerRunning = true;
  221. }
  222. }
  223. // 停止计时
  224. void WhiteSquareScene::stopTimer()
  225. {
  226. if(isTimerRunning){
  227. unscheduleUpdate();
  228. isTimerRunning = false;
  229. }
  230. }
  231.  
  232.  


方块
  1. //
  2. // Square.h
  3. // 01_cocos2d-x
  4. //
  5. // Created by beyond on 14-10-7.
  6. //
  7. // 方块
  8.  
  9. #ifndef ___1_cocos2d_x__Square__
  10. #define ___1_cocos2d_x__Square__
  11.  
  12. #include <cocos2d.h>
  13.  
  14. USING_NS_CC;
  15.  
  16. class Square:public Sprite {
  17. private:
  18. // 静态数组,每创建一个Square对象,就加到 数组中,static Vector<Square *> *squareArr;
  19. int lineIndex;
  20. public:
  21. // 创建 参数:方块的颜色、尺寸、显示文字、字体大小、字体颜色
  22. static Square* createWithArgs(Color3B color,Size size,std::string label,float fontSize,Color4B textColor);
  23. // 初始化 参数:方块的颜色、尺寸、显示文字、字体大小、字体颜色
  24. virtual bool initWithArgs(Color3B color,Color4B textColor);
  25. // 获取对象数组
  26. static Vector<Square *> *getSquareArr();
  27. // 遍历对象数组,从数组中 最后一个对象 开始,从父容器中(Layer)移除;并且从数组中移除
  28. static void removeAllSquares();
  29. // 移除 (父容器 及 数组中)
  30. void removeSquare();
  31. // 每一个方块 自己记住自己是在 哪一个正常的行里,因为行号 决定 了Square的Y值
  32. int getLineIndex();
  33. // 每一个方块 自己记住自己是在 哪一个正常的行里,因为行号 决定 了Square的Y值
  34. void setLineIndex(int lineIndex);
  35. // 让每一个方块 执行 向移一行的操作
  36. void moveDown();
  37. };
  38.  
  39. #endif /* defined(___1_cocos2d_x__Square__) */


  1. //
  2. // Square.cpp
  3. // 01_cocos2d-x
  4. //
  5. // Created by beyond on 14-10-7.
  6. //
  7. //
  8.  
  9. #include "Square.h"
  10. #define kWinSize Director::getInstance()->getVisibleSize()
  11. #define kSquareWidth kWinSize.width/4
  12. #define kSquareHeight kWinSize.height/4
  13.  
  14.  
  15. // 静态数组,Vector<Square*> * Square::squareArr = new Vector<Square*>();
  16.  
  17. #pragma mark - 生命周期方法
  18. // 参数:方块的颜色、尺寸、显示文字、字体大小、字体颜色
  19. Square* Square::createWithArgs(Color3B color,Color4B textColor)
  20. {
  21. auto b = new Square();
  22. // 调用init方法 初始化
  23. b->initWithArgs(color,size,label,fontSize,textColor);
  24. b->autorelease();
  25. // 每创建一个Square对象,就加到 对象数组中
  26. squareArr->pushBack(b);
  27. return b;
  28. }
  29. // 参数:方块的颜色、尺寸、显示文字、字体大小、字体颜色
  30. bool Square::initWithArgs(Color3B color,Color4B textColor)
  31. {
  32. // 父类的init
  33. Sprite::init();
  34. //
  35. lineIndex = 0;
  36. // 尺寸
  37. setContentSize(size);
  38. // 锚点左下角
  39. setAnchorPoint(Point::ZERO);
  40. // 颜色区域
  41. setTextureRect(Rect(0,size.width,size.height));
  42. setColor(color);
  43. // 方块内部 居中显示文字
  44. auto l = Label::create();
  45. l->setString(label);
  46. l->setSystemFontSize(fontSize);
  47. l->setTextColor(textColor);
  48. addChild(l);
  49. l->setPosition(size.width/2,size.height/2);
  50. return true;
  51. }
  52. #pragma mark - 供外界调用
  53. // 对象数组的 getter方法
  54. Vector<Square*> * Square::getSquareArr()
  55. {
  56. return Square::squareArr;
  57. }
  58. // 遍历对象数组,从父容器中(Layer)移除;并且从数组中移除
  59. void Square::removeAllSquares()
  60. {
  61. while (getSquareArr()->size()) {
  62. // 遍历对象数组,从父容器中(Layer)移除;并且从数组中移除
  63. getSquareArr()->back()->removeFromParent();
  64. getSquareArr()->popBack();
  65. }
  66. }
  67. // 移除 (父容器 及 数组中)
  68. void Square::removeSquare()
  69. {
  70. // auto c = getColor();
  71. // log("Remove Square,color is (%d,%d,%d)",c.r,c.g,c.b);
  72. // 从父容器中 即 Layer 中移除
  73. removeFromParent();
  74. // 从对象数组 中移除
  75. squareArr->eraSEObject(this);
  76. }
  77.  
  78. // 每一个方块 自己记住自己是在 哪一个正常的行里,因为行号 决定 了Square的Y值
  79. void Square::setLineIndex(int i)
  80. {
  81. this->lineIndex = i;
  82. }
  83. // 每一个方块 自己记住自己是在 哪一个正常的行里,因为行号 决定 了Square的Y值
  84. int Square::getLineIndex()
  85. {
  86. return this->lineIndex;
  87. }
  88. // 让每一个方块 执行 向移一行的操作
  89. void Square::moveDown(){
  90. // 既然方块 自己下移一行,那么 行号就要 减减
  91. this->lineIndex--;
  92.  
  93. if (getNumberOfRunningActions()!=0) {
  94. stopAllActions();
  95. }
  96. // 下移动作 移动到指定坐标 (也可以用MoveBy)
  97. MoveTo *to = MoveTo::create(0.1f,Point(getPositionX(),lineIndex * kSquareHeight));
  98. // 回调动作
  99. CallFunc *func = CallFunc::create([this](){
  100. // 如果 出屏幕了,直接 调用 移除方块 (内部会从 父容器和数组中都移除)
  101. if (lineIndex<0) {
  102. this->removeSquare();
  103. }
  104. });
  105. // 序列动作
  106. Sequence *s = Sequence::create(to,func,NULL);
  107. // 执行动作
  108. runAction(s);
  109. }


封装的结束行EndLine
  1. //
  2. // EndLine.h
  3. // 01_cocos2d-x
  4. //
  5. // Created by beyond on 14-10-7.
  6. //
  7. //
  8.  
  9. #ifndef ___1_cocos2d_x__EndLine__
  10. #define ___1_cocos2d_x__EndLine__
  11.  
  12. #include "Square.h"
  13. #include "WhiteSquareScene.h"
  14. // 声明 用到了游戏的主场景
  15. class WhiteSquareScene;
  16. // 结束行 继承自 Square 方块
  17. class EndLine:public Square
  18. {
  19. private:
  20. Size _winSize;
  21. WhiteSquareScene *_context;
  22. public:
  23. // 静态方法 创建时 需要 使用到主场景 WhiteSquareScene;内部调用init方法
  24. static EndLine* createWithContext(WhiteSquareScene *context);
  25. // 在init方法中,实现真正的 初始化
  26. bool initWithContext(WhiteSquareScene *context);
  27. };
  28.  
  29.  
  30. #endif /* defined(___1_cocos2d_x__EndLine__) */


  1. //
  2. // EndLine.cpp
  3. // 01_cocos2d-x
  4. //
  5. // Created by beyond on 14-10-7.
  6. //
  7. //
  8.  
  9. #include "EndLine.h"
  10.  
  11. // 静态方法 创建时 需要 使用到主场景 WhiteSquareScene;内部调用init方法
  12. EndLine* EndLine::createWithContext(WhiteSquareScene *context)
  13. {
  14. auto el = new EndLine();
  15. // 在init方法中,实现真正的 初始化
  16. el->initWithContext(context);
  17. el->autorelease();
  18. // 创建好 方块对象 就要加入到静态对象数组中
  19. Square::getSquareArr()->pushBack(el);
  20. return el;
  21. }
  22.  
  23. // 在init方法中,实现真正的 初始化
  24. bool EndLine::initWithContext(WhiteSquareScene *context)
  25. {
  26. this->_context = context;
  27. _winSize = Director::getInstance()->getVisibleSize();
  28. // 全屏 绿色
  29. Square::initWithArgs(Color3B::GREEN,_winSize,"游戏结束",50,Color4B::BLACK);
  30. auto label = Label::create();
  31. label->setString("再玩一次");
  32. label->setSystemFontSize(50);
  33. label->setPosition(_winSize.width/2,label->getContentSize().height/2+50);
  34. // 文字红色
  35. label->setTextColor(Color4B::RED);
  36. addChild(label);
  37. // 点击 label 【再玩一次】,重新开始一盘游戏
  38. auto listener = EventListenerTouchOneByOne::create();
  39. listener->onTouchBegan = [this](Touch* t,Event * e){
  40. // 点击了Label
  41. if (e->getCurrentTarget()->getBoundingBox().containsPoint(t->getLocation()-e->getCurrentTarget()->getParent()->getPosition())) {
  42. // 开始游戏
  43. this->_context->initGame();
  44. }
  45. return false;
  46. };
  47. // 注册监听器
  48. Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraPHPriority(listener,label);
  49. return true;
  50. }

猜你在找的Cocos2d-x相关文章