Cocos2d-x personal notes "2048" (3), cocos2d-x "2048"

Source: Internet
Author: User

Cocos2d-x personal notes "2048" (3), cocos2d-x "2048"


Start to write the main 2048Scene. cpp.

2048Scene. h Content

#pragma once#include "cocos2d.h"#include <queue>using namespace std;using namespace cocos2d;class My2048Scene:public CCLayer{public:My2048Scene();~My2048Scene();static CCScene * scene();void menuCall_restart(CCObject *pSender);void menuCall_return(CCObject *pSender);virtual void build();virtual bool init(); virtual void My2048Move();virtual bool My2048Cheack();virtual bool My2048union(int m);virtual void Mydisplay(int x1,int y1,int x2,int y2);void Mypointadd();void My2048over();void My2048win();long long My2048Point;int My2048[5][5];CCLabelTTF *Mypoint;CCSize Mysize;CCSize size;CCSprite *Overboard;void onEnter();void onExit();virtual bool ccTouchBegan(CCTouch *pTouch,CCEvent *pEvent);virtual void ccTouchMoved(CCTouch *pTouch,CCEvent *pEvent);virtual void ccTouchEnded(CCTouch *pTouch,CCEvent *pEvent);CREATE_FUNC(My2048Scene);private:CCPoint startP,thendP;float move_dis;void get_ture_callback();int Myreverse;bool MyWin;queue<int>Myque;};


All codes of 2048Sense. cpp

