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

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

概述资源图片  http://yunpan.cn/cQ63WwezMEmWD (提取码:715f) 棋盘绘制好了,接下来就是触摸了 通过触摸上一层,使其显示下一层的情况 触摸代码 bool HelloWorld::ccTouchBegan(cocos2d::CCTouch *pTouch,cocos2d::CCEvent *pEvent) { CCPoint p=pTouch->getLocation

资源图片 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__

总结

以上是内存溢出为你收集整理的Cocos2dx------详细介绍如何编写扫雷这个游戏含源码(二)全部内容,希望文章能够帮你解决Cocos2dx------详细介绍如何编写扫雷这个游戏含源码(二)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1062349.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存