Cocos2d-x 个人笔记 《2048》(3)

开始写主要的 2048Scene.cpp 了。

2048Scene.h 的内容

#pragma once
#include "cocos2d.h"
#include <queue>
using namespace std;
using namespace cocos2d;

class My2048Scene:public CCLayer
	{
	public:
		My2048Scene();
		~My2048Scene();

		static CCScene * scene();

		void menuCall_restart(CCObject *pSender);
		void menuCall_return(CCObject *pSender);

		virtual void build();
		virtual bool init();
		virtual void My2048Move();
		virtual bool My2048Cheack();
		virtual bool My2048union(int m);
		virtual void Mydisplay(int x1,int y1,int x2,int y2);
		void Mypointadd();
		void My2048over();
		void My2048win();
		long long My2048Point;
		int My2048[5][5];
		CCLabelTTF *Mypoint;
		CCSize Mysize;
		CCSize size;
		CCSprite *Overboard;
		void onEnter();
		void onExit();
		virtual bool ccTouchBegan(CCTouch *pTouch,CCEvent *pEvent);
		virtual void ccTouchMoved(CCTouch *pTouch,CCEvent *pEvent);
		virtual void ccTouchEnded(CCTouch *pTouch,CCEvent *pEvent);

		CREATE_FUNC(My2048Scene);
	private:

		CCPoint startP,thendP;
		float move_dis;
		void get_ture_callback();
		int Myreverse;
		bool MyWin;
		queue<int>Myque;
	};

2048Sense.cpp 的全部代码

# include "2048Scene.h"
# include "MyScene.h"
# include <cstring>
# include <ctime>
# include <queue>
using namespace std;
#define FOR(i,a,b) for(int i=a;i<b;i++)

const int xx[]= {0,0,-1,1};
const int yy[]= {-1,1,0,0};

CCSprite *g[16];
const int PP[]= {0,2,4,8,16,32,64,128,256,512,1024,2048};
My2048Scene::My2048Scene()
{

}
My2048Scene::~My2048Scene()
{

}
CCScene* My2048Scene::scene()
{
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::create();

    // 'layer' is an autorelease object
    My2048Scene *layer = My2048Scene::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}
bool My2048Scene::init()
{

    size.width=480,size.height=854;
    move_dis=100;

    CCSprite *background_2048=CCSprite::create("homepage.png");
    background_2048->setPosition(ccp(size.width/2,size.height*0.5));
    this->addChild(background_2048);

    CCSprite *coord_2048=CCSprite::create("2048-coord.png");
    coord_2048->setPosition(ccp(size.width/2,size.height*0.5));
    this->addChild(coord_2048);

    //CCLOG("%f %f\n",size.width,size.height);

    Mysize=coord_2048->getPosition();
    Mysize.width-=150,Mysize.height-=10;
    //CCLOG("%f %f\n",Mysize.width,Mysize.height);

    /*int i=0,j=1;
    CCSprite *sp1=CCSprite::create("5.png");
    sp1->setPosition(ccp(Mysize.width+100*j,Mysize.height-100*i));
    this->addChild(sp1);*/

    CCLabelTTF *My_restart_tmp =CCLabelTTF :: create("restart" ,"fonts/FZNHT.TTF",40);
    CCMenuItemLabel *My_restart =CCMenuItemLabel::create(My_restart_tmp,this,menu_selector(My2048Scene::menuCall_restart));
    My_restart->setPosition(ccp(size.width/3*2,size.height*0.6));

    CCLabelTTF *My_return_tmp =CCLabelTTF :: create("return" ,"fonts/FZNHT.TTF",40);
    CCMenuItemLabel *My_return =CCMenuItemLabel::create(My_return_tmp,this,menu_selector(My2048Scene::menuCall_return));
    My_return->setPosition(ccp(size.width/3,size.height*0.6));

    CCMenu *menu =CCMenu::create(My_restart,My_return,NULL);
    menu->setPosition(CCPointZero);

    this->addChild(menu);

    CCLabelTTF *Mypoint_borad=CCLabelTTF::create("Point  :","fonts/FZNHT.TTF",40);
    Mypoint_borad->setColor(ccc3(255,255,0));
    Mypoint_borad->setPosition(ccp(size.width/5,size.height*0.675));
    this->addChild(Mypoint_borad);

    Mypoint=CCLabelTTF::create("0","fonts/FZNHT.TTF",40);
    Mypoint->setColor(ccc3(0,255,255));
    //Mypoint->setAnchorPoint(ccp(size.width/5*4,size.height*0.675));
    Mypoint->setPosition(ccp(size.width/5*4,size.height*0.675));
    this->addChild(Mypoint);

    FOR(m,0,16)
    {
        int i=m/4,j=m%4;
        My2048[i][j]=0;
        CCString *str=CCString::createWithFormat("%d.png",My2048[i][j]);
        g[i*4+j]=CCSprite::create(str->getCString());
        g[i*4+j]->setPosition(ccp(Mysize.width+100*j,Mysize.height-100*i));
        this->addChild(g[i*4+j]);
		//初始化全部覆盖透明图片
    }
    My2048Point=0;
    MyWin=false;

    time_t t;
    time(&t);
    t%=1000;
    t*=t;
    srand(t);

    build();
    build();
    //setTouchEnabled(false);
    setTouchEnabled(true);
	//设置触摸
    Myreverse=0;
    //My2048win();
    Overboard=NULL;
    return true;
}

