cocos2dx实现经典飞机大战

游戏开始层

#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

游戏主界面
游戏结束界面

转载于:https://www.cnblogs.com/jjx2013/p/6223765.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值