cocos2d_x_07_游戏_别踩白块儿

cocos2d_x_07_游戏_别踩白块儿,第1张

概述最终效果图: 环境版本:cocos2d-x-3.3beta0 没有使用到物理引擎 游戏场景 //// WhiteSquareScene.h// 01_cocos2d-x//// Created by beyond on 14-10-7.////#ifndef ___1_cocos2d_x__WhiteSquareScene__#define ___1_cocos2d_x__ 最终效果图:
环境版本:cocos2d-x-3.3beta0 没有使用到物理引擎 游戏场景
////  WhiteSquareScene.h//  01_cocos2d-x////  Created by beyond on 14-10-7.////#ifndef ___1_cocos2d_x__WhiteSquareScene__#define ___1_cocos2d_x__WhiteSquareScene__#include "Square.h"#include "Endline.h"class Endline;USING_NS_CC;class WhiteSquareScene : public cocos2d::Layer{    private:    // 屏幕尺寸    Size winSize;    // 临时累积 当前 总共添加了多少行 (当到了50行时,添加结束行)    int tempTotalline;        // 是否 正在 显示 结束行    bool isEndlineshowing;    // 计时器标签    Label *timerLabel;        // 用一个Node 包装起来 所有的东东,方便进行整体控制    Node *_gameLayer;        // 标记 是否正在计时    bool isTimerRunning;    // 游戏开始时的时间戳    long startTime;        // 结束行    Endline * currentEndline;    public:    // static create() 方法 内部调用init    CREATE_FUNC(WhiteSquareScene);    virtual bool init();    // 供外界调用(导演)    static cocos2d::Scene* createScene();        // 添加开始行    voID addStartline();    // 添加结束行    voID addEndline();    // 添加 正常的游戏行,最下面是起始行,正常行的 行号 从 1 开始 2、3等等    // 之所以在 添加正常的行时,要传入 行号作为参数,是因为,行号 决定 了该正常行 在屏幕中的Y值    voID addnormalline(int lineIndex);        // 添加 监听当前 Layer的侦听器,如果 点击的是正常游戏行的第一行,就开始游戏    voID addLayertouchHandler();        // 游戏控制  游戏初始化    voID initGame();    // 游戏层 整体下移    voID gameLayerMoveDown();    voID startTimer();    voID stopTimer();            virtual voID update(float dt);};#endif /* defined(___1_cocos2d_x__WhiteSquareScene__) */


