Cocos2d-x 個人筆記 《2048》(3),cocos2d-x《2048》

來源:互聯網
上載者:User

Cocos2d-x 個人筆記 《2048》(3),cocos2d-x《2048》


開始寫主要的 2048Scene.cpp 了。

2048Scene.h 的內容

#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;};


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=a;i<b;i++)const int xx[]= {0,0,-1,1};const int yy[]= {-1,1,0,0};CCSprite *g[16];const int PP[]= {0,2,4,8,16,32,64,128,256,512,1024,2048};My2048Scene::My2048Scene(){}My2048Scene::~My2048Scene(){}CCScene* My2048Scene::scene(){    // 'scene' is an autorelease object    CCScene *scene = CCScene::create();    // 'layer' is an autorelease object    My2048Scene *layer = My2048Scene::create();    // add layer as a child to scene    scene->addChild(layer);    // return the scene    return scene;}bool My2048Scene::init(){    size.width=480,size.height=854;    move_dis=100;    CCSprite *background_2048=CCSprite::create("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 %f\n",size.width,size.height);    Mysize=coord_2048->getPosition();    Mysize.width-=150,Mysize.height-=10;    //CCLOG("%f %f\n",Mysize.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(My_restart_tmp,this,menu_selector(My2048Scene::menuCall_restart));    My_restart->setPosition(ccp(size.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);    CCLabelTTF *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]);//初始化全部覆蓋透明圖片    }    My2048Point=0;    MyWin=false;    time_t t;    time(&t);    t%=1000;    t*=t;    srand(t);    build();    build();    //setTouchEnabled(false);    setTouchEnabled(true);//設定觸摸    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;//隨機產生2或者4    g[i*4+j]->removeFromParentAndCleanup(true);    //釋放精靈圖片    //CCLOG("%d %d",i,j);    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]);        //CCLOG("%d %d==%d\n",i,j,My2048[i][j]);}void My2048Scene::Mydisplay(int x1,int y1,int x2,int y2){//重載圖片    g[x1*4+y1]->removeFromParentAndCleanup(true);    g[x2*4+y2]->removeFromParentAndCleanup(true);    CCString *str1=CCString::createWithFormat("%d.png",My2048[x1][y1]);    CCString *str2=CCString::createWithFormat("%d.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){//合并    bool flag=false;    bool pointadd=false;    if(m==1)//l    {        FOR(i,0,4)        {            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,0,4)            {                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;                        My2048Point+=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;                        My2048Point+=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[My2048[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(){//加分    Mypoint->setString(CCString::createWithFormat("%lld",My2048Point)->getCString());}void My2048Scene::My2048Move(){// 開始主動作    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(){//遊戲勝利畫面    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,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 My2048Scene::My2048over(){//遊戲結束畫面    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(){//檢查勝利或者失敗    bool flag=false;    FOR(i,0,4)    FOR(j,0,4)    {        if(My2048[i][j]==11)MyWin=true;    }    FOR(i,0,4)    FOR(j,0,4)    {        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(){//判斷觸摸方向    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::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,0,false);}void My2048Scene::onExit(){    CCLayer::onExit();    CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);}bool My2048Scene::ccTouchBegan(CCTouch *pTouch,CCEvent *pEvent){//開始觸摸    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 菜單    if(Overboard!=NULL)        Overboard->removeFromParentAndCleanup(true);    CCScene *temps=My2048Scene::scene();    CCDirector::sharedDirector()->replaceScene(temps);}void My2048Scene::menuCall_return(CCObject *pSender){//return 菜單    CCScene *temps=MyScene::scene();    CCDirector::sharedDirector()->replaceScene(temps);}




中間遇到過很多問題


比如 隨機數,開始隨機產生 座標,卻會出現一直迴圈,就是隨機不到最後一個空的格子。

我就用了隊列,把所有空的格子入隊,然後隨機出幾個隊,選一個就好。


還有觸摸方向,我檢測的是起點和終點的 座標 x ,y 的距離。然後判斷上下左右。


最麻煩的是 移動的時候上下左右了,剛好和座標系的有點不一樣。

我是採用的矩陣數組,左上方是0,0 。而計算座標的時候 左下角才是初始。然後就要 一個加,一個減。


遊戲結束後,螢幕滑動還能繼續,又憂傷我半天,最後就 加上觸摸關閉。


資源檔:

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


cocos2d-x31添加返回鍵監聽報錯,不知道為何捕捉不到?

你這個
/system/media/audio/ui/Effect_Tick.ogg

音效檔案貌似沒找到吧。
 
cocos2d-x-32在VS2010產生解決方案失敗?

喲,我部落格上正好有此一條博文。。CSDN windvix的專欄。
 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.