Cocos2d-x简单游戏代码实现|第一部分:鱼类

时间:2023-02-08 10:46:04


这个简单的捕鱼游戏Demo只是完成了简单的:

 1.场景切换、数据加载、武器等级更换<大炮的升级>

 2.鱼的随机游动、大炮发射子弹、撒网、捕鱼

 3.子弹、鱼、网的碰撞检测等;

 4.场景及背景音乐的定时更换,碰撞时得音效;

 

  仅供参考、入门练习用例;

 

 第一部分:Cocos2d-x简单游戏<捕鱼达人>代码实现|第一部分:鱼类

 第二部分:Cocos2d-x简单游戏<捕鱼达人>代码实现|第二部分:子弹、渔网、大炮类

 第三部分:Cocos2d-x简单游戏<捕鱼达人>代码实现|第三部分:菜单类

 第四部分:Cocos2d-x简单游戏<捕鱼达人>代码实现|第四部分:加载场景类

 第五部分:Cocos2d-x简单游戏<捕鱼达人>代码实现|第五部分:游戏类



#ifndef __shows__Fish__

#define __shows__Fish__


#include <iostream>

#include "Header.h"

class Fish:publicCCSprite{

    

public:

   bool isCatched;//是否被抓

   CCSpeed *path;//速度

   int fishLevel;//鱼的类别

   CREATE_FUNC(Fish);


public:

   void removeFish(CCSprite *sprites);

   void addPath();

   void run();//

    //使用贝塞尔曲线创建鱼游动的路径

   void moveWithBezier(CCNode *mySprite,CCPoint startPoint,CCPoint endPoint,CCPoint controlPoint,float startAngle,float endAngle,float dirTime);

   bool randomCatch(int bowLevel);


};

#endif /* defined(__shows__Fish__) */




#include "Fish.h"

//命中率--打中精灵的概率

constint odds[][2] = {

    {20,30},

    {20,40},

    {20,50},

    {30,70},

    {40,100},

    {50,150},

    {60,200},

    {50,300},

    {50,300},

    {60,300},

    {70,300},

    {80,300}

};

int commonNum =2.5;

int commonTarget =1.8;

constint pathArray[][8]={

    //   0-x    1-y  2-x  3-y  4-x  5-y  6   7

    {-200*commonNum,100*commonNum,240*commonNum,320*commonNum,560*commonNum,240*commonNum,150*commonTarget,190*commonTarget},//0左到右(偏上)--·

    {-200*commonNum,-100*commonNum,240*commonNum,320*commonNum,560*commonNum,120*commonNum,125*commonTarget,200*commonTarget},//1左到右(偏下)

    {-100*commonNum, -50*commonNum,240*commonNum,320*commonNum,560*commonNum,-100*commonNum,110*commonTarget,240*commonTarget},//2左下到右下

    {-100*commonNum,330*commonNum, -20*commonNum,-100*commonNum,550*commonNum,380*commonNum,270*commonTarget,130*commonTarget},//3左上到右上

    {  50*commonNum,-100*commonNum30*commonNum,350*commonNum,500*commonNum,350*commonNum70*commonTarget,180*commonTarget},//4左下到右上

    { 600*commonNum,100*commonNum,300*commonNum,100*commonNum,-100*commonNum,300*commonNum, -20*commonTarget40*commonTarget},//5右到左(偏上)

    { 550*commonNum,300*commonNum,300*commonNum, -50*commonNum,-150*commonNum,160*commonNum, -60*commonTarget25*commonTarget},//6右上到左

    { 600*commonNum,240*commonNum, -20*commonNum,350*commonNum,-150*commonNum,-100*commonNum10*commonTarget, -30*commonTarget},//7右到左偏下

    { 550*commonNum,-100*commonNum,450*commonNum,350*commonNum,-100*commonNum,350*commonNum70*commonTarget20*commonTarget},//8右下到左上

    { 400*commonNum,400*commonNum,150*commonNum,420*commonNum,100*commonNum,-100*commonNum, -20*commonTarget, -80*commonTarget},//9上到下偏左1

    { 300*commonNum,400*commonNum,600*commonNum,100*commonNum50*commonNum,-100*commonNum,-130*commonTarget, -35*commonTarget},//10上到下偏右2

    {  50*commonNum,400*commonNum,600*commonNum,150*commonNum,250*commonNum,-100*commonNum,-160*commonTarget, -60*commonTarget},//11上到下偏右1

    { 300*commonNum,550*commonNum,-100*commonNum,100*commonNum,100*commonNum,-100*commonNum, -50*commonTarget,-105*commonTarget},//12上到下偏左2

    {  25*commonNum,-100*commonNum,350*commonNum,200*commonNum,100*commonNum,400*commonNum,150*commonTarget60*commonTarget},//13下到上

    { 200*commonNum,-100*commonNum,-200*commonNum,240*commonNum,350*commonNum,400*commonNum10*commonTarget,160*commonTarget},//14下到上

    { 400*commonNum,-100*commonNum,500*commonNum,200*commonNum,200*commonNum,400*commonNum,120*commonTarget40*commonTarget},//15下到上

    { 450*commonNum,-100*commonNum,-100*commonNum,200*commonNum,260*commonNum,400*commonNum,  0*commonTarget,110*commonTarget},//16下到上

};

