码迷,mamicode.com
首页 > 其他好文 > 详细

分享一个《连连看》的小游戏,cocos2dx版本

时间:2015-04-13 10:53:12      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:

先上效果图:

  

技术分享
技术分享
在设计算法上还是很值得钻研一下的!不过我自己实在是没有什么耐心了,就扔下不管了!
没有做好,就先这样吧
再贴一下代码:
#include "GameScene.h"


GameScene::GameScene(void)
{
    this->selected = -1;
}


GameScene::~GameScene(void)
{

}

bool GameScene::init()
{

    CCSize base_coord = CCSize(570,46);

    CCLayer::init();

    CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();

    this->background = CCSprite::create("board.png");
    this->background->setPosition(CCSize(visibleSize.width/2,visibleSize.height/2));

    this->addChild(this->background);


    for(int i=0;i<8;i++)
    {
        for(int j=0;j<8;j++)
        {
            ItemNode* node = new ItemNode(this,i*8 + j);
            node->init();
            node->setPosition(CCSize(base_coord.width + (node->getContentSize().width + 2) * i ,base_coord.height + (node->getContentSize().height + 2) * j));
            
            this->addChild(node);
            this->item_vect.push_back(node);
        }
    }


    return true;
}

void GameScene::update(float tick)
{

}

void GameScene::set_selected(int id)
{
    if(this->selected != -1)
    {
        ItemNode* node = this->item_vect[this->selected];
        if(node != NULL)
        {
            node->un_select();
        }

        ItemNode* new_node = this->item_vect[id];

        if(node->get_index() == new_node->get_index() && this->is_node_reach(id,selected))
        {
            new_node->setVisible(false);
            node->setVisible(false);
            this->selected = -1;
            return ;

        }

    }
    
    this->selected = id;
}

int GameScene::get_selected()
{
    return this->selected;
}

bool GameScene::is_node_reach(int start,int end)
{

    if(start == end)
    {
        return false;
    }

    int start_x = start/8;
    int start_y = start%8;

    int end_x = end/8;
    int end_y = end%8;

    int result = 0;
    result = abs(start_x - end_x) + abs(start_y - end_y);
    if(result == 1)
    {
        return true;
    }


    if(start_x == end_x)
    {

        if(start_y > end_y)
        {
            int temp = start_y;
            start_y = end_y;
            end_y = temp;
        }

        bool is_reach = true;
        for( int i = start_y + 1 ; i < end_y ; i++)
        {
            ItemNode* node = this->item_vect[start_x * 8 + i];
            if(node->isVisible())
            {
                is_reach = false;
            }
        }

        if(is_reach)
        {
            return true;
        }
        
    }

    if(start_y == end_y)
    {
        bool is_reach = true;

        if(start_x > end_x)
        {
            int temp = start_x;
            start_x = end_x;
            end_x = temp;
        }

        for(int i = start_x + 1 ; i < end_x ; i++)
        {
            ItemNode* node = this->item_vect[i*8 + start_y];
            if(node->isVisible())
            {
                is_reach = false;
            }
        }

        if(is_reach)
        {
            return true;
        }
    }

    {
        //检测 拐角
        //start_x,start_y   //end_x,end_y
        
        //start_x,end_y
        //end_x,start_y

        if(is_coord_reach(start_x,start_y,end_x,end_y))
        {
            return true;
        }
        

    }



    return false;
}

bool GameScene::is_coord_reach(int start_x,int start_y,int end_x,int end_y)
{

    if(start_x > end_x)
    {
        int temp = start_x;
        start_x = end_x;
        end_x = temp;
    }

    if(start_y > end_y)
    {
        int temp = start_y;
        start_y = end_y;
        end_y = temp;
    }

    {

        bool is_reach = true;
        for(int i=start_x + 1;i < end_x;i++)
        {
            ItemNode* node = this->item_vect[i*8 + start_y];    
            if(node->isVisible())
            {
                is_reach = false;
            }
        }

        for(int i = start_y + 1;i<end_y;i++)
        {
            ItemNode* node = this->item_vect[end_x*8 + i];
            if(node->isVisible())
            {
                is_reach = false;
            }
        }

        if(is_reach)
        {
            return true;
        }
    }

    {
        bool is_reach = true;

        for(int i=start_y+1;i < end_y;i++)
        {
            ItemNode* node = this->item_vect[start_x*8 + i];
            if(node->isVisible())
            {
                is_reach = false;
            }
        }

        for(int i=start_x+1;i < end_x;i++)
        {
            ItemNode* node = this->item_vect[i*8 + end_y];
            if(node->isVisible())
            {
                is_reach = false;
            }
        }

        if(is_reach)
        {
            return true;
        }
    }

    return false;
}

bool GameScene::is_coord_reach_i(int start_x,int start_y,int end_x,int end_y)
{

    return true;
}
#include "ItemNode.h"
#include "GameStruct.h"
#include "GameScene.h"


ItemNode::ItemNode(GameScene* scene,int id)
{
    this->scene = scene;
    this->index = 0;
    this->image = NULL;
    this->background = NULL;
    this->id = id;
}


ItemNode::~ItemNode(void)
{

}

bool ItemNode::init()
{
    CCNode::init();
    
    this->index = rand()%7;
    this->image = CCSprite::create(item_file_name[this->index].c_str());

    CCSize item_size = this->image->getContentSize();
    this->setContentSize(item_size);

    this->image->setPosition(CCSize(item_size.width/2,item_size.height/2));
    this->addChild(this->image);

    return true;
}

void ItemNode::onEnter()
{
    CCDirector* pDirector = CCDirector::sharedDirector();
    pDirector->getTouchDispatcher()->addTargetedDelegate(this, -128, false);
    CCNode::onEnter();
}

void ItemNode::onExit()
{
    CCDirector* pDirector = CCDirector::sharedDirector();
    pDirector->getTouchDispatcher()->removeDelegate(this);
    CCNode::onExit();
}

int ItemNode::get_index()
{
    return this->index;
}

void ItemNode::selected()
{
    this->background = CCSprite::create("selector.png");
    this->background->setPosition(CCSize(background->getContentSize().width/2,background->getContentSize().height/2));
    this->addChild(this->background);
}

void ItemNode::un_select()
{
    if(this->background != NULL)
    {
        this->removeChild(this->background);
    }
}

bool ItemNode::is_in_sprite(CCTouch* touch)
{
    CCPoint touchPoint = touch->getLocation();
    CCPoint reallyPoint = this->convertToNodeSpace(touchPoint);
    CCRect rect = this->boundingBox();
    
    if(rect.containsPoint(touchPoint))
    {
        return true;
    }

    return false;
}

bool ItemNode::ccTouchBegan(CCTouch* touch, CCEvent* event)
{
    if(this->isVisible())
    {
        if(this->is_in_sprite(touch))
        {
            this->scene->set_selected(this->id);
            this->selected();

        }
    }
    return true;
}

void ItemNode::ccTouchMoved(CCTouch* touch, CCEvent* event)
{
    
}

void ItemNode::ccTouchEnded(CCTouch* touch, CCEvent* event)
{

}

不能再这样写了,准备去谈几个生意了!做赚钱的东西!

分享一个《连连看》的小游戏,cocos2dx版本

标签:

原文地址:http://www.cnblogs.com/archy_yu/p/4421526.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!