////  WhiteSquareScene.cpp//  01_cocos2d-x////  Created by beyond on 14-10-7.////#include "WhiteSquareScene.h"#define kSquareWIDth winSize.wIDth/4#define kSquareHeight winSize.height/4USING_NS_CC;Scene* WhiteSquareScene::createScene(){    auto scene = Scene::create();    // create方法 内部会调用init方法    auto layer = WhiteSquareScene::create();    scene->addChild(layer);    return scene;}#pragma mark - 添加行(开始行、正常的游戏行、结束行)// 添加 开始行 占屏幕底部的 四分之一,不用显示文字voID WhiteSquareScene::addStartline(){    auto b = Square::createWithArgs(color3B::YELLOW,Size(winSize.wIDth,kSquareHeight),"",20,color4B::BLACK);    // 添加到游戏层,方便管理    _gameLayer->addChild(b);        // 绑定 其所在的 行号;开始行 是0,正常行 是1、2、3    b->setlineIndex(0);}// 添加 黑白相间的行 (该行中 黑色只有一个,且随机出现)// 之所以在 添加正常的行时,行号 决定 了该正常行 在屏幕中的Y值voID WhiteSquareScene::addnormalline(int lineIndex){    // 每添加一个游戏行,用成员变量 记住,当到50行时,就添加结束行    tempTotalline++;        Square *b;    // 正常行中 黑色只有一个,且随机出现    int blackIndex = rand()%4;    // 创建4个 小方块    for (int i=0; i<4; i++)    {        color3B color = blackIndex==i?color3B::BLACK:color3B::WHITE;        // 宽度和高度 均为屏幕的四分之一,减去1是为了 有1个缝隙        b = Square::createWithArgs(color,Size(kSquareWIDth - 1,kSquareHeight - 1),color4B::BLACK);        // 添加到游戏层,方便管理        _gameLayer->addChild(b);                // 重要~~~最下面是起始行,正常行的 行号 从 1 开始 2、3等等        b->setposition(i * kSquareWIDth,lineIndex * kSquareHeight);        // 绑定 其所在的 行号        // 每一个方块 自己记住自己是在 哪一个正常的行里,因为行号 决定 了Square的Y值        b->setlineIndex(lineIndex);    }        }// 添加 结束行 铺满全屏幕的 绿色的方块,还要显示文字voID WhiteSquareScene::addEndline(){    auto b = Endline::createWithContext(this);    // 绑定 其所在的 行号;开始行 是0,正常行 是1、2、3;结束行是 4    b->setlineIndex(4);        b->setpositionY(b->getlineIndex() * kSquareHeight);        // 添加到游戏层,方便管理    _gameLayer->addChild(b);        currentEndline = b;}#pragma mark - 初始化// 初始化bool WhiteSquareScene::init(){    // 父类的初始化    if ( !Layer::init() ) return false;    // 重要~~~~用当前 的时间戳 作为 随机数的种子    srand(time(NulL));    // 屏幕大小    winSize = Director::getInstance()->getVisibleSize();        // 用一个Node 包装起来 所有的东东,方便进行整体控制    _gameLayer = Node::create();    addChild(_gameLayer);        // 添加一个计时器标签    timerLabel = Label::create();    timerLabel->setTextcolor(color4B::BLUE);    timerLabel->setSystemFontSize(48);    timerLabel->setposition(winSize.wIDth/2,winSize.height-100);    addChild(timerLabel);        // 游戏初始化    initGame();    // 添加 监听当前 Layer的侦听器,就开始游戏    addLayertouchHandler();        return true;}#pragma mark - Layer触摸监听// 添加 监听当前 Layer的侦听器,就开始游戏voID WhiteSquareScene::addLayertouchHandler(){    // 单点触摸    auto Listener = EventListenertouchOneByOne::create();    Listener->ontouchBegan = [this](touch* t,Event* e)    {        auto squareArr = Square::getSquareArr();        Square *s;        // 遍历 所有的 方块 对象 数组,取出每一个方块        for (auto it = squareArr->begin(); it!=squareArr->end(); it++)        {            // 解引用 取出每一个方块            s = *it;            // 如果 方块的行号是1 表示是正常行中的第一行;            // 并且 正常行的第1行中的某个方块 被触摸了            // 并且 被点击的还是黑块;开始计时            if (s->getlineIndex()==1&&                s->getBoundingBox().containsPoint(t->getLocation()))            {                // 黑色                if (s->getcolor()==color3B::BLACK) {                    // 第1次点击黑块,且计时器没有开始,那么才要开始计时                    if (!isTimerRunning) {                        this->startTimer();                    }                    // 黑色被点击之后 变成灰色                    s->setcolor(color3B::GRAY);                    // 执行整体下移 动画                    this->gameLayerMoveDown();                                    }else if(s->getcolor()==color3B::GREEN){                    // 点击了绿色,即 endline                    // 整体下移 并且停止计时                    this->gameLayerMoveDown();                    this->stopTimer();                }else{                    // 变成红色 警示                    s->setcolor(color3B::RED);                    // 其他情况 不小心 点击到了 白块;d出消息框                    MessageBox("你点错了","点错了");                                        this->initGame();                }                // 重要~~~跳转循环,不用再遍历 SquareArr了                break;            }        }        return false;    };    // 向事件分发器注册侦听器,侦听整个Layer    Director::getInstance()->getEventdispatcher()->addEventListenerWithSceneGraPHPriority(Listener,this);}#pragma mark - 游戏控制// 游戏控制  开始游戏voID WhiteSquareScene::initGame(){    //init    stopTimer();    // 开始时 清空 用于记录 当前游戏行数的 变量    tempTotalline = 0;    // 是否 正在 显示 结束行    // 只有 没显示时 才要显示 (只显示一次)    isEndlineshowing = false;        isTimerRunning = false;    currentEndline = NulL;    timerLabel->setString("0.000000");        // 每次重新开始游戏,清空方块对象数组    Square::removeAllSquares();        // 添加 开始行    addStartline();    // 添加 正常的游戏行    addnormalline(1);    addnormalline(2);    addnormalline(3);}// 游戏层 整体下移voID WhiteSquareScene::gameLayerMoveDown(){    // 临时累积 当前 总共添加了多少行 (当到了50行时,添加结束行)    if (tempTotalline<50) {        // 添加 正常行的第4行;由于 起始行号是0 ;正常行号是从1开始;因此 4表示在屏幕最上方 外边        addnormalline(4);    }else if(!isEndlineshowing){        // 是否 正在 显示 结束行        // 只有 没显示时 才要显示 (只显示一次)        addEndline();        isEndlineshowing = true;    }        // 对所有的方块 进行遍历,让所有的方块 执行 下移 *** 作    auto squareArr = Square::getSquareArr();    // 使用迭代器 对 对象数组进行 遍历    for (auto it = squareArr->begin(); it!=squareArr->end(); it++) {        // 解引用  获得每一个方块;让方块自己执行 moveDown *** 作        (*it)->moveDown();    }        if (currentEndline!=NulL) {        // 最后一行的行号是1时,也要下移        if (currentEndline->getlineIndex()==1) {            // Game end            // 整体下移            gameLayerMoveDown();            // 停止计时器            stopTimer();        }    }}#pragma mark - 时钟方法// 计时控制voID WhiteSquareScene::update(float dt){    // 不断地获取时间,计算用时    long timeInterval = clock()-startTime;    // 设置计时器标签  微秒转成秒 6次方    std::string str = StringUtils::format("%g",((double)timeInterval)/1000000);    timerLabel->setString(str);}// 开始计时voID WhiteSquareScene::startTimer(){    if (!isTimerRunning) {        scheduleUpdate();        // 游戏开始时的时间戳        startTime = clock();        isTimerRunning = true;    }}// 停止计时voID WhiteSquareScene::stopTimer(){    if(isTimerRunning){        unscheduleUpdate();        isTimerRunning = false;    }}