void My2048Scene:: build()
{
    FOR(k,0,16)
    {
        int i=k/4,j=k%4;
        if(My2048[i][j]==0)
            Myque.push(k);
    }

    int m=CCRANDOM_0_1()*(Myque.size()-1)+1;
    //CCLOG("%d ",m);
    int tmp;
    while (m--&&!Myque.empty())
    {
        tmp=Myque.front();
        Myque.pop();
    }
    while(!Myque.empty())Myque.pop();
    m=tmp;
    int i=m/4,j=m%4;
    int or_2_4=CCRANDOM_0_1()*10;
    if(or_2_4>=8)
        My2048[i][j]=2;
    else
        My2048[i][j]=1;

	//随机生成2或者4

    g[i*4+j]->removeFromParentAndCleanup(true);
    //释放精灵图片

    //CCLOG("%d %d",i,j);
    CCString *str=CCString::createWithFormat("%d.png",My2048[i][j]);

    g[i*4+j]=CCSprite::create(str->getCString());

    g[i*4+j]->setPosition(ccp(Mysize.width+100*j,Mysize.height-100*i));
    this->addChild(g[i*4+j]);

    //CCLOG("%d %d==%d\n",i,j,My2048[i][j]);
}

void My2048Scene::Mydisplay(int x1,int y1,int x2,int y2)
{
//重载图片
    g[x1*4+y1]->removeFromParentAndCleanup(true);

    g[x2*4+y2]->removeFromParentAndCleanup(true);

    CCString *str1=CCString::createWithFormat("%d.png",My2048[x1][y1]);
    CCString *str2=CCString::createWithFormat("%d.png",My2048[x2][y2]);

    g[x1*4+y1]=CCSprite::create(str1->getCString());
    g[x2*4+y2]=CCSprite::create(str2->getCString());

    g[x1*4+y1]->setPosition(ccp(Mysize.width+100*y1,Mysize.height-100*x1));
    g[x2*4+y2]->setPosition(ccp(Mysize.width+100*y2,Mysize.height-100*x2));

    this->addChild(g[x1*4+y1]);
    this->addChild(g[x2*4+y2]);
}
bool My2048Scene::My2048union(int m)
{
//合并
    bool flag=false;
    bool pointadd=false;
    if(m==1)//l
    {
        FOR(i,0,4)
        {
            FOR(j,0,4)
            {
                if(My2048[i][j]==0)continue;
                for(int k=j+1; k<4; k++)
                {
                    if(My2048[i][k]==0)continue;
                    else if(My2048[i][k]==My2048[i][j])
                    {
                        My2048[i][j]++;
                        My2048[i][k]=0;
                        My2048Point+=PP[My2048[i][j]];
                        Mydisplay(i,j,i,k);
                        flag=true;
                        pointadd=true;
                        break;
                    }
                    else
                        break;
                }
            }
            FOR(j,0,4)
            {
                if(My2048[i][j]!=0)continue;
                for(int k=j+1; k<4; k++)
                {
                    if(My2048[i][k]==0)continue;
                    int tmp=My2048[i][j];
                    My2048[i][j]=My2048[i][k];
                    My2048[i][k]=tmp;
                    Mydisplay(i,j,i,k);
                    flag=true;
                    break;
                }
            }
        }

    }
    else if(m==2)//r
    {
        FOR(i,0,4)
        {
            for(int j=3; j>=0; j--)
            {
                if(My2048[i][j]==0)continue;
                for(int k=j-1; k>=0; k--)
                {
                    if(My2048[i][k]==0)continue;
                    else if(My2048[i][k]==My2048[i][j])
                    {
                        My2048[i][j]++;
                        My2048[i][k]=0;
                        My2048Point+=PP[My2048[i][j]];
                        Mydisplay(i,j,i,k);
                        flag=true;
                        pointadd=true;
                        break;
                    }
                    else
                        break;
                }
            }
            for (int j=3; j>=0; j--)
            {
                if(My2048[i][j]!=0)continue;
                for(int k=j-1; k>=0; k--)
                {
                    if(My2048[i][k]==0)continue;
                    int tmp=My2048[i][j];
                    My2048[i][j]=My2048[i][k];
                    My2048[i][k]=tmp;
                    Mydisplay(i,j,i,k);
                    flag=true;
                    break;
                }
            }
        }
    }
    else if(m==3)//d
    {
        FOR(i,0,4)
        {
            for(int j=3; j>=0; j--)
            {
                if(My2048[j][i]==0)continue;
                for(int k=j-1; k>=0; k--)
                {
                    if(My2048[k][i]==0)continue;
                    else if(My2048[k][i]==My2048[j][i])
                    {
                        My2048[j][i]++;
                        My2048[k][i]=0;
                        My2048Point+=PP[My2048[j][i]];
                        Mydisplay(j,i,k,i);
                        pointadd=true;
                        flag=true;
                        break;
                    }
                    else
                        break;
                }
            }
            for (int j=3; j>=0; j--)
            {
                if(My2048[j][i]!=0)continue;
                for(int k=j-1; k>=0; k--)
                {
                    if(My2048[k][i]==0)continue;
                    int tmp=My2048[j][i];
                    My2048[j][i]=My2048[k][i];
                    My2048[k][i]=tmp;
                    Mydisplay(j,i,k,i);
                    flag=true;
                    break;
                }
            }
        }
    }
    else if(m==4)//u
    {
        FOR(i,0,4)
        {
            FOR(j,0,4)
            {
                if(My2048[j][i]==0)continue;
                for(int k=j+1; k<4; k++)
                {
                    if(My2048[k][i]==0)continue;
                    else if(My2048[k][i]==My2048[j][i])
                    {
                        My2048[j][i]++;
                        My2048[k][i]=0;
                        My2048Point+=PP[My2048[j][i]];
                        Mydisplay(j,i,k,i);
                        pointadd=true;
                        flag=true;
                        break;
                    }
                    else
                        break;
                }
            }
            FOR(j,0,4)
            {
                if(My2048[j][i]!=0)continue;
                for(int k=j+1; k<4; k++)
                {
                    if(My2048[k][i]==0)continue;
                    int tmp=My2048[j][i];
                    My2048[j][i]=My2048[k][i];
                    My2048[k][i]=tmp;
                    Mydisplay(j,i,k,i);
                    flag=true;
                    break;
                }
            }
        }
    }
    if(pointadd)
    {
        Mypointadd();
    }

    return flag;
}

