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
執行結果
相關文章
- Java實現飛機大戰遊戲Java遊戲
- Python飛機大戰Python
- Java小遊戲——飛機大戰Java遊戲
- 飛機大戰小程式(1)
- html+js 實現 推箱子 貪吃蛇和簡單的飛機大戰HTMLJS
- 飛機大戰和javaweb商城專案JavaWeb
- Android 專案之飛機大戰Android
- 網際網路史上10大經典商戰
- 基於Flutter Canvas的飛機大戰(一)FlutterCanvas
- 基於Flutter Canvas的飛機大戰(二)FlutterCanvas
- canvas繪製“飛機大戰”小遊戲,真香!Canvas遊戲
- Unity飛機大戰 原始碼 分享 學習Unity原始碼
- 實現累加的經典sql方法SQL
- 用飛槳做命名實體識別,手把手教你實現經典模型 BiGRU + CRF模型CRF
- tkinter飛機大戰測試程式by李興球
- 微信飛機大戰小遊戲編寫分享(上)遊戲
- JavaScript實現經典排序演算法JavaScript排序演算法
- 經典排序演算法PHP實現排序演算法PHP
- python 入門實戰改進B站小甲魚飛機大戰增強版4.0Python
- 微信demo小遊戲:飛機大戰從無到有遊戲
- python及pygame雷霆戰機遊戲專案實戰01 控制飛機PythonGAM遊戲
- 用 PHP 實現經典排序演算法PHP排序演算法
- 經典排序演算法及其 Java 實現排序演算法Java
- 十大經典排序演算法動畫解析和 Java 程式碼實現排序演算法動畫Java
- 視訊教程-Python機器學習經典案例實戰-PythonPython機器學習
- Mac經典戰略策略遊戲:Mac遊戲
- 神經機器翻譯實戰
- PHP 三大經典模式初探PHP模式
- Swift實現八種經典排序演算法Swift排序演算法
- 23個經典設計模式的Swift實現設計模式Swift
- 經典n皇后問題java程式碼實現Java
- 微信5.0 Android版飛機大戰破解無敵模式手記Android模式
- java飛機大戰小遊戲作業二次開發Java遊戲
- [原始碼和文件分享]基於VC++的MFC類庫的飛機大戰遊戲的設計與實現原始碼C++遊戲
- cocos2dx原始碼:popup實現原始碼
- 安卓入門---安卓開發實戰經典1-3章安卓
- 前端時光機(經典技能)前端
- 計算機經典論文計算機