偶然发现了一个叫2048的游戏,感觉是一个魔方类游戏,所以自己做一个试试
file :2048.h
#include "cocos2d.h"
#include <stdio.h>
USING_NS_CC;
class gezi :public CCSprite//每个格子的类
{
public:
gezi();
~gezi();
void setpos(float x,float y);
void setvalue(int v);
class CCLabelTTF* pLabel;//显示的数值
int value;//精灵的值
private:
void eat(gezi *p);
//cocos2d::CCSprite *kuang;//格子边框,暂时用精灵类显示,以后改为贴图等更合适的方法
class cocos2d::CCPoint pos;//格子坐标
friend class gong;
};
gezi::gezi():value(0)//初始化值,图片,位置
{
//this-> cocos2d::CCSprite::create("CloseNormal.png");//创建边框
this->initWithFile("kuang1.png");
pLabel = CCLabelTTF::create("0", "Arial", 24);
pos.x=NULL;
pos.y=NULL;
return;
}
gezi::~gezi()
{}
void gezi::setpos(float x=0,float y=0)//只规定格子的位置,其中精灵的位置又它自己决定
{
this->setPosition(ccp(x, y));//设定格子边框的位置
pLabel->setPosition(ccp(x, y));//设定内部数值的位置
pos.x=x;
pos.y=y;//显示的指示当前位置
}
void gezi::setvalue(int v)//只规定格子的位置,其中精灵的位置又它自己决定
{
value=v;
const char* pchar=CCString::createWithFormat("%d",v)->getCString();
//pchar->autorelease();自动释放
pLabel->CCLabelTTF::setString(pchar);
const char* pchar1=CCString::createWithFormat("%d.png",v)->getCString();
this->initWithFile(pchar1);
}
/——————————————————————
class gong : public cocos2d::CCLayer//背景宫图的类,是一个层
{
public:
gong();//背景宫图();
~gong();//背景宫图();
void up();//移动格子
void down();
void left();
void right();
virtual bool init();
void suiji();//随机生成新节点
CREATE_FUNC(gong);
virtual bool ccTouchBegan(cocos2d::CCTouch * touch,cocos2d::CCEvent * event);
virtual void onEnter();
//virtual void onExit();
private:
class cocos2d::CCPoint origin;//原点坐标
class cocos2d::CCSize visibleSize;//屏幕大小
class gezi arry[4][4];//格子类的矩阵,以后修改为P214中描述的二维数组
friend class gezi;
};
void gong::onEnter()
{
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate( this,0,false);
CCLayer::onEnter();
}
void gong::suiji()//背景宫图()
{
int s=rand()%5;
for(int i=0;i<4;i++)
{
for(int j=0; j<4;j++)
{
if(arry[i][j].value == 0)
{
if(s==0) //选择这个点作为新的随机生成的点
{
arry[i][j].setvalue(2);
return;
}
else
s--;
}
}
}
}
bool gong::ccTouchBegan(cocos2d::CCTouch * touch,cocos2d::CCEvent * event)
{
up();
suiji();
return true;
}
gong::gong()//背景宫图()
{
}
gong::~gong()//背景宫图()
{
}
bool gong::init()
{
//
// 1. super init first
int a[16]={2,0,2,0,
2,2,0,2,
2,4,2,0,
0,2,4,2};
if ( !CCLayer::init() )
{
return false;
}
CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
int i=4,j=4,k=0;
for(int ii=0;ii<i;ii++)
for(int jj=0;jj<j;jj++)
{
arry[ii][jj].setvalue(a[k++]);
//初始化每个格子的位置
arry[ii][jj].setpos((visibleSize.height/j)*(ii) + origin.x+visibleSize.width/(i+1), visibleSize.height/(j+1)+(visibleSize.height/j)*(jj) + origin.y);
this->addChild(&arry[ii][jj], 1);
this->addChild(arry[ii][jj].pLabel, 2);
}
/
// 2. add a menu item with "X" image, which is clicked to quit the program
// you may modify it.
// add a "close" icon to exit the progress. it's an autorelease object
CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
this,
menu_selector(HelloWorld::menuCloseCallback));
pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,
origin.y + pCloseItem->getContentSize().height/2));
// create menu, it's an autorelease object
CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
pMenu->setPosition(CCPointZero);
this->addChild(pMenu, 1);
/
// 3. add your codes below...
// add a label shows "Hello World"
// create and initialize a label
//CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24);
//
position the label on the center of the screen
//pLabel->setPosition(ccp(origin.x + visibleSize.width/2,
// origin.y + visibleSize.height - pLabel->getContentSize().height));
add the label as a child to this layer
//this->addChild(pLabel, 1);
// add "HelloWorld" splash screen"
//CCSprite* pSprite = CCSprite::create("backkuang.png");
// position the sprite on the center of the screen
//pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
// add the sprite as a child to this layer
//this->addChild(pSprite, 0);
加入格子了
//gong G;
//this->addChild(&G, 0);
onEnter();
return true;
}
void up_lie( gezi (*p)[4],int n)
{
int k;
for(int i=0;i<4;)
{
if(p[i][n].value != 0)
{
for(k=i+1;k<4;)
{
if(p[k][n].value)
{
if(p[i][n].value == p[k][n].value)
{
p[i][n].setvalue(p[i][n].value*2);
p[k][n].setvalue(0);
}
i++;
break;
}
else
{
k++;
}
}
if( k >=4)
{
i++;
break;
}
}
else
i++;
}
for(int i=0;i<4;)
{
if(p[i][n].value == 0 )
{
for(k=i+1;k<4;)
{
if(p[k][n].value)
{
p[i][n].setvalue(p[k][n].value);
p[k][n].setvalue(0);
i++;
break;
}
else
k++;
}
if(k>=4)
{
i++;
break;
}
}
else
{
i++;
}
}
}
void gong::up()//整体上移
{
for(int i=0;i<4;i++)
up_lie(arry,i);
}
file :HelloWorldScene.cpp
#include "HelloWorldScene.h"
#include "_2048.h"
USING_NS_CC;
CCScene* HelloWorld::scene()
{
// 'scene' is an autorelease object
CCScene *scene = CCScene::create();
// 'layer' is an autorelease object
//HelloWorld *layer = HelloWorld::create();
gong *lay = gong::create();
// add layer as a child to scene
//scene->addChild(layer);
scene->addChild(lay);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//
// 1. super init first
if ( !CCLayer::init() )
{
return false;
}
CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
/
// 2. add a menu item with "X" image, which is clicked to quit the program
// you may modify it.
// add a "close" icon to exit the progress. it's an autorelease object
CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
this,
menu_selector(HelloWorld::menuCloseCallback));
pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,
origin.y + pCloseItem->getContentSize().height/2));
// create menu, it's an autorelease object
CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
pMenu->setPosition(CCPointZero);
this->addChild(pMenu, 1);
/
// 3. add your codes below...
// add a label shows "Hello World"
// create and initialize a label
CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24);
// position the label on the center of the screen
pLabel->setPosition(ccp(origin.x + visibleSize.width/2,
origin.y + visibleSize.height - pLabel->getContentSize().height));
// add the label as a child to this layer
this->addChild(pLabel, 1);
// add "HelloWorld" splash screen"
//CCSprite* pSprite = CCSprite::create("backkuang.png");
// position the sprite on the center of the screen
//pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
// add the sprite as a child to this layer
//this->addChild(pSprite, 0);
加入格子了
gong G;//创建一个2*2宫
//this->addChild(&G, 0);
return true;
}
//bool HelloWorld::ccTouchBegan(cocos2d::CCTouch * touch,cocos2d::CCEvent * event)
//{
// CCPoint touchPoint=touch->getLocation();
// CCPoint reallyPoint=this->getParent()->convertToNodeSpace(touchPoint);
// pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
// return 1;
//
//}
void HelloWorld::menuCloseCallback(CCObject* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
#else
CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
#endif
}