cocos2dx连连看

本文提供了一款连连看游戏的开发教程,包括游戏界面、精灵管理、连接判断、以及多种连接方式实现。教程中详细解释了游戏核心逻辑,如初始化界面、精灵放置、触摸事件处理等,并提供了代码实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

算法么,网上的基本上都看了下,


我也是新手来着,代码很乱,如果你写连连看遇到难处了,就下下来看看!


下下来的文件 一个资源文件一个 class   覆盖下自己的就可以了,记得把 class文件夹里的main.cpp  拷贝到win.32文件夹里去覆盖下  要不自己改成 800*480的分辨率也行!



上代码!

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"
USING_NS_CC;
using namespace std;

class HelloWorld : public cocos2d::CCLayer
{
public:
	HelloWorld();
	~HelloWorld();
    virtual bool init();  
    static cocos2d::CCScene* scene();
    void menuCloseCallback(CCObject* pSender);
	
	virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
	virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);
	virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
	virtual void ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent);
	virtual void registerWithTouchDispatcher();
	//初始化界面
	bool initInterface(int bar,int num);
	void setPos(CCSprite* sprite);

	void callbackND(CCNode* sender, void* data);

	
    CREATE_FUNC(HelloWorld);

	//void showLine(CCScale9Sprite* lineSprite);
	//直接连接
	bool match_direct(cocos2d::CCPoint a, cocos2d::CCPoint b);
	//一个转折点
	bool match_one_corner(cocos2d::CCPoint a, cocos2d::CCPoint b);
	//两个转折点
	bool match_two_corner(cocos2d::CCPoint a, cocos2d::CCPoint b);
	//连接判断
	bool match(cocos2d::CCPoint a,cocos2d::CCPoint b);
	//是否为可见的点
	bool isValiableNode(CCPoint a);
	//是否是空的点(没有精灵就是空的点)
	bool isEmptyNode(CCPoint a);
private:
	CCPoint m_point1;//精灵的第一个点
	CCPoint m_point2;//精灵的第二个点
	CCPoint m_point3;
	int m_count;//计数
	int m_array[18][10];//标记位置是否有精灵(0为没有精灵,1为有精灵)
	int m_tag1;//精灵标记1
	int m_tag2;//精灵标记2
	int m_tag3;
	list<CCSprite*> m_spriteList;//精灵列表
	CCSprite* m_sprite1;//过渡精灵1
	CCSprite* m_sprite2;//过渡精灵2
	CCSprite* m_sprite3;
	CCSprite* m_frameSprite;//框框精灵
	bool m_oneLine;//一条直接连接
	bool m_twoLine;//二条直线连接
	bool m_threeLine;//三条直线连接
	CCPoint m_crisis1;//转折点1
	CCPoint m_crisis2;//转折点2
	
};

#endif // __HELLOWORLD_SCENE_H__



cpp文件


#include "HelloWorldScene.h"




HelloWorld::HelloWorld():m_point1(ccp(0,0)),m_point2(ccp(0,0)),m_count(0){
	for (int i = 0;i < 18;i++)
	{
		for (int j = 0;j < 10;j++)
		{
			m_array[i][j] = 0;
		}
	}
	m_oneLine = false;
	m_twoLine = false;
	m_threeLine = false;
}
HelloWorld::~HelloWorld(){

}


CCScene* HelloWorld::scene()
{
    CCScene *scene = CCScene::create();
    HelloWorld *layer = HelloWorld::create();
    scene->addChild(layer);
    return scene;
}


bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !CCLayer::init() )
    {
        return false;
    }
    //随机种子
	srand(time(NULL));

	CCSize size = CCDirector::sharedDirector()->getWinSize();
	CCSprite* sprite = CCSprite::create("black.jpg");
	sprite->setPosition(ccp(size.width/2,size.height/2));
	addChild(sprite,-1);

    CCMenuItemImage *closeItem = CCMenuItemImage::create(
                                        "CloseNormal.png",
                                        "CloseSelected.png",
                                        this,
                                        menu_selector(HelloWorld::menuCloseCallback));
	closeItem->setAnchorPoint(ccp(1,1));
	closeItem->setPosition(ccp(size.width,size.height));
    CCMenu* pMenu = CCMenu::create(closeItem, NULL);
    pMenu->setPosition(CCPointZero);
    this->addChild(pMenu, 1);

	initInterface(1,20);
    
    this->setTouchEnabled(true);

    return true;
}

bool HelloWorld::initInterface(int bar,int num){
	bool bRet = false;
	do 
	{
		char str[20] ={0};	
		for (int i = 0;i < 90;i++)
		{
			
			int num = rand()%20;
			sprintf(str,"touxiang/%d.png",num);
			CCSprite* sprite = CCSprite::create(str);
			CCSprite* sprite1 = CCSprite::create(str);
			this->setPos(sprite);
			this->setPos(sprite1);
			sprite->setTag(num);
			sprite1->setTag(num);
			m_spriteList.push_back(sprite);
			m_spriteList.push_back(sprite1);
			addChild(sprite,1);
			addChild(sprite1,1);		
		}
		
		m_frameSprite = CCSprite::create("frame.png");
		addChild(m_frameSprite,1);
		m_frameSprite->setVisible(false);
		bRet = true;
	} while (0);
	return bRet;
}

