首页 > 代码库 > Cocos2d-x 个人笔记 《2048》(3)
Cocos2d-x 个人笔记 《2048》(3)
开始写主要的 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 的全部代码
# 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; } } 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::sharedDirector()->getTouchDispatcher()->removeDelegate(this); } bool My2048Scene::ccTouchBegan(CCTouch *pTouch,CCEvent *pEvent) { //开始触摸 startP=pTouch->getLocation(); return true; } void My2048Scene::ccTouchMoved(CCTouch *pTouch,CCEvent *pEvent) { //触摸点移动中 //thendP=pTouch->getLocation(); } void My2048Scene::ccTouchEnded(CCTouch *pTouch,CCEvent *pEvent) { //触摸结束 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-x 个人笔记 《2048》(3)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。