void My2048Scene::Mypointadd()
{
//加分
    Mypoint->setString(CCString::createWithFormat("%lld",My2048Point)->getCString());
}
void My2048Scene::My2048Move()
{
// 开始主动作
    bool flag=false;
    if(Myreverse==0)
        return ;
    else
        flag=My2048union(Myreverse);

    if(flag)
    {
        build();
    }
    else
        return;

    bool gameflag=false;
    gameflag=My2048Cheack();
    if(MyWin)
    {
        //system("pause");
        CCLOG("Win\n");
        My2048win();
    }
    else if(!gameflag)
    {
        //system("pause");
        CCLOG("over\n");
        My2048over();
    }
}
void My2048Scene::My2048win()
{
//游戏胜利画面
    Overboard=CCSprite::create("overboard.png");
    Overboard->setPosition(ccp(Mysize.width+150,Mysize.height-150));
    this->addChild(Overboard,100);

    CCLabelTTF *Win_flag=CCLabelTTF::create("YOU WIN","fonts/FZNHT.TTF",40);
    Win_flag->setColor(ccc3(255,0,0));
    CCSize tmp=Overboard->getPosition();
    Win_flag->setPosition(ccp(tmp.width-125,tmp.height-125));
    Overboard->addChild(Win_flag);

    CCLabelTTF *board_point=CCLabelTTF::create("0","fonts/FZNHT.TTF",30);
    board_point->setString(CCString::createWithFormat("%lld",My2048Point)->getCString());
    board_point->setColor(ccc3(0,0,0));
    board_point->setPosition(ccp(tmp.width/2,tmp.height/6));
    Overboard->addChild(board_point);
    this->setTouchEnabled(false);
}
void My2048Scene::My2048over()
{
//游戏结束画面
    Overboard=CCSprite::create("overboard.png");
    Overboard->setPosition(ccp(Mysize.width+150,Mysize.height-150));
    this->addChild(Overboard,100);

    CCLabelTTF *Lost_flag=CCLabelTTF::create("YOU LOST","fonts/FZNHT.TTF",40);
    Lost_flag->setColor(ccc3(0,255,255));
    CCSize tmp=Overboard->getPosition();
    Lost_flag->setPosition(ccp(tmp.width-125,tmp.height-125));
    Overboard->addChild(Lost_flag);

    CCLabelTTF *board_point=CCLabelTTF::create("0","fonts/FZNHT.TTF",30);
    board_point->setString(CCString::createWithFormat("%lld",My2048Point)->getCString());
    board_point->setColor(ccc3(0,0,0));
    board_point->setPosition(ccp(tmp.width/2,tmp.height/6));
    Overboard->addChild(board_point);
    this->setTouchEnabled(false);
}

