cocos2dx 游戏 飞机大战

前端之家收集整理的这篇文章主要介绍了cocos2dx 游戏 飞机大战前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

本来学Android学的好好的,由于某些原因,现在暂时要做cocos游戏开发,学了五天,把做的一个demo分享一下:

能超过20000分的告诉我吧,我反正是到不了。。。运行有bug或者太卡就跟我说下吧,说了也不一定改。。(懒得折腾它了,继续做其它的。简单的规则:每存活一帧得一分,三种飞机不同血量分值,击杀敌机得分,漏杀扣分,敌人根据你的位置移动攻击,三种道具,加血量,加武器等级,导弹必杀技。血量100,挨子弹和撞敌机均扣15血。

上截图:



下载链接:http://beta.qq.com/m/q1q8



源码链接github仓库

当然,这里源码也只是把代码放下,毕竟一个工程文件500多m。。


主要的一些文件介绍:

  1. #pragma once
  2. #ifndef __PLAYER_H_
  3. #define __PLAYER_H_
  4.  
  5. #include "cocos2d.h"
  6. #include "Entrity.h"
  7. class BulletLayer;
  8.  
  9. #define JUMP_ACTION_TAG 1
  10. USING_NS_CC;
  11.  
  12. /*
  13.  
  14. 主角类
  15. 自控制生命值,子弹等级,必杀技等
  16. */
  17. class Player :public Entrity
  18. {
  19. public:
  20. Player();
  21. ~Player();
  22. CREATE_FUNC(Player);
  23. virtual bool init();
  24. int getHp();
  25. void hit();
  26. int getAttack();
  27. int getBullNum();
  28. void upBullet();
  29. void shootBoom(int x);
  30. void getHp(int x);
  31. void setBullet(BulletLayer *bullet);
  32. private:
  33. int mHp;
  34. int mAtt;
  35. int mBulnum;
  36. BulletLayer *mBullet;
  37. };
  38. #endif


  1. #pragma once
  2. #ifndef __OTHER_SCENE_H_
  3. #define __OTHER_SCENE_H_
  4. #include <cocos2d.h>
  5. #include "Player.h"
  6. #include "cocostudio/CocoStudio.h"
  7. #include "ui/CocosGUI.h"
  8. #include "Bullet.h"
  9. #include "RollerLayer.h"
  10. #include "ControllMoveLayer.h"
  11. #include "ColliderLayer.h"
  12. #include "GameLogic.h"
  13. #include "UiLayer.h"
  14. using namespace cocos2d::ui;
  15. /*
  16. 游戏运行场景
  17. */
  18. USING_NS_CC;
  19. class GameScene:public cocos2d::Layer
  20. {
  21. public:
  22. static cocos2d::Scene* createScene();
  23. virtual bool init();
  24. CREATE_FUNC(GameScene);
  25. virtual void update(float dt);
  26. private :
  27. void initBackground();
  28. void initPlayer();
  29. void initUi();
  30. void initGameLogic();
  31. void initController();
  32. void initCollider();
  33. private:
  34. Player*mPlayer;
  35. RollerLayer *mBackGround;
  36. UiLayer *mUiLayer;
  37. GameLogic *mGameLogic;
  38. ControllMoveLayer *mControllerLayer;
  39. ColliderLayer *mColliderLayer;
  40.  
  41. };
  42.  
  43.  
  44. #endif


  1. #pragma once
  2.  
  3. #ifndef __GAMEOVER_H_
  4. #define __GAMEOVER_H_
  5. #include"cocos2d.h"
  6. #include "cocostudio/CocoStudio.h"
  7. #include "ui/CocosGUI.h"
  8. USING_NS_CC;
  9. using namespace cocos2d;
  10. /*
  11.  
  12. 游戏失败场景
  13. */
  14. class GameOver :public cocos2d:: Layer
  15. {
  16. public:
  17.  
  18. static cocos2d::Scene* createScene(int sc);
  19. virtual bool init();
  20.  
  21. CREATE_FUNC(GameOver);
  22. void setscore(int score);
  23. void onMenu(Ref *ref);
  24. void update(float dt);
  25. cocos2d::ui::Text *mscore;
  26. private :
  27. int sscore;
  28. };
  29. #endif
  1. #pragma once
  2. #ifndef __UI_LAYER__H__
  3. #define __UI_LAYER__H__
  4. #include "cocostudio/CocoStudio.h"
  5. #include "ui/CocosGUI.h"
  6. #include "cocos2d.h"
  7. USING_NS_CC;
  8. using namespace cocos2d::ui;
  9. /*
  10. ui层,用于显示ui部分
  11. */
  12. class UiLayer :public Layer
  13. {
  14. public :
  15. CREATE_FUNC(UiLayer);
  16. void setHpPercent(float per);
  17. void setscore(int score);
  18. private:
  19. bool init();
  20. LoadingBar *mHpBar;
  21. Text *mscoreText;
  22. };
  23.  
  24. #endif


  1. #pragma once
  2. #ifndef _ROLLER_LAYER_H_
  3. #define _ROLLER_LAYER_H_
  4. #include "cocos2d.h"
  5. USING_NS_CC;
  6.  
  7. /*
  8.  
  9. 背景滚动层,通过两张背景图片的滚动,实现无线循环的背景
  10. */
  11. class RollerLayer :public Layer
  12. {
  13.  
  14. public:
  15. //滚动模式,水平,垂直
  16. static const int VERTICAL = 1;
  17. static const int HORIZONTAL = 2;
  18. CREATE_FUNC(RollerLayer);
  19. virtual bool init();
  20. void update(float dt);
  21. //设置好用于滚动的两张背景图片
  22. void setBackgrounds(Sprite *map1,Sprite*map2);
  23. //滚动方向
  24. void setRollOrientaion(int orientation);
  25. //滚动速度
  26. void setSpeed(int iSpeed);
  27. //返回背景大小
  28. Size getMapSize();
  29. private:
  30. //水平滚动
  31. void rollHorizontal();
  32. //垂直滚动
  33. void rollVertical();
  34. protected :
  35. Sprite *mMap1;
  36. Sprite *mMap2;
  37. int mOrientation;
  38. int mSpeed;
  39. };
  40.  
  41. #endif

  1. #pragma once
  2. #ifndef __CONTROLL_MOVE_H_
  3. #define __CONTROLL_MOVE_H_
  4.  
  5. #include "cocos2d.h"
  6. USING_NS_CC;
  7.  
  8. /*
  9.  
  10. 游戏角色的移动控制层
  11. */
  12. class ControllMoveLayer :public Layer
  13. {
  14. public :
  15. CREATE_FUNC(ControllMoveLayer);
  16. bool init();
  17. void setPlayer(Node *player);
  18. private:
  19. void update(float dt);
  20. bool onTouchBegan(Touch *touch,Event *unused_event);
  21. void onTouchCancelled(Touch *touch,Event *unused_event);
  22. void onTouchMoved(Touch *touch,Event *unused_event);
  23. void onTouchEnded(Touch *touch,Event *unused_event);
  24. float mDeltaX;
  25. float mDeltaY;
  26. Node *mPlayer;
  27. bool isControlled;
  28. };
  29. #endif

  1. #pragma once
  2. #ifndef ___GAME_LAYER_H__
  3. #define ___GAME_LAYER_H__
  4.  
  5. #include "cocos2d.h"
  6. #include "Player.h"
  7. #include "Exp.h"
  8. USING_NS_CC;
  9. /*
  10.  
  11. 游戏逻辑层
  12. */
  13. class GameLogic :public Layer
  14. {
  15.  
  16. public:
  17. CREATE_FUNC(GameLogic);
  18. bool init();
  19. //游戏分值的修改
  20. void modifyscore(int x);
  21. //当前得分
  22. int getscore();
  23. //当前等级
  24. int getLevel();
  25. //根据道具,进行相关计算
  26. void getExp(ToolExp exp);
  27.  
  28. void getMonster(int x);
  29. //设置好游戏逻辑中需要用到了角色
  30. void setPlayer(Player *player);
  31. void update(float dt);
  32. private :
  33. Player *mPlayer;
  34. int mscore=0;
  35. int mLevel=1;
  36. };
  37. #endif
  1. #pragma once
  2. #ifndef __EXP_H_H_
  3. #define __EXP_H_H_
  4. //枚举道具类型
  5. enum ToolExp
  6. {
  7. BULLET,//必杀技
  8. HP,//恢复血量
  9. STAR,//增强角色的子弹
  10. };
  11. #endif
  1. #pragma once
  2. #ifndef __ITEM_H____
  3. #define __ITEM_H____
  4.  
  5. #include "Entrity.h"
  6. class ItemFactor;
  7. class Item :public Entrity
  8. {
  9.  
  10. public:
  11. ~Item();
  12. CREATE_FUNC(Item);
  13. virtual bool init();
  14. //针对角色,进行动作
  15. virtual void action(Node *player);
  16. //自己判断与角色是否产生了碰撞
  17. virtual bool colliderWithPlayer(Node *player);
  18. //死亡
  19. virtual void death(ItemFactor *factor);
  20. //产生爆炸效果,并返回这个效果需要的时间
  21. virtual float boom();
  22. //是否存活
  23. bool isAlive();
  24. private :
  25. bool mAlive;
  26. //死亡
  27. void onDeath(float dt);
  28. };
  29. #endif

  1. #pragma once
  2. #ifndef __ITEM_FACTOR__H__
  3. #define __ITEM_FACTOR__H__
  4.  
  5. /*
  6.  
  7. 产生物品的工厂
  8. */
  9. #include "Item.h"
  10. class ItemFactor :public Node
  11. {
  12. public:
  13. ~ItemFactor();
  14. CREATE_FUNC(ItemFactor);
  15. bool init();
  16. //移除所有工厂产生的物品
  17. void removeAllItem();
  18. //移除指定物品
  19. void removeItem(Item *item);
  20. //产生一个新的物品
  21. virtual void createItem(float dt);
  22. //设置角色
  23. void setPlayer(Node *player);
  24. //设置产生物品的速度
  25. void setCreateSpeed(float iSpeed);
  26. //返回当前工厂已将创建的所有物品
  27. const Vector<Item*>& getItems();
  28. //删除所有死亡缓冲队列中已经死亡的物品
  29. void deleteCache();
  30. //把一个物品加入到死亡缓冲队列中
  31. void addToDeleteQueue(Item *item);
  32. protected:
  33. Node *mPlayer;
  34. Vector<Item*>mItemsVec;
  35. Vector<Item*>mDeleItemCache;
  36. float mCreateSpeed;
  37. };
  38. #endif

  1. #pragma once
  2. #include "Item.h"
  3.  
  4.  
  5. /*
  6.  
  7. 普通怪物,有怪物的普遍特性,血量,分值等
  8. */
  9. class MonsterItem :public Item
  10. {
  11. public:
  12. CREATE_FUNC(MonsterItem);
  13. bool init();
  14. void action(Node *player);
  15. int getscore();
  16. void hit(int att);
  17. int getHp();
  18. void set(int hp,int mscore);
  19. protected:
  20. int mHp;
  21. int mscore;
  22. };


  1. #pragma once
  2. #pragma once
  3. #ifndef __ATTACK_MONSTER__H_
  4.  
  5. #define __ATTACK_MONSTER__H_
  6.  
  7. #include "MonsterItem.h"
  8. /*
  9. 具有攻击能力的怪物类
  10. */
  11. class AttackMonster :public MonsterItem
  12. {
  13. public:
  14. ~AttackMonster();
  15. CREATE_FUNC(AttackMonster);
  16. bool init();
  17. //移除自身的所有子弹
  18. void removeAllObject();
  19. //移除某一个子弹
  20. void removeBullet(Node *node);
  21. //针对角色进行动作,主要是移动,攻击
  22. void action(Node *player);
  23. void setLevel(int lev);
  24. //根据主角移动
  25. virtual void move(Node *player);
  26. //根据主角攻击
  27. virtual void attack(Node *player);
  28. protected:
  29. int mLevlel;
  30. Vector<Sprite*>mBullets;
  31. };
  32. #endif


  1. #pragma once
  2. #ifndef __MONSTER__BOSS_H_
  3. #define __MONSTER__BOSS_H_
  4.  
  5. #include "AttackMonster.h"
  6.  
  7.  
  8. /*
  9.  
  10. 游戏boss类型
  11. */
  12. class MonsterBoss :public AttackMonster
  13. {
  14. public:
  15. CREATE_FUNC(MonsterBoss);
  16. bool init();
  17. virtual void move(Node *player);
  18. virtual void attack(Node *player);
  19. };
  20.  
  21.  
  22. #endif

  1. #pragma once
  2. #ifndef __MONSTER__FACTOR__H_
  3. #define __MONSTER__FACTOR__H_
  4. #include "ItemFactor.h"
  5.  
  6. /*
  7. 怪物产生工厂。根据当前等级,产生相应数量的怪物,根据随机数,产生相应类型的怪物
  8. */
  9. class MonsterFactor :public ItemFactor
  10. {
  11. public :
  12. CREATE_FUNC(MonsterFactor);
  13. bool init();
  14. virtual void createItem(float dt);
  15. void upLevel();
  16. //根据当前分数,更改当前怪物生产上限
  17. void chooseLevel(int score);
  18. private :
  19. int level;
  20. //一个标记位,防止第一个出现的怪物就是boss
  21. bool mHasBoss;
  22. };
  23. #endif
  1. #pragma once
  2. #ifndef __TOOL__TEM_H_
  3. #define __TOOL__TEM_H_
  4. #include "Item.h"
  5. #include "Exp.h"
  6.  
  7. /*
  8.  
  9. 道具类,有道具的类型等参数。
  10. */
  11. class ToolItem :public Item
  12. {
  13. public:
  14. CREATE_FUNC(ToolItem);
  15. ToolExp getExp();
  16. void action(Node *player);
  17. void setExp(ToolExp exp);
  18. float boom();
  19. private:
  20. ToolExp mExp;
  21. };
  22.  
  23.  
  24.  
  25. #endif

  1. #pragma once
  2. #ifndef __TOOL_FACTOR__H__
  3. #define __TOOL_FACTOR__H__
  4. #include "ItemFactor.h"
  5.  
  6.  
  7. /*
  8.  
  9. 道具产生工厂
  10. */
  11. class ToolFactor :public ItemFactor
  12. {
  13. public:
  14. CREATE_FUNC(ToolFactor);
  15.  
  16. bool init();
  17. virtual void createItem(float dt);
  18. };
  19.  
  20. #endif


  1. #pragma once
  2. #ifndef __BULLET_LAYER_H_
  3. #define __BULLET_LAYER_H_
  4. #include "cocos2d.h"
  5. #include "BulletBase.h"
  6. USING_NS_CC;
  7. /*
  8.  
  9. 子弹控制层,主要用于控制子弹等级
  10. */
  11. class BulletLayer :public Layer
  12. {
  13. public:
  14. BulletLayer();
  15. ~BulletLayer();
  16. CREATE_FUNC(BulletLayer);
  17. bool init();
  18. void bindPlayer(Player*player);
  19. Vector<Sprite*>&getBullets();
  20. //更改子弹等级
  21. void changeBullet(int bulletNumber);
  22. //移除子弹
  23. void removeBullet(Node *bullet);
  24. //发射必杀技
  25. void shootBoom(int x);
  26. //子弹升级
  27. void upBullet();
  28. //重置子弹
  29. void resetBullet();
  30. void update(float delta);
  31. private :
  32. Player *mPlayer;
  33. BulletBase *mBullet;
  34. int bulletNumber;
  35. };
  36.  
  37.  
  38. #endif

  1. #ifndef __BULLET_BASE_H_
  2. #define __BULLET_BASE_H_
  3. #include "cocos2d.h"
  4. #include "Player.h"
  5. USING_NS_CC;
  6. /*
  7.  
  8. 子弹发射基类
  9. */
  10. class BulletBase :public cocos2d::Node
  11. {
  12. public:
  13. ~BulletBase();
  14. CREATE_FUNC(BulletBase);
  15. //移除产生的所有子弹
  16. void removeAllObject();
  17. //移除指定子弹
  18. void removeBullet(Node *node);
  19. //根绝角色位置产生子弹
  20. virtual void createBullet(Node *plane);
  21. //子弹发射
  22. virtual void shootBullet(float dt);
  23. //返回产生的所有子弹
  24. virtual Vector<Sprite*> &getBullets();
  25. //更改子弹等级
  26. void changeBullet(int num);
  27. //发射必杀技
  28. virtual void shootBoom(int x);
  29. protected:
  30. Vector<Sprite*>mBullets;
  31. SpriteBatchNode *mBatchNode;
  32. Node *mPlayer;
  33. int mBulletNum = 1;
  34. };
  35.  
  36. #endif
  1. #pragma once
  2. #ifndef __BULLET_ONE_H_
  3. #define __BULLET_ONE_H_
  4. #include "cocos2d.h"
  5. #include "Player.h"
  6. #include "BulletBase.h"
  7. USING_NS_CC;
  8. class BulletOne :public BulletBase
  9. {
  10. public:
  11.  
  12. CREATE_FUNC(BulletOne);
  13. virtual void shootBullet(float dt);
  14. virtual void shootBoom(int x);
  15. };
  16.  
  17.  
  18.  
  19. #endif

  1. #pragma once
  2. #ifndef __FACTOR_LAYER_H__
  3. #define __FACTOR_LAYER_H__
  4. #include "cocos2d.h"
  5. #include "Player.h"
  6. #include "ItemFactor.h"
  7. #include "MonsterFactor.h"
  8. #include "ToolFactor.h"
  9. #include "Bullet.h"
  10. #include "GameLogic.h"
  11. USING_NS_CC;
  12. /*
  13.  
  14. 游戏碰撞层,主要是控制角色与道具的碰撞检测,角色与怪物,与敌方子弹的碰撞检测,己方子弹与敌方的碰撞
  15. */
  16. class ColliderLayer :public Layer
  17. {
  18. public:
  19. CREATE_FUNC(ColliderLayer);
  20. bool init();
  21. void setPlayer(Player *player);
  22. const Vector<Sprite*>&getBullets();
  23. //游戏逻辑层,因为这里检测碰撞后,需要逻辑层计算得分,物品等
  24. void setGameLogic(GameLogic *gameLogic);
  25. private:
  26. //每一帧都要进行碰撞检测
  27. virtual void update(float dt);
  28. //检测怪物的碰撞
  29. void colliderMonster();
  30. //检测游戏道具的碰撞
  31. void colliderTool();
  32. //怪物生产工厂,负责怪物的产生位置,类型,时间
  33. MonsterFactor *mMonseterFactor;
  34. //道具生产工厂,负责道具的产生位置,类型,时间
  35. ToolFactor *toolFactor;
  36. //持有一个角色,进行相关计算
  37. Player *mPlayer;
  38. //持有角色的子弹层,因为需要统一在碰撞层进行碰撞检测
  39. BulletLayer *mBullerLayer;
  40. //游戏逻辑层,进行相关游戏逻辑计算。
  41. GameLogic *mGameLogic;
  42. };
  43. #endif

猜你在找的Cocos2d-x相关文章