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

先上效果图:

  

在设计算法上还是很值得钻研一下的!不过我自己实在是没有什么耐心了,就扔下不管了!

没有做好,就先这样吧

再贴一下代码:

#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)
{

}

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

时间: 2024-08-04 00:25:04

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

Cocos2d-X开发一个简单的小游戏

学了这么久Cocos2d-X,今天终于可以做出一个简单的小游戏了,游戏非常简单,通过菜单项控制精灵运动 在做游戏前,先学一个新概念 调度器(scheduler): Cocos2d-x调度器为游戏提供定时事件和定时调用服务.所有Node对象都知道如何调度和取消调度事件,使用调度器有几个好处: 每当Node不再可见或已从场景中移除时,调度器会停止. Cocos2d-x暂停时,调度器也会停止.当Cocos2d-x重新开始时,调度器也会自动继续启动. Cocos2d-x封装了一个供各种不同平台使用的调度

【C语言探索之旅】 第一部分第八课:第一个C语言小游戏

? 内容简介 1.课程大纲 2.第一部分第八课:第一个C语言小游戏 3.第一部分第九课预告: 函数 课程大纲 我们的课程分为四大部分,每一个部分结束后都会有练习题,并会公布答案.还会带大家用C语言编写三个游戏. C语言编程基础知识 什么是编程? 工欲善其事,必先利其器 你的第一个程序 变量的世界 运算那点事 条件表达式 循环语句 实战:第一个C语言小游戏 函数 练习题 习作:完善第一个C语言小游戏 C语言高级技术 模块化编程 进击的指针,C语言王牌 数组 字符串 预处理 创建你自己的变量类型 文

需求:有一个猜数字小游戏,请写一个程序实现在测试类中只能使用5次,超过5次提示:游戏试玩结束,请付费。

package cn.idcast4; import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.Reader;import java.io.Writer;import java.util.Properties; /* * 需求:有一个猜数字小游戏,请写一个程序实现在测试类中只能使用5次, *

html5面向对象做一个贪吃蛇小游戏

canvas加面向对象方式的贪吃蛇 2016-08-25 这个小游戏可以增加对面向对象的理解,可以加强js逻辑能力,总之认真自己敲一两遍收获还是不少啊!!适合刚学canvas的同学练习!! 废话不多说,直接来讲思路和代码. ----------------------------------------------------------------------------------------------------------------- 开发思路:首先要有蛇吃的食物,就是一个个canv

我的第一个Apple Watch小游戏——猜数字(Swift)

这是一个在AppleWatch上实现的一个小型App,开发语言为Swift.是一个猜数字的游戏,屏幕上会出现不同数字的滚动,并能控制游戏的开始结束,让别人来猜数字.是不是很有意思.还可以多个人来玩这个游戏,比大家谁最后的数字大. 该应用我已经上传至 https://github.com/chenyufeng1991/GuessNumber   . 由于该应用我主要是在Watch上实现的,所以在手机上不会有任何的效果,只会有一个白色的界面而已.实现步骤如下: (1)新建一个iOS中的Apple W

canvas学习作业,模仿做一个祖玛的小游戏

这个游戏的原理我分为11个步骤,依次如下: 1.布局, 2.画曲线(曲线由两个半径不同的圆构成) 3.画曲线起点起始圆和曲线终点终止圆 4.起始的圆动起来, 5.起始的圆沿曲线走起来 6.起始的圆沿曲线走起来,并在曲线初始位置处产生新圆 7.添加图片,这个图片是为了发射子弹 8.让图片跟随鼠标动起来 9.让动起来的图片跟随鼠标的位置发送子弹,并让子弹的颜色变红 10.图片发射的子弹和轨迹上的小圆碰撞检测 11.碰撞检测后让发射的子弹和轨迹上的小圆消失 这就是该程序步骤的的分解. 第一点:布局 <

一个小小的俄罗斯方块小游戏

用了八个小时 也挺不容易的 大神不喜勿喷 #include <iostream> #include <string> #include <ctime> #include <cstdlib> #include <windows.h> #include <conio.h> using namespace std; int block00[4][4] = { { 10,0,0,0 },{ 1,1,1,1 },{ 0,0,0,0 },{ 0,

写一个三子棋小游戏的感悟

在写之前,我已经把函数的结构设计出来,主要由以下几个函数实现: 1:初始化棋框 2:玩家下棋 3:电脑下棋 4:判断胜负 5:更新棋框 6:主函数 在写的过程中碰到了不能将玩家下的位置放入棋框中,原因是在函数定义过程中没有将二维数组引入, 导致棋框更新不正确,在函数中引用二维数组时,因为c语言编译系统不检查第一维的大小,只要第二维大小相同,形参数组第一维可以与实参不同,因此在函数定义时只需要写int qk(arr[][i]),i为常数,在主函数调用时只需要写qk(arr)即可,因为数组名本身就是

C++ 制作一个“测运”小游戏-rand()函数的应用

游戏说明: 游戏名:Lucky Guy 玩法说明:有2种模式可以选择,一种是一直选择数字,直到抽到炸弹为止.另一种是在0~9个数字中进行选择,有5个炸弹,最高分为5,抽到炸弹即游戏结束.游戏结束后,可以选择继续玩或者直接退出. 主要用到了rand()函数,具体用法可以参考:https://baike.baidu.com/item/rand%E5%87%BD%E6%95%B0/5916603 文件下载: 码云:https://gitee.com/ikaros-521/c_project/tree/

Demo1:编写一个电子宠物的小游戏——出自《深入浅出JavaScript》

需求背景: 结束一场成功的HTML与Css网页设计大会后,老板在办公室召见你,让你看看他的最新在线发明:iRock.虚拟宠物在玩具业大会已经造成轰动,但我们的测试用户却对这只在线宠物不太满意. 非常明显,用户看到石头,自然想点看看有什么新奇好玩的反应会出现……不过你的老板根本没想到要有“反应”这回事.现在,你得负责iRock的交互性,这颗石头会升天成为五彩缤纷的仙石,还是会被丢到垃圾桶里,就全看你了. 本次demo实现功能: 1.创建iRock的页面 2.页面加载完成后,弹出欢迎用户的窗口 3.