<Pre name = "code" class = "cpp"> # include "2048Scene. h "# include" MyScene. h "# include <cstring> # include <ctime> # include <queue> using namespace std; # define FOR (I, a, B) for (int I =; I <B; I ++) const int xx [] = {,-}; const int yy [] = }; CCSprite * g [16]; const int PP [] = {64,128,256,512,102,}; My2048Scene: My2048Scene () {} My2048Scene ::~ My2048Scene () {} CCScene * My2048Scene: scene () {// 'Scene 'is an autorelease object CCScene * scene = CCScene: create (); // 'lay' is an autorelease object My2048Scene * layer = My2048Scene: create (); // add layer as a child to scene-> addChild (layer ); // return the scene return scene;} bool My2048Scene: init () {size. width = 480, size. height = 854; move_dis = 100; CCSprite * background_2048 = CCSprite: crea Te ("homepage.png"); background_2048-> setPosition (ccp (size. width/2, size. height * 0.5); this-> addChild (background_2048); CCSprite * coord_2048 = CCSprite: create ("2048-coord.png"); coord_2048-> setPosition (ccp (size. width/2, size. height * 0.5); this-> addChild (coord_2048); // CCLOG ("% f \ n", size. width, size. height); Mysize = coord_2048-> getPosition (); Mysize. width-= 150, Mysize. height-= 10; // CCLOG ("% f \ n", Mysiz E. width, Mysize. height);/* int I = 0, j = 1; CCSprite * sp1 = CCSprite: create ("5.png"); sp1-> setPosition (ccp (Mysize. width + 100 * j, Mysize. height-100 * I); this-> addChild (sp1); */CCLabelTTF * My_restart_tmp = CCLabelTTF: create ("restart", "fonts/FZNHT. TTF ", 40); CCMenuItemLabel * My_restart = CCMenuItemLabel: create (parameters, this, menu_selector (My2048Scene: menuCall_restart); My_restart-> setPosition (ccp (siz) E. width/3*2, size. height * 0.6); CCLabelTTF * My_return_tmp = CCLabelTTF: create ("return", "fonts/FZNHT. TTF ", 40); CCMenuItemLabel * My_return = CCMenuItemLabel: create (My_return_tmp, this, menu_selector (My2048Scene: menuCall_return); My_return-> setPosition (ccp (size. width/3, size. height * 0.6); CCMenu * menu = CCMenu: create (My_restart, My_return, NULL); menu-> setPosition (CCPointZero); this-> addChild (menu); C CLabelTTF * Mypoint_borad = CCLabelTTF: create ("Point:", "fonts/FZNHT. TTF ", 40); Mypoint_borad-> setColor (ccc3 (255,255, 0); Mypoint_borad-> setPosition (ccp (size. width/5, size. height * 0.675); this-> addChild (Mypoint_borad); Mypoint = CCLabelTTF: create ("0", "fonts/FZNHT. TTF ", 40); Mypoint-> setColor (ccc3 (0,255,255); // Mypoint-> setAnchorPoint (ccp (size. width/5*4, size. height * 0.675); Mypoint-> setPosition (ccp (size. Width/5*4, size. height * 0.675); this-> addChild (Mypoint); FOR (m, 0, 16) {int I = m/4, j = m % 4; my2048 [I] [j] = 0; CCString * str = CCString: createWithFormat ("d.png", My2048 [I] [j]); g [I * 4 + j] = CCSprite: create (str-> getCString (); g [I * 4 + j]-> setPosition (ccp (Mysize. width + 100 * j, Mysize. height-100 * I); this-> addChild (g [I * 4 + j]); // initialize to overwrite all transparent images} My2048Point = 0; MyWin = false; time_t t; time (& t); t % = 1000; t * = t; srand (t); bui Ld (); build (); // setTouchEnabled (false); setTouchEnabled (true); // set the touch Myreverse = 0; // My2048win (); Overboard = NULL; return true;} void My2048Scene: build () {FOR (k, 0, 16) {int I = k/4, j = k % 4; if (My2048 [I] [j] = 0) Myque. push (k);} int m = CCRANDOM_0_1 () * (Myque. size ()-1) + 1; // CCLOG ("% d", m); int tmp; while (m --&&! Myque. empty () {tmp = Myque. front (); Myque. pop () ;}while (! Myque. empty () Myque. pop (); m = tmp; int I = m/4, j = m % 4; int or_2_4 = CCRANDOM_0_1 () * 10; if (or_2_4> = 8) my2048 [I] [j] = 2; else My2048 [I] [j] = 1; // randomly generate 2 or 4g [I * 4 + j]-> removeFromParentAndCleanup (true); // release the sprite image // CCLOG ("% d", I, j); CCString * str = CCString: createWithFormat ("mongod.png", My2048 [I] [j]); g [I * 4 + j] = CCSprite :: create (str-> getCString (); g [I * 4 + j]-> setPosition (ccp (Mysize. width + 100 * j, Mysize. height-100 * I); this-> AddChild (g [I * 4 + j]); // CCLOG ("% d = % d \ n", I, j, my2048 [I] [j]);} void My2048Scene: Mydisplay (int x1, int y1, int x2, int y2) {// reload the image g [x1*4 + y1]-> removeFromParentAndCleanup (true); g [x2*4 + y2]-> removeFromParentAndCleanup (true ); CCString * str1 = CCString: createWithFormat ("mongod.png", My2048 [x1] [y1]); CCString * str2 = CCString: createWithFormat ("mongod.png ", my2048 [x2] [y2]); g [x1*4 + y1] = CCSprite: create (str1-> getCString () ); G [x2*4 + y2] = CCSprite: create (str2-> getCString (); g [x1*4 + y1]-> setPosition (ccp (Mysize. width + 100 * y1, Mysize. height-100 * x1); g [x2*4 + y2]-> setPosition (ccp (Mysize. width + 100 * y2, Mysize. height-100 * x2); this-> addChild (g [x1*4 + y1]); this-> addChild (g [x2*4 + y2]);} bool My2048Scene: My2048union (int m) {// merge bool flag = false; bool pointadd = false; if (m = 1) // l {FOR (I) {FOR (j, 0, 4) {if (My2048 [I] [j] = 0) continue; For (int k = j + 1; k <4; k ++) {if (My2048 [I] [k] = 0) continue; else if (My2048 [I] [k] = My2048 [I] [j]) {My2048 [I] [j] ++; my2048 [I] [k] = 0; My2048Point + = PP [My2048 [I] [j]; Mydisplay (I, j, I, k); flag = true; pointadd = true; break;} else break;} FOR (j,) {if (My2048 [I] [j]! = 0) continue; for (int k = j + 1; k <4; k ++) {if (My2048 [I] [k] = 0) continue; int tmp = My2048 [I] [j]; My2048 [I] [j] = My2048 [I] [k]; My2048 [I] [k] = tmp; mydisplay (I, j, I, k); flag = true; break ;}}} else if (m = 2) // r {FOR (I, 0, 4) {for (int j = 3; j> = 0; j --) {if (My2048 [I] [j] = 0) continue; for (int k = J-1; k> = 0; k --) {if (My2048 [I] [k] = 0) continue; else if (My2048 [I] [k] = My2048 [I] [j]) {My2048 [I] [j] ++; my2048 [I] [k] = 0; My204 8 Point + = PP [My2048 [I] [j]; Mydisplay (I, j, I, k); flag = true; pointadd = true; break ;} else break ;}}for (int j = 3; j> = 0; j --) {if (My2048 [I] [j]! = 0) continue; for (int k = J-1; k> = 0; k --) {if (My2048 [I] [k] = 0) continue; int tmp = My2048 [I] [j]; My2048 [I] [j] = My2048 [I] [k]; My2048 [I] [k] = tmp; mydisplay (I, j, I, k); flag = true; break ;}}} else if (m = 3) // d {FOR (I, 0, 4) {for (int j = 3; j> = 0; j --) {if (My2048 [j] [I] = 0) continue; for (int k = J-1; k> = 0; k --) {if (My2048 [k] [I] = 0) continue; else if (My2048 [k] [I] = My2048 [j] [I]) {My2048 [j] [I] ++; my2048 [k] [I] = 0; My20 48 Point + = PP [My2048 [j] [I]; Mydisplay (j, I, k, I); pointadd = true; flag = true; break ;} else break ;}}for (int j = 3; j> = 0; j --) {if (My2048 [j] [I]! = 0) continue; for (int k = J-1; k> = 0; k --) {if (My2048 [k] [I] = 0) continue; int tmp = My2048 [j] [I]; My2048 [j] [I] = My2048 [k] [I]; My2048 [k] [I] = tmp; mydisplay (j, I, k, I); flag = true; break ;}}} else if (m = 4) // u {FOR (I, 0, 4) {FOR (j, 0, 4) {if (My2048 [j] [I] = 0) continue; for (int k = j + 1; k <4; k ++) {if (My2048 [k] [I] = 0) continue; else if (My2048 [k] [I] = My2048 [j] [I]) {My2048 [j] [I] ++; My2048 [k] [I] = 0; My2048Point + = PP [My 2048 [j] [I]; Mydisplay (j, I, k, I); pointadd = true; flag = true; break;} else break ;}} FOR (j, 0, 4) {if (My2048 [j] [I]! = 0) continue; for (int k = j + 1; k <4; k ++) {if (My2048 [k] [I] = 0) continue; int tmp = My2048 [j] [I]; My2048 [j] [I] = My2048 [k] [I]; My2048 [k] [I] = tmp; mydisplay (j, I, k, I); flag = true; break ;}}}if (pointadd) {Mypointadd () ;}return flag ;}void My2048Scene :: mypointadd () {// Add Mypoint-> setString (CCString: createWithFormat ("% lld", My2048Point)-> getCString ();} void My2048Scene: My2048Move () {// start to take the initiative for bool flag = false; If (Myreverse = 0) return; else flag = My2048union (Myreverse); if (flag) {build ();} else return; bool gameflag = false; gameflag = My2048Cheack (); if (MyWin) {// system ("pause"); // CCLOG ("Win \ n"); My2048win ();} else if (! Gameflag) {// system ("pause"); // CCLOG ("over \ n"); My2048over () ;}} void My2048Scene: My2048win () {// game victory screen Overboard = CCSprite: create ("overboard.png"); Overboard-> setPosition (ccp (Mysize. width + 150, Mysize. height-150); this-> addChild (Overboard, 100); CCLabelTTF * Win_flag = CCLabelTTF: create ("you win", "fonts/FZNHT. TTF ", 40); Win_flag-> setColor (ccc3 (255, 0); CCSize tmp = Overboard-> getPosition (); Win_flag-> SetPosition (ccp (tmp. width-125, tmp. height-125); Overboard-> addChild (Win_flag); CCLabelTTF * board_point = CCLabelTTF: create ("0", "fonts/FZNHT. TTF ", 30); board_point-> setString (CCString: createWithFormat (" % lld ", My2048Point)-> getCString (); board_point-> setColor (ccc3 (0, 0, 0); board_point-> setPosition (ccp (tmp. width/2, tmp. height/6); Overboard-> addChild (board_point); this-> setTouchEnabled (false);} void My2 048 Scene: My2048over () {// game end screen Overboard = CCSprite: create ("overboard.png"); Overboard-> setPosition (ccp (Mysize. width + 150, Mysize. height-150); this-> addChild (Overboard, 100); CCLabelTTF * Lost_flag = CCLabelTTF: create ("you lost", "fonts/FZNHT. TTF ", 40); Lost_flag-> setColor (ccc3 (0,255,255); CCSize tmp = Overboard-> getPosition (); Lost_flag-> setPosition (ccp (tmp. width-125, tmp. height-125); Overboard-> addChil D (Lost_flag); CCLabelTTF * board_point = CCLabelTTF: create ("0", "fonts/FZNHT. TTF ", 30); board_point-> setString (CCString: createWithFormat (" % lld ", My2048Point)-> getCString (); board_point-> setColor (ccc3 (0, 0, 0); board_point-> setPosition (ccp (tmp. width/2, tmp. height/6); Overboard-> addChild (board_point); this-> setTouchEnabled (false);} bool My2048Scene: My2048Cheack () {// check victory or failure bool flag = false; FOR (I,) F OR (j,) {if (My2048 [I] [j] = 11) MyWin = true;} FOR (I,) FOR (j) {FOR (k, 0, 4) {int x = I + xx [k]; int y = j + yy [k]; if (x <0 | y <0 | x> = 4 | y> = 4) continue; if (! My2048 [I] [j] | My2048 [I] [j] = My2048 [x] [y]) return true ;}} return false;} void My2048Scene :: get_ture_callback () {// judge the touch direction Myreverse = 0; CCPoint ans = ccpSub (startP, thendP); if (ans. x * ans. x + ans. y * ans. y <move_dis * move_dis) return; if (fabs (ans. x)> fabs (ans. y) {if (ans. x> move_dis) {// CCLOG ("l"); Myreverse = 1;} else if (ans. x <-move_dis) {// CCLOG ("r"); Myreverse = 2;} elsereturn;} else {if (ans. y> move_dis ){ // CCLOG ("d"); Myreverse = 3;} else if (ans. y <-move_dis) {// CCLOG ("u"); Myreverse = 4;} else {return ;}} if (Myreverse = 0) return; MoveFlag = 1; my2048Move ();} void My2048Scene: onEnter () {CCLayer: onEnter (); CCDirector: shareddire()-> getTouchDispatcher ()-> addTargetedDelegate (this, 0, false);} void My2048Scene: onExit () {CCLayer: onExit (); CCDirector: shareddire()-> getTouchDispatcher ()-> removeDelegate (th Is);} bool My2048Scene: ccTouchBegan (CCTouch * pTouch, CCEvent * pEvent) {// start to touch MoveFlag = 0; startP = pTouch-> getLocation (); return true ;} void My2048Scene: ccTouchMoved (CCTouch * pTouch, CCEvent * pEvent) {// if (MoveFlag) return; thendP = pTouch-> getLocation (); Myreverse = 0; get_ture_callback ();} void My2048Scene: ccTouchEnded (CCTouch * pTouch, CCEvent * pEvent) {// if (MoveFlag) return; thendP = pTouch-> getLocation (); Myreverse = 0; get_ture_callback ();} void My2048Scene: menuCall_restart (CCObject * pSender) {// restart menu if (Overboard! = NULL) Overboard-> outputs (true); CCScene * temps = My2048Scene: scene (); CCDirector: shareddire()-> replaceScene (temps);} void My2048Scene :: menuCall_return (CCObject * pSender) {// return menu CCScene * temps = MyScene: scene (); CCDirector: sharedDirector ()-> replaceScene (temps );}

 


I encountered many problems


For example, if a random number starts to generate coordinates randomly, but there is always a loop, that is, the last blank lattice is not random.

I used the queue, joined all the empty grids, and then randomly selected several teams.


There is also the touch direction. I detected the distance between the start point and the end point x and y. Then determine the upper, lower, and left.


The most troublesome thing is to move up and down, just a little different from the coordinate system.

I am using a matrix array, with the upper left corner being 0, 0. When the coordinates are calculated, the lower left corner is the initial state. Then add and subtract.


After the game is over, the screen sliding can continue, and I am sad for a long time, and finally close with a touch.


Resource file:

Http://pan.baidu.com/s/1dDD0o1r


An error is reported when the cocos2d-x31 adds a return key listener. Why can't I catch it?

You
/System/media/audio/ui/Effect_Tick.ogg

The sound file does not seem to be found.
 
Cocos2d-x-32 failed to generate solution in VS2010?

Yo, this blog is exactly on my blog .. CSDN windvix column.
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.