void HelloWorld::setPos(CCSprite* sprite){
	do 
	{
		int x = rand()%18;
		int y = rand()%10;
		if (m_array[x][y] == 1)
		{
			continue;
		}else{
			m_array[x][y] = 1;
			sprite->setPosition(ccp((x+1)*40+20,(y+1)*40 +20));
			break;
		}
	} while (1);
}



void HelloWorld::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent){
	
	
	CCPoint loction = pTouch->getLocationInView();
	loction = CCDirector::sharedDirector()->convertToGL(loction);
	list<CCSprite*>::iterator it = m_spriteList.begin();
	for (int i = 0;i < 180,it != m_spriteList.end();++i,++it)
	{
		if(*it){
			if ((*it)->boundingBox().containsPoint(loction))
			{
				m_count++;
				if (m_count==1)
				{
					m_tag1 = (*it)->getTag();
					m_point1 = (*it)->getPosition();
					m_sprite1 = *it;
					m_frameSprite->setPosition(m_point1);
					m_frameSprite->setVisible(true);

				}else if(m_count ==2){
					m_point2 = (*it)->getPosition();
					m_sprite2 = *it;
					m_tag2 = (*it)->getTag();
					m_frameSprite->setPosition(m_point2);
					m_frameSprite->setVisible(true);
				}else{
					m_tag3 = (*it)->getTag();
					m_point3 = (*it)->getPosition();
					m_sprite3 = *it;
					m_frameSprite->setPosition(m_point3);
					m_frameSprite->setVisible(true);

					m_point1 = m_point2;
					m_point2 = m_point3;

					m_tag1 = m_tag2;
					m_tag2 = m_tag3;

					m_sprite1 = m_sprite2;
					m_sprite2 = m_sprite3;
				}	
				break;
			}
		}
	}
	ccColor4F cc;
	cc.a = 1.0f;
	cc.b = 1.0f;
	cc.g = 0.0f;
	cc.r = 0.0f;
	if(m_count >= 2  && !isEmptyNode(loction)){
		if(m_tag1 == m_tag2 && match(m_point1,m_point2)){
			if (m_oneLine == true)
			{	
				CCDrawNode* lineOne = CCDrawNode::create();
				lineOne->drawSegment(m_point1,m_point2,5.0f,cc);
				addChild(lineOne,11);
				 CCCallFuncND * funcall= CCCallFuncND::create(
					 this, callfuncND_selector(HelloWorld::callbackND) ,(void*)lineOne);
				
				CCSequence* seq = CCSequence::create(CCDelayTime::create(0.4f),funcall,NULL);
				lineOne->runAction(seq);

				m_oneLine = false;	
			}else if (m_twoLine == true)
			{
				CCDrawNode* lineOne = CCDrawNode::create();
				lineOne->drawSegment(m_point1,m_crisis1,5.0f,cc);
				lineOne->drawSegment(m_crisis1,m_point2,5.0f,cc);
				addChild(lineOne,11);
				CCCallFuncND * funcall= CCCallFuncND::create(
					this, callfuncND_selector(HelloWorld::callbackND) ,(void*)lineOne);

				CCSequence* seq = CCSequence::create(CCDelayTime::create(0.4f),funcall,NULL);
				lineOne->runAction(seq);
				m_twoLine = false;

			}else if(m_threeLine == true){
				CCDrawNode* lineOne = CCDrawNode::create();
				lineOne->drawSegment(m_point1,m_crisis2,5.0f,cc);
				lineOne->drawSegment(m_crisis2,m_crisis1,5.0f,cc);
				lineOne->drawSegment(m_crisis1,m_point2,5.0f,cc);
				addChild(lineOne,11);
				CCCallFuncND * funcall= CCCallFuncND::create(
					this, callfuncND_selector(HelloWorld::callbackND) ,(void*)lineOne);

				CCSequence* seq = CCSequence::create(CCDelayTime::create(0.4f),funcall,NULL);
				lineOne->runAction(seq);

				m_threeLine =  false;
			}
			
			it = m_spriteList.begin();
			for (;it != m_spriteList.end();++it)
			{
				if(*it == m_sprite1){
					removeChild(m_sprite1,true);
					m_spriteList.erase(it);
					break;
				}
			}
			it = m_spriteList.begin();
			for (;it != m_spriteList.end();++it)
			{
				if(*it == m_sprite2){
					removeChild(m_sprite2,true);
					m_spriteList.erase(it);
					break;
				}
			}
			m_tag2 = -1;
			m_tag1 = -1;
			m_frameSprite->setVisible(false);
		}
	}

}


void HelloWorld::callbackND(CCNode* sender, void* data){
	CCDrawNode* line = (CCDrawNode*)data;
	removeChild(line,true);
}


bool HelloWorld::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent){
	return true;
}
void HelloWorld::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent){}