bool My2048Scene::My2048Cheack()
{
//检查胜利或者失败
    bool flag=false;
    FOR(i,0,4)
    FOR(j,0,4)
    {
        if(My2048[i][j]==11)MyWin=true;
    }
    FOR(i,0,4)
    FOR(j,0,4)
    {
        FOR(k,0,4)
        {
            int x=i+xx[k];
            int y=j+yy[k];
            if(x<0||y<0||x>=4||y>=4)continue;

            if(!My2048[i][j]||My2048[i][j]==My2048[x][y])
                return true;
        }
    }
    return false;
}

void My2048Scene::get_ture_callback()
{
//判断触摸方向
    Myreverse=0;
    CCPoint ans=ccpSub(startP,thendP);
    if(ans.x*ans.x+ans.y*ans.y<move_dis*move_dis)return;
    if (fabs(ans.x)>fabs(ans.y))
    {
        if (ans.x>move_dis)
        {
            //CCLOG("l");
            Myreverse=1;

        }
        else if(ans.x<-move_dis)
        {
            //CCLOG("r");
            Myreverse=2;

        }
    }
    else
    {
        if (ans.y>move_dis)
        {
            //CCLOG("d");
            Myreverse=3;

        }
        else
        {
            //CCLOG("u");
            Myreverse=4;

        }
    }

    My2048Move();

}

void My2048Scene::onEnter()
{
    CCLayer::onEnter();
    CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,0,false);

}

void My2048Scene::onExit()
{
    CCLayer::onExit();
    CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
}
bool My2048Scene::ccTouchBegan(CCTouch *pTouch,CCEvent *pEvent)
{
//开始触摸
    startP=pTouch->getLocation();

    return true;
}

void My2048Scene::ccTouchMoved(CCTouch *pTouch,CCEvent *pEvent)
{
//触摸点移动中
//thendP=pTouch->getLocation();

}

void My2048Scene::ccTouchEnded(CCTouch *pTouch,CCEvent *pEvent)
{
//触摸结束
    thendP=pTouch->getLocation();
    Myreverse=0;
    get_ture_callback();
}

void My2048Scene::menuCall_restart(CCObject *pSender)
{
//restart 菜单
    if(Overboard!=NULL)
        Overboard->removeFromParentAndCleanup(true);
    CCScene *temps=My2048Scene::scene();
    CCDirector::sharedDirector()->replaceScene(temps);
}
void My2048Scene::menuCall_return(CCObject *pSender)
{
//return 菜单
    CCScene *temps=MyScene::scene();
    CCDirector::sharedDirector()->replaceScene(temps);
}

中间遇到过很多问题

比如 随机数,开始随机生成 坐标,却会出现一直循环,就是随机不到最后一个空的格子。

我就用了队列,把所有空的格子入队,然后随机出几个队,选一个就好。

还有触摸方向,我检测的是起点和终点的 坐标 x ,y 的距离。然后判断上下左右。

最麻烦的是 移动的时候上下左右了,刚好和坐标系的有点不一样。

我是采用的矩阵数组,左上角是0,0 。而计算坐标的时候 左下角才是初始。然后就要 一个加,一个减。

游戏结束后,屏幕滑动还能继续,又忧伤我半天,最后就 加上触摸关闭。

资源文件:

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

时间: 2024-08-12 04:40:44

Cocos2d-x 个人笔记 《2048》(3)的相关文章

cocos2d基础篇笔记四

1.//有两种集合 //第一种是array 特点:插入,删除效率低,可是查找效率高 //另外一种是list  特点:插入,删除效率高,可是查找效率低 //分析这个游戏: 插入的时候:怪物,射弹出现时,删除的时候:碰撞时,怪物.射弹出界时. //遍历:fps(每秒中填充图像的帧数(帧/秒)相应的时间,怪物是2秒出现一次,而遍历是60次每秒,可见遍历用的较多,所以我们选择array. CCArray*_targets;//定义怪物集合,3.0一般用vector定义集合 CCArray*_projs;

Cocos2d-x 个人笔记 《2048》(1)

想来想去还是想做游戏,毕竟狂热.想来想去还是2048比较简单.于是就开始瞎做了. 顺道写个笔记,记录一下. (1)Cocos2d-x 配置 cocos2d官网下载的 cocos2d-x-2.2.3 还有 随意下载 Python . 我的Cocos2d-x 解压放在D盘根目录下的,Python默认安装在C盘. Python修改一下环境变量 创建项目可以使用 D:\cocos2d-x-2.2.3\tools\project-creator 目录下的 create_project.py 进入控制台 d

[cocos2dx笔记008]cocos2d 用luabridge手动绑定类

基于cocos2dx 2.2.2版本.这几天使用了cocostudio实现了,动画,骨骼动画,UI编辑,粒子效果,虽然有些不足,但已经算是非常好了.今天尝试用lua,这个非常简单,创建的时候,设置语言为lua,那就可以创建lua工程. 在cocos2d-x-2.2.2\tools\project-creator下运行: python create_project.py -project test_lua -package com.android.zdhsoft -language lua xco

[ios5 cocos2d游戏开发实战] 笔记3-FileUtils, notificationCenter

FileUtils //文件管理工具 FileUtils::getInstance() std::string getStringFromFile(const std::string& filename);//读取文件中的字符串 Data getDataFromFile(const std::string& filename);//获取文件数据 void setSearchPaths(const std::vector<std::string>& searchPaths

Cocos2D学习笔记(1)- 常用的类

1.坐标系 >屏幕坐标系(UIKit):原点在左上角! >OpenGl坐标系:原点在屏幕的左下角! 2.游戏设计:Director--Scene--Layer--Sprite. >CCDirector:导演类,相当于是游戏策划,负责整个游戏的布局和运行规则的制定. >CCScene:场景类,每个场景可以是一个界面或一个关卡. >CCLayer:图层类,为了方便游戏界面的渲染管理. >CCSprite:精灵类, 小结:一个导演类(CCDirector)可以指挥多个场景类(

Cocos2d-x 个人笔记 《2048》(5)

- - 想做特效. 我写的合并方法是调用 display 重绘精灵的图片,使用CCActionInterval 后,我打算把重构精灵延时. 然后就乱了...动作线程还没执行玩,精灵就销毁了什么的.还有定时器还没执行就又调用定时器什么. CCScheduler#scheduleSelector. Selector already scheduled...这之类的错误. 果断重写,把每个要移动的矩阵元素 的移动目标保存下来,然后一次执行所有的动作,再延时重载精灵图片. 用了一个 b[][] 存移动到

Cocos2d-x 3.2 学习笔记(十一)Game 2048

一.规则 游戏2048的规则很简单 2+2=4  4+4=8 8+8=16 16+16=32 ……1024+1024=2048 这游戏挺火的……不过做起来也不难,就用cocos2dx做一下,首先我也没看过别人 怎么做的,当然写的也不是很好,能玩…… 二.游戏 游戏的界面是用画的,数字是写上去的,卡片的移动效果没有做,加入了声音,玩起来还行. 1.主要的类只有四个: GameScene 主场景(分值显示.菜单按钮.游戏区域) GameLayer 游戏操作区域(数值变化.逻辑控制) GameItem

Cocos2d-x 个人笔记 《2048》(2)

我自己的爪机是 480*854 的分辨率的,但是--笔记本也就 768 的高度. 于是: eglView->setViewName("2048"); eglView->setFrameSize(240, 427); 窗口缩小一半方便调试. AppDelegate.cpp 就是Cocos2d的入口了. pDirector->setDisplayStats(false); 是否打开FPS,可以 true 打开,后面发布的时候再 false 关闭. CCEGLView::s

Cocos2d-x 个人笔记 《2048》(4)

忧伤的Android移植之路. 1)首先得安装eclipse,android-sdk,android-ndk-r9d,Java.eclipse 还的安装插件ADT 插件,各种环境变量.反正能保证第一个Android HelloWorld能成功就好.这就不详细说了. 2)我的这些全部放在D盘根目录下,如果有变化,请找到相应安装目录. 3) 打开Eclipse 开始导入包. 4)导入一个Android包 5)选择你的项目下的proj.android 6)复制Cocos2d相关的库文件到自己的项目下面