Cocos2dx------详细介绍如何编写扫雷这个游戏含源码(二)

前端之家收集整理的这篇文章主要介绍了Cocos2dx------详细介绍如何编写扫雷这个游戏含源码(二)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

资源图片 http://yunpan.cn/cQ63WwezMEmWD (提取码:715f)



棋盘绘制好了,接下来就是触摸了

通过触摸上一层,使其显示下一层的情况


触摸代码

bool HelloWorld::ccTouchBegan(cocos2d::CCTouch *pTouch,cocos2d::CCEvent *pEvent)
{
CCPoint p=pTouch->getLocation();


if (spLayer->boundingBox().containsPoint(p)&&Cantouch==true)//判断触摸点是否在棋盘界面里,Cantouch true表示还能触摸,赢输的时候把该值设为false
{
CCPoint orgin=spLayer->getPosition()-ccp(spLayer->getContentSize().width/2,spLayer->getContentSize().height/2);//得到spLayer层的左下角点的坐标,每种难度该坐标都不一样
CCPoint temp=p-orgin;
hang=temp.y/32+1;//把触摸点的坐标,换算成 是第几行和第几列
lie=temp.x/32+1;


if (maze[0][hang][lie]==9||maze[0][hang][lie]==11||maze[0][hang][lie]==12)//只有上一层的值是 正常的 红旗 问号的时候才能进行触摸
{


if (brush==1)//brush 表示 画笔,1表示当前选择的是 正常的,2表示红旗,3问号
{
if (maze[0][hang][lie]==11)//每少一个红旗,地雷输就+1
{
temp_mine_Number++;
mine_Number_label->setString(CCString::createWithFormat("%d",temp_mine_Number)->m_sString.c_str());
}









maze[0][hang][lie]=maze[1][hang][lie];//使上一行的值等于下一行的值
sp[hang][lie]->initWithFile(CCString::createWithFormat("%d.png",maze[0][hang][lie])->m_sString.c_str());


if (maze[0][hang][lie]==10)//如果下一层是地雷,游戏结束
{
Cantouch=false;


tips->setString("Game Over");
}


if (maze[0][hang][lie]==0)//如果下一层是空的,就显示这个格子周围全部为空白的格子,用到遍历
{
CCPoint m_location;
for (int a=LocationList.size()-1;a>=0;a--)
{
m_location=LocationList[a];
std::vector<CCPoint>::iterator Locationiter=LocationList.begin()+a;
LocationList.erase(Locationiter);
}//清空列表


LocationList.push_back(ccp(hang,lie));
for (unsigned int k=0;k<(LocationList.size());k++)
{
m_location=LocationList[k];


int i,j,a,b;
i=m_location.x;
j=m_location.y;


a=i+1;
b=j;
PushList(a,b);//上


a=i;
b=j+1;
PushList(a,b);//右


a=i-1;
b=j;
PushList(a,b);//下


a=i;
b=j-1;
PushList(a,b);//左
}

for (unsigned int k=0;k<(LocationList.size());k++)//读取表里的数据,这些数据表示空白的格子
{
m_location=LocationList[k];
hang=m_location.x;
lie=m_location.y;
maze[0][hang][lie]=maze[1][hang][lie];
sp[hang][lie]->initWithFile(CCString::createWithFormat("%d.png",maze[0][hang][lie])->m_sString.c_str());
}
}




int num=0;
for (int i=1;i<=height;i++)
{
for (int j=1;j<=width;j++)
{
if (maze[0][i][j]==9||maze[0][i][j]==11||maze[0][i][j]==12)
{
num++;
}

}
}



if (num==mine_Number)//maze[0][i][j]层等于9 11 12的格子加起来数量等于地雷数的话,说明剩下来的格子都是地雷了,游戏胜利
{
Cantouch=false;
tips->setString("Win");
}



}







if (brush==2)
{
if (maze[0][hang][lie]!=11)
{
maze[0][hang][lie]=11;
temp_mine_Number--;//每插一个红旗,地雷数-1
mine_Number_label->setString(CCString::createWithFormat("%d",temp_mine_Number)->m_sString.c_str());


sp[hang][lie]->initWithFile(CCString::createWithFormat("11.png",maze[0][hang][lie])->m_sString.c_str());
}
}


if (brush==3)
{

if (maze[0][hang][lie]!=12)
{
if (maze[0][hang][lie]==11)
{
temp_mine_Number++;
mine_Number_label->setString(CCString::createWithFormat("%d",temp_mine_Number)->m_sString.c_str());
}


maze[0][hang][lie]=12;
sp[hang][lie]->initWithFile(CCString::createWithFormat("12.png",maze[0][hang][lie])->m_sString.c_str());
}


}
}


}

return true;
}







