分享一个《连连看》的小游戏,cocos2dx版本_移动开发_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > 移动开发 > 分享一个《连连看》的小游戏,cocos2dx版本

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

 2015/4/13 11:37:05  archy_yu  程序员俱乐部  我要评论(0)
  • 摘要:先上效果图:在设计算法上还是很值得钻研一下的!不过我自己实在是没有什么耐心了,就扔下不管了!没有做好,就先这样吧再贴一下代码:#include"GameScene.h"GameScene::GameScene(void){this->selected=-1;}GameScene::~GameScene(void){}boolGameScene::init(){CCSizebase_coord=CCSize(570,46);CCLayer::init()
  • 标签:一个 游戏 版本

先上效果图:

  

在设计算法上还是很值得钻研一下的!不过我自己实在是没有什么耐心了,就扔下不管了! 没有做好,就先这样吧 再贴一下代码:
#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)
{

}

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

发表评论
用户名: 匿名