void HelloWorld::ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent){}

void HelloWorld::registerWithTouchDispatcher(){
	CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,0,true);
}


//一直线
bool HelloWorld::match_direct(cocos2d::CCPoint a, cocos2d::CCPoint b)
{
	if (a.x == b.x && a.y == b.y) {
		return false;
	}
	int i;
	bool match_x = false;
	if(a.x == b.x) {
		match_x = true;
		if(a.y > b.y) {
			if (a.y - 40 != b.y)
			{
				for (int i = a.y - 40 ; i >= b.y + 40; i -= 40)
				{
					CCPoint point = CCPointMake(a.x , i );
					if(!this->isValiableNode(point) || !this->isEmptyNode(point)){
						match_x = false;
					}
				}
			}
		}
		if(b.y > a.y) {
			
			if(b.y - 40 != a.y){
				for (int i = b.y - 40; i >= a.y + 40; i -= 40)
				{
					CCPoint point = CCPointMake(b.x , i );
					if(!this->isValiableNode(point) || !this->isEmptyNode(point)){
						match_x = false;
					}
				}
			}
		}
	}

	bool match_y = false;
	if(a.y == b.y) {
		match_y = true;
		if(a.x > b.x) {
			if(a.x - 40 != b.x){
				for(i = a.x - 40 ; i >= b.x + 40; i -= 40) {
					CCPoint point = CCPointMake(i , a.y );
					if(!this->isValiableNode(point) || !this->isEmptyNode(point)) {
						match_y = false;
					}
				}
			}
		}
		if(b.x > a.x) {
			if(b.x -40 != a.x){
				for(i = b.x  - 40 ; i >= a.x + 40 ; i-=40) {
					CCPoint point = CCPointMake(i , b.y );
					if(!this->isValiableNode(point) || !this->isEmptyNode(point)) {
						match_y = false;
					}
				}
			}
		}
	}

	return match_x || match_y;
}

//一个拐点的
bool HelloWorld::match_one_corner(cocos2d::CCPoint a, cocos2d::CCPoint b)
{
	CCPoint point = CCPointMake(b.x, a.y);

	if( this->isValiableNode(point) && this->isEmptyNode(point) && this->match_direct(a, point) && this->match_direct(b, point)){
		m_crisis1 = point;
		return true;
	}

	point = CCPointMake(a.x , b.y);
	if( this->isValiableNode(point) && this->isEmptyNode(point) && this->match_direct(a, point) && this->match_direct(b, point)){
		m_crisis1 = point;
		return true;
	}

	return false;
}

//两个拐点的
bool HelloWorld::match_two_corner(cocos2d::CCPoint a, cocos2d::CCPoint b)
{
	for(int i = a.x  - 40; i >= 0; i -=40) {
		CCPoint point = CCPointMake(i , a.y);
		if (this->isValiableNode(point) && this->isEmptyNode(point)) {
			if (this->match_one_corner(point, b)) {
				m_crisis2 = point;
				return true;
			}
		} else {
			break;
		}
	}

	for(int i = a.x + 40 ; i < 800; i += 40) {
		CCPoint point = CCPointMake(i , a.y);
		if (this->isValiableNode(point) && this->isEmptyNode(point)) {
			if (this->match_one_corner(point, b)) {
				m_crisis2 = point;
				return true;
			}
		}else {
			break;
		}
	}

	for(int i = a.y  - 40 ; i >= 0; i -= 40) {
		CCPoint point = CCPointMake(a.x ,i);
		if (this->isValiableNode(point) && this->isEmptyNode(point)) {
			if (this->match_one_corner(point, b)) {
				m_crisis2 = point;
				return true;
			}
		} else {
			break;
		}
	}

	for(int i = a.y + 40 ; i < 480; i+=40) {
		CCPoint point = CCPointMake(a.x ,i );
		if (this->isValiableNode(point) && this->isEmptyNode(point)) {
			if (this->match_one_corner(point, b)) {
				m_crisis2 = point;
				return true;
			}
		} else {
			break;
		}
	}

	return false;
}


bool HelloWorld::match(cocos2d::CCPoint a,cocos2d::CCPoint b)
{
	if (this->match_direct(a, b)) {
		m_oneLine = true;
		return true;
	}
	if (this->match_one_corner(a, b)) {
		m_twoLine = true;
		return true;
	}
	if (this->match_two_corner(a, b)) {
		m_threeLine  = true;
		return true;
	}

	return false;
}


bool HelloWorld::isValiableNode(CCPoint a){
	if (a.x > 800 || a.y > 480)
	{
		return false;
	}
	
	return true;
}
bool HelloWorld::isEmptyNode(CCPoint a){
	list<CCSprite*>::iterator it = m_spriteList.begin();
	for (int i = 0;i < 180,it != m_spriteList.end();++i,++it)
	{
		if(*it){
			if ((*it)->boundingBox().containsPoint(a))
			{
				return false;
			}
		}
	}
	return true;
}

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
}


网盘下载地址


http://pan.baidu.com/s/1qWwHDcK


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值