cocos2d_x_07_遊戲_別踩白塊兒,cocos2d_x_07塊兒

來源:互聯網
上載者:User

cocos2d_x_07_遊戲_別踩白塊兒,cocos2d_x_07塊兒
最終:
環境版本: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),"",20,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);                    // 其他情況 不小心 點擊到了 白塊;彈出訊息框                    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,Size size,std::string label,float fontSize,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// 靜態數組,每建立一個Square對象,就加到 數組中,Vector<Square*> * Square::squareArr = new Vector<Square*>();#pragma mark - 生命週期方法// 參數:方塊的顏色、尺寸、顯示的文字、字型大小、字型顏色Square* Square::createWithArgs(Color3B color,Size size,std::string label,float fontSize,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,Size size,std::string label,float fontSize,Color4B textColor){    // 父類的init    Sprite::init();    //    lineIndex = 0;    // 尺寸    setContentSize(size);    // 錨點左下角    setAnchorPoint(Point::ZERO);    // 顏色地區    setTextureRect(Rect(0, 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;}






蘋果遊戲'類似於別踩白塊兒的遊戲考驗頭腦與手指速度的遊戲有沒有?

flappy bird
 
遊戲別踩白塊兒經典8秒算快沒

7.467的飄過
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.