方块
////  Square.h//  01_cocos2d-x////  Created by beyond on 14-10-7.////  方块#ifndef ___1_cocos2d_x__Square__#define ___1_cocos2d_x__Square__#include <cocos2d.h>USING_NS_CC;class Square:public Sprite {    private:    // 静态数组,每创建一个Square对象,就加到 数组中,static Vector<Square *> *squareArr;        int lineIndex;    public:    // 创建 参数:方块的颜色、尺寸、显示的文字、字体大小、字体颜色    static Square* createWithArgs(color3B color,Size size,std::string label,float FontSize,color4B textcolor);    // 初始化 参数:方块的颜色、尺寸、显示的文字、字体大小、字体颜色    virtual bool initWithArgs(color3B color,color4B textcolor);        // 获取对象数组    static Vector<Square *> *getSquareArr();    // 遍历对象数组,从数组中 最后一个对象 开始,从父容器中(Layer)移除;并且从数组中移除    static voID removeAllSquares();    // 移除 (父容器 及 数组中)    voID removeSquare();        // 每一个方块 自己记住自己是在 哪一个正常的行里,因为行号 决定 了Square的Y值    int getlineIndex();    // 每一个方块 自己记住自己是在 哪一个正常的行里,因为行号 决定 了Square的Y值    voID setlineIndex(int lineIndex);        // 让每一个方块 执行 向移一行的 *** 作    voID moveDown();};#endif /* defined(___1_cocos2d_x__Square__) */