#define TOTALPATH 17

#define SPRITE_OFFSET 0

//const int moveTime = 15;

constint moveTime =30;

//随机

boolFish::randomCatch(int bowLevel){


//    CCLog("%d",odds[fishLevel][0]);

    //根据大炮的等级获取打中精灵的命中率

if (rand()%odds[fishLevel][1] < (odds[fishLevel][0] * (1 +0.1*bowLevel)) ){

       returntrue;

}

else {

returnfalse;

}

    

}

voidFish::addPath(){


   float time =rand()%20+18;//随机时间

   float startAngle,endAngle;//旋转

    int i =rand()%TOTALPATH;//鱼游动的路径数组的第一维

    startPoint =ccp(pathArray[i][0],pathArray[i][1]);

    controlPoint =ccp(pathArray[i][2],pathArray[i][3] );

    endPoint =ccp(pathArray[i][4],pathArray[i][5]);

    startAngle =pathArray[i][6] -SPRITE_OFFSET;

    endAngle =pathArray[i][7] -SPRITE_OFFSET;


   this->moveWithBezier(this, startPoint, endPoint, controlPoint, startAngle, endAngle,time);

}

voidFish::moveWithBezier(CCNode *mySprite,CCPoint startPoint,CCPoint endPoint,CCPoint controlPoint,float startAngle,float endAngle,float dirTime){


   int xChange =rand()%450 -50;

   int yChange =rand()%300 -50;

   float sx = startPoint.x + xChange;//起点x

   float sy = startPoint.y + yChange;//终点y

   CCPoint sp =CCPointMake(sx, sy);//cpp(sx,sy);


   int ex = endPoint.x +rand()%1000 -40;

   int ey = endPoint.y +rand()%650 -40;

   CCPoint ep =CCPointMake(ex, ey);//cpp(ex,ey);

   int cx = controlPoint.x + xChange;

   int cy = controlPoint.y + yChange;

   CCPoint cp =CCPointMake(cx, cy);

    mySprite->setPosition(sp);//startPoint;

    mySprite->setRotation(startAngle);

    //动作设置---使用贝塞尔曲线

    ccBezierConfig bezier;

    bezier.controlPoint_1 = sp;

    bezier.controlPoint_2 = cp;

    bezier.endPosition = ep;

   CCBezierTo * actionMove =CCBezierTo::create(dirTime, bezier);

   CCRotateTo * actionRotate =CCRotateTo::create(dirTime, endAngle);

   CCActionInterval * action =CCSpawn::create(actionMove,actionRotate,NULL);

   CCCallFunc *func =CCCallFunc::create(this,callfunc_selector(Fish::removeFish));

   CCSequence * actionSequence =CCSequence::create(action,func,NULL);

   path =CCSpeed::create(actionSequence,1.85f);//鱼游动的速度


}

voidFish::run(){

   this->runAction(path);

}


voidFish::removeFish(CCSprite *sprite){

    this->removeFromParentAndCleanup(true);

}