Cocos2d-x 个人笔记 《2048》(3)

  次阅读 作者:智能小宝 来源:互联网 2015-09-28 10:37 我要评论(0)

开始写主要的 2048Scene.cpp 了。

2048Scene.h 的内容

#pragma once

#include cocos2d.h

#include

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;

queueMyque;

};

2048Sense.cpp 的全部代码

# include 2048Scene.h # include MyScene.h # include# include# includeusing namespace std; #define FOR(i,a,b) for(int i=a;iaddChild(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 ,size.width,size.height); Mysize=coord_2048->getPosition(); Mysize.width-=150,Mysize.height-=10; //CCLOG(%f %f ,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 ,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 ); My2048win(); } else if(!gameflag) { //system(pause); //CCLOG(over ); 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.yfabs(ans.y)) { if (ans.x>move_dis) { //CCLOG(l); Myreverse=1; } else if(ans.x<-move_dis) { //CCLOG(r); Myreverse=2; } else return; } 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); } *move_dis)return;>;i++)>

本站文章信息来源于网络以及网友投稿,本站只负责对文章进行整理、排版、编辑,是出于传递更多信息之目的,并不意味着赞同其观点或证实其内容的真实性。如果您有什么意见或建议,请联系QQ28-1688-302!

人工智能实验室
相关文章相关文章
  • 韩春雨称已能重复实验结果 近期将有消息公布

    韩春雨称已能重复实验结果 近期将有消息公布

  • 无人驾驶汽车如何改变城市生活?听听他们怎么说

    无人驾驶汽车如何改变城市生活?听听他们怎么说

  • 英国研发“杀生”机器人 通过生命体获取能量

    英国研发“杀生”机器人 通过生命体获取能量

  • 未来两年人工智能要怎么走?看这篇就够了

    未来两年人工智能要怎么走?看这篇就够了

网友点评网友点评
阅读推荐阅读推荐

据国外媒体报道,在过去两年内,聊天机器人(chatbot)、人工智能以及机器学习的研发和采用取得了巨大进展。许多初创公司正利用人工智能和...

霍金 视觉中国 图 英国著名物理学家霍金(Stephen Hawking)再次就人工智能(AI)发声,他认为:对于人类来说,强大AI的出现可能是最美妙的...

文|郑娟娟 今年,人工智能(AI) 60岁了。在AI60岁的时候,笔者想要介绍一下AI100,一个刚刚2岁的研究项目,但它的预设寿命是100年,甚至更长...

AlphaGo与李世石的人机大战,为大众迅速普及了人工智能的概念。 但对谷歌而言,除了下围棋,现在的人工智能进展到哪一步了?未来,人工智能...