cocos2dx實現經典飛機大戰

江軍祥發表於2016-02-25

遊戲開始層

#ifndef __LayerGameStart_H__
#define __LayerGameStart_H__
#include "cocos2d.h"
USING_NS_CC;


class LayerGameStart :public CCLayer
{
public:
    static CCScene * scene();
    CREATE_FUNC(LayerGameStart);
    bool init();

    void addStartGamePicture();
    void addPreLoadMusic();
    void toMainGameCallback();


};


#endif

遊戲主場景

#include "cocos2d.h"
#include "Plane.h"
#include "LayerGameStart.h"
#include "LayerBullet.h"
#include "LayerEnemy.h"
#include "LayerControl.h"
#include "LayerFood.h"
#include "LayerGameOver.h"
USING_NS_CC;

const int  SMALL_SCORE = 1000;
const int  MID_SCORE = 6000;
const int  BIG_SCORE = 30000;

const int MAX_BIGBOOM_COUNT = 100000;
const int TAG_BIGBOOM = 1000;
const int TAG_BIGBOOMCOUNT = 2000;
class LayerGameMain :public CCLayer
{
public:
    static CCScene * scene();
    CREATE_FUNC(LayerGameMain);
    bool init();
    //載入主場景背景
    void addBackGround();
    //背景動起來
    void movingBackGround(float dt);
    void addHero();
    void addBulletLayer();
    void addEnemyLayer();
    void addCtrlLayer();
    void addFoodLayer();

    void updateBigBoomCount(int bigBoomCount);

    void boomMenuCallBack(CCObject * obj);
    enum BACKGROUND
    {
        BACK1, BACK2
    };
    void update(float dt);
    virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
    virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);
    LayerBullet  * _bulletLayer;
    LayerEnemy   * _enemyLayer;
    LayerControl * _ctrlLayer;
    LayerFood    * _foodLayer;
    static int score;
    static void setScore(int num);
    int bigBoomCount;

};      

碰撞檢測

其中分為子彈與飛機的碰撞檢測,飛機與飛機的碰撞檢測,其中敵機分為大飛機,中飛機與小飛機,原理大致相同,這裡只展示小飛機的原理與方法。

CCARRAY_FOREACH(_bulletLayer->_bulletArray, bt)
    {
        CCSprite * bullet = (CCSprite*)bt;
        CCArray  * smallEnemyToDel = CCArray::create();

    CCARRAY_FOREACH(_enemyLayer->smallArray, et)
    {
        Enemy * smallEnemy = (Enemy *)et;
        if (bullet->boundingBox().intersectsRect(smallEnemy->getBoundingBox()))
        {
            CCLog("%d", smallEnemy->getLife());
            if (smallEnemy->getLife() == 1)
            {
                smallEnemy->loseLife();
                bulletsToDel->addObject(bullet);
                smallEnemyToDel->addObject(smallEnemy);
                score += SMALL_SCORE;
                _ctrlLayer->updataScore(score);
            }
        }
    }
    CCARRAY_FOREACH(smallEnemyToDel, et)
    {
        Enemy * smallEnemy = (Enemy*)et;
        _enemyLayer->smallEnemyBlowUp(smallEnemy);
    }
    smallEnemyToDel->release();
}

“`

飛機與飛機的碰撞檢測

//hero vs enemySmall
CCRect planeRect = Plane::getInstance()->boundingBox();
planeRect.origin.x += 30;
planeRect.origin.y += 20;
planeRect.size.width -= 60;
planeRect.size.height -= 40;

CCARRAY_FOREACH(_enemyLayer->smallArray,et)
{
    Enemy  * smallEnemy = (Enemy  *)et;
    if (planeRect.intersectsRect(smallEnemy->getBoundingBox()))
    {
        _bulletLayer->stopShoot();
        unscheduleAllSelectors();
        Plane::getInstance()->blowUp();
        Plane::getInstance()->removePlane();
        _enemyLayer->smallEnemyBlowUp(smallEnemy);

    }
}

飛機大戰的分層

由於遊戲的複雜性,需要將精靈分為好幾個層次加入到遊戲中,可以分為控制層,敵機層,食物層,子彈層等

控制層程式碼

    #ifndef __LayerControl_H__
    #define __LayerControl_H__

    #include "cocos2d.h"
    USING_NS_CC;

    class LayerControl : public CCLayer
    {
    public:
        CREATE_FUNC(LayerControl);
        bool init();

        void menuCallBack(CCObject *obj);
        void updataScore(int score);

    private:
        CCMenuItemSprite * pauseMenuItem;
        CCLabelBMFont * scoreItem;
    };


    #endif

食物層程式碼

#ifndef __LayerFood_H__
#define __LayerFood_H__

#include "cocos2d.h"
USING_NS_CC;
class LayerFood :public CCLayer
{
public:
    CREATE_FUNC(LayerFood);
    bool init();

    //void addMultiBullets(float dt);
    //void multiBulletsMoveFinished(CCNode * pSender);
    //void removeMultiBullets(CCSprite *mb);

    void addBigBoom(float dt);
    void bigBoomMoveFinished(CCNode *pSender);
    void removeBigBoom(CCSprite *bb);

public:
    CCArray * multiBulletArray;
    CCArray * bigBoomArray;

};

#endif

敵機層程式碼

#ifndef __LayerEnemy_H__
#define __LayerEnemy_H__

#include "cocos2d.h"
#include "Enemy.h"

USING_NS_CC;

const int SMALL_MAXLIFE = 1;
const int MID_MAXLIFE = 3;
const int BIG_MAXLIFE = 5;

class LayerEnemy: public CCLayer
{
public:
    CREATE_FUNC(LayerEnemy);
    bool init();


    void addSmallEnemy(float dt);
    void smallEnemyMovefinished(CCNode *node);
    void smallEnemyBlowUp(Enemy * smallEnemy);
    void removeSmallEnemy(CCNode * target, void * data);
    void removeAllSmallEnemy();

    void addMidEnemy(float dt);
    void midHitAnimate(Enemy *midEnemy);
    void midEnemyMovefinished(CCNode *node);
    void midEnemyBlowUp(Enemy * midEnemy);
    void removeMidEnemy(CCNode * target, void * data);
    void removeAllMidEnemy();

    void addBigEnemy(float dt);
    void bigHitAnimate(Enemy *midEnemy);
    void bigEnemyMovefinished(CCNode *node);
    void bigEnemyBlowUp(Enemy * bigEnemy);
    void removeBigEnemy(CCNode * target, void * data);
    void removeAllBigEnemy();

    void removeAllEnemy();


    CCArray * smallArray;
    CCArray *midArray;
    CCArray *bigArray;
};


#endif

子彈層程式碼

#ifndef __LayerBullet_H__
#define __LayerBullet_H__

#include "cocos2d.h"
USING_NS_CC;

class LayerBullet: public CCLayer
{
public:
    CREATE_FUNC(LayerBullet);
    bool init();

    void startShoot();
    void stopShoot();
    void addBulletCallback(float dt);
    void bulletMoveFinished(CCNode* node);
    void removeBullet(CCSprite* bullet);

    CCArray *_bulletArray;
    CCSpriteBatchNode * _bulletBatchNode;
};

#endif

執行結果

開始介面123

遊戲主介面
遊戲結束介面

相關文章