Cocos2d-x personal notes 2048 (3)

Source: Internet
Author: User
Tags addchild


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

# 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-> Se Tposition (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 my204 8 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-> addchild (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,) (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 ;}} else {If (ans. y> move_dis) {// cclog ("D" ); Myreverse = 3;} else {// cclog ("U"); myreverse = 4;} my2048move ();} void my2048scene: onenter () {cclayer :: onenter (); ccdirector: shareddirector ()-> gettouchdispatcher ()-> addtargeteddelegate (this, 0, false);} void my2048scene: onexit () {cclayer :: onexit (); ccdirector: shareddire()-> gettouchdispatcher ()-> removedelegate (this);} bool my2048scene: cctouchbegan (cctouch * ptouch, ccevent * pevent) {// start to touch St Artp = ptouch-> getlocation (); Return true;} void my2048scene: cctouchmoved (cctouch * ptouch, ccevent * pevent) {// touch point moving... // thendp = ptouch-> getlocation ();} void my2048scene: cctouchended (cctouch * ptouch, ccevent * pevent) {// touch end 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

Cocos2d-x personal notes 2048 (3)

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.