PushList函数代码

void HelloWorld::PushList(int a,int b)
{
if (a>=1&&a<=height&&b>=1&&b<=width)
{
if (maze[1][a][b]==0)
{
CCPoint temp=ccp(a,b);
for (unsigned int n=0;n<(LocationList.size());n++)
{
CCPoint m_location=LocationList[n];


if (temp.x==m_location.x&&temp.y==m_location.y)
{
break;
}
else
{
if (n==(LocationList.size()-1))
{
LocationList.push_back(temp);
}
}


}
}


}
}










HelloWorldScene.h文件的源码

#ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" USING_NS_CC; class HelloWorld : public cocos2d::CCLayer { public: // Here's a difference. Method 'init' in cocos2d-x returns bool,instead of returning 'id' in cocos2d-iphone virtual bool init(); // there's no 'id' in cpp,so we recommend returning the class instance pointer static cocos2d::CCScene* scene(); // implement the "static node()" method manually CREATE_FUNC(HelloWorld); CCSize winSize;//屏幕大小 int width;//棋盘宽度 int height;//棋盘高度 int mine_Number;//地雷数 int lv; //游戏难度 int brush;//画笔 void Menu_button(CCObject* pSender);//点初级 中级 高级 按钮时候的回调 void Menu_restart(CCObject* pSender);//重新开始 void Menu_brush(CCObject* pSender);//画笔设置的回调 void gameinit(int lv);//根据难度初始化棋盘 int maze[2][50][50];//3维数组 int lie,hang;//列 行 int getValue(int i,int j);//得到格子的值 CCSprite* sp[50][50];//显示在棋盘上的每个格子 CCLayer* spLayer;//棋盘层 virtual bool ccTouchBegan(cocos2d::CCTouch *pTouch,cocos2d::CCEvent *pEvent); virtual void ccTouchMoved(cocos2d::CCTouch *pTouch,cocos2d::CCEvent *pEvent); virtual void ccTouchEnded(cocos2d::CCTouch *pTouch,cocos2d::CCEvent *pEvent); void onEnterTransitionDidFinish();//注册触摸函数 std::vector<CCPoint> LocationList;//一个存放CCPoint类数据的表 点的横坐标表示行 纵坐标表示列 void PushList(int a,int b);//把 ccp(a,b)这个点压人表 CCLabelTTF*tips;//右上角的提示文本框 CCLabelTTF*mine_Number_label;//地雷数文本 CCSprite *check1;//左边的勾 CCSprite *check2;//右边的勾 CCMenuItemImage *button1item;//初级按钮 CCMenuItemImage *button2item;//中级按钮 CCMenuItemImage *button3item;//高级按钮 CCMenuItemImage *brush1item;//正常的 CCMenuItemImage *brush2item;//红旗 CCMenuItemImage *brush3item;//问号 int temp_mine_Number; bool Cantouch;//能否触摸 }; #endif // __HELLOWORLD_SCENE_H__

原文链接:https://www.f2er.com/cocos2dx/342498.html

猜你在找的Cocos2d-x相关文章