////  Square.cpp//  01_cocos2d-x////  Created by beyond on 14-10-7.////#include "Square.h"#define kWinSize Director::getInstance()->getVisibleSize()#define kSquareWIDth kWinSize.wIDth/4#define kSquareHeight kWinSize.height/4// 静态数组,Vector<Square*> * Square::squareArr = new Vector<Square*>();#pragma mark - 生命周期方法// 参数:方块的颜色、尺寸、显示的文字、字体大小、字体颜色Square* Square::createWithArgs(color3B color,color4B textcolor){    auto b = new Square();    // 调用init方法 初始化    b->initWithArgs(color,size,label,FontSize,textcolor);    b->autorelease();    // 每创建一个Square对象,就加到 对象数组中    squareArr->pushBack(b);    return b;}//  参数:方块的颜色、尺寸、显示的文字、字体大小、字体颜色bool Square::initWithArgs(color3B color,color4B textcolor){    // 父类的init    Sprite::init();    //    lineIndex = 0;    // 尺寸    setContentSize(size);    // 锚点左下角    setAnchorPoint(Point::ZERO);    // 颜色区域    setTextureRect(Rect(0,size.wIDth,size.height));    setcolor(color);        // 方块内部 居中显示文字    auto l = Label::create();    l->setString(label);    l->setSystemFontSize(FontSize);    l->setTextcolor(textcolor);    addChild(l);    l->setposition(size.wIDth/2,size.height/2);        return true;}#pragma mark - 供外界调用// 对象数组的 getter方法Vector<Square*> * Square::getSquareArr(){    return Square::squareArr;}// 遍历对象数组,从父容器中(Layer)移除;并且从数组中移除voID Square::removeAllSquares(){    while (getSquareArr()->size()) {        // 遍历对象数组,从父容器中(Layer)移除;并且从数组中移除        getSquareArr()->back()->removeFromParent();        getSquareArr()->popBack();    }}// 移除 (父容器 及 数组中)voID Square::removeSquare(){    //    auto c = getcolor();    //    log("Remove Square,color is (%d,%d,%d)",c.r,c.g,c.b);        // 从父容器中 即 Layer 中移除    removeFromParent();    // 从对象数组 中移除    squareArr->eraSEObject(this);}// 每一个方块 自己记住自己是在 哪一个正常的行里,因为行号 决定 了Square的Y值voID Square::setlineIndex(int i){    this->lineIndex = i;}// 每一个方块 自己记住自己是在 哪一个正常的行里,因为行号 决定 了Square的Y值int Square::getlineIndex(){    return this->lineIndex;}// 让每一个方块 执行 向移一行的 *** 作voID Square::moveDown(){    // 既然方块 自己下移一行,那么 行号就要 减减    this->lineIndex--;            if (getNumberOfRunningActions()!=0) {        stopAllActions();    }    // 下移动作 移动到指定坐标 (也可以用MoveBy)    Moveto *to = Moveto::create(0.1f,Point(getpositionX(),lineIndex * kSquareHeight));    // 回调动作    CallFunc *func = CallFunc::create([this](){        // 如果 出屏幕了,直接 调用 移除方块 (内部会从 父容器和数组中都移除)        if (lineIndex<0) {            this->removeSquare();        }    });    // 序列动作    Sequence *s = Sequence::create(to,func,NulL);    // 执行动作    runAction(s);}


封装的结束行Endline
////  Endline.h//  01_cocos2d-x////  Created by beyond on 14-10-7.////#ifndef ___1_cocos2d_x__Endline__#define ___1_cocos2d_x__Endline__#include "Square.h"#include "WhiteSquareScene.h"// 声明 用到了游戏的主场景class WhiteSquareScene;// 结束行 继承自 Square 方块class Endline:public Square{    private:    Size _winSize;    WhiteSquareScene *_context;    public:    // 静态方法 创建时 需要 使用到主场景 WhiteSquareScene;内部调用init方法    static Endline* createWithContext(WhiteSquareScene *context);    // 在init方法中,实现真正的 初始化    bool initWithContext(WhiteSquareScene *context);};#endif /* defined(___1_cocos2d_x__Endline__) */


////  Endline.cpp//  01_cocos2d-x////  Created by beyond on 14-10-7.////#include "Endline.h"// 静态方法 创建时 需要 使用到主场景 WhiteSquareScene;内部调用init方法Endline* Endline::createWithContext(WhiteSquareScene *context){    auto el = new Endline();    // 在init方法中,实现真正的 初始化    el->initWithContext(context);    el->autorelease();    // 创建好 方块对象 就要加入到静态对象数组中    Square::getSquareArr()->pushBack(el);    return el;}// 在init方法中,实现真正的 初始化bool Endline::initWithContext(WhiteSquareScene *context){    this->_context = context;    _winSize = Director::getInstance()->getVisibleSize();    // 全屏 绿色    Square::initWithArgs(color3B::GREEN,_winSize,"游戏结束",50,color4B::BLACK);        auto label = Label::create();    label->setString("再玩一次");    label->setSystemFontSize(50);        label->setposition(_winSize.wIDth/2,label->getContentSize().height/2+50);    // 文字红色    label->setTextcolor(color4B::RED);    addChild(label);    // 点击 label 【再玩一次】,重新开始一盘游戏    auto Listener = EventListenertouchOneByOne::create();    Listener->ontouchBegan = [this](touch* t,Event * e){        // 点击了Label        if (e->getCurrentTarget()->getBoundingBox().containsPoint(t->getLocation()-e->getCurrentTarget()->getParent()->getposition())) {            // 开始游戏            this->_context->initGame();        }        return false;    };    // 注册监听器    Director::getInstance()->getEventdispatcher()->addEventListenerWithSceneGraPHPriority(Listener,label);    return true;}
总结

以上是内存溢出为你收集整理的cocos2d_x_07_游戏_别踩白块儿全部内容,希望文章能够帮你解决cocos2d_x_07_游戏_别踩白块儿所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/web/1001651.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存