Cocos2d-x 3.0 My Flappy Bird

最近都要搞Unity,都快忘记C++怎么写了。

这几天刚好闲下来,花了几个小时用cocos2d-x 3.0 山寨了一个粗糙的FlappyBird。

//主层
#include "GameView.h"
#include "GameManager.h"
#include "Bird.h"
#include "WaterPipe.h"
USING_NS_CC;
CGameView::CGameView(void)
{
    _bird = NULL;
    _backSky = NULL;
    _isBackSkyReload = false;
    _backSkyRe = NULL;
    _fRandWPds = 70.0f;
}
CGameView::~CGameView(void)
{
}
bool CGameView::init()
{
    if(!Layer::create())
        return false;
    Size winSize = deGameManager->getVisibleSize();
    auto bg = Sprite::create("bg.png");
    bg->setAnchorPoint(Point(0, 0));
    bg->setPosition(Point(0, 0));
    this->addChild(bg, 0);
    _backSky = Sprite::create("bg2.png");
    _backSky->setAnchorPoint(Point(0, 0));
    _backSky->setPosition(Point(0, -_backSky->getContentSize().height / 2.0f));
    this->addChild(_backSky, 3);
    _backSkyRe = Sprite::create("bg2.png");
    _backSkyRe->setAnchorPoint(Point(0, 0));
    _backSkyRe->setPosition(Point(winSize.width, -_backSkyRe->getContentSize().height / 2.0f));
    this->addChild(_backSkyRe, 3);
        //小鸟类
    _bird = CBird::create();
    _bird->_limitDown = _backSky->getContentSize().height / 2.0f;
    _bird->setPosition(Point(70.0f, 200.0f));
    this->addChild(_bird, 2);
    _bird->fly();
    _scoreLabel = LabelTTF::create("", "Arial", 36);
    _scoreLabel->setPosition(Point(winSize.width / 2.0f, 480.0f));
    this->addChild(_scoreLabel, 4);
    this->setScore(0);
                         
    this->setTouchMode(Touch::DispatchMode::ONE_BY_ONE);
    this->setTouchEnabled(true);
    scheduleUpdate();
        //每1.3s生成一个水管类
    schedule(schedule_selector(CGameView:: createWaterPine), 1.3f);
                         
    return true;
}
bool CGameView::onTouchBegan(Touch *touch, Event *unused_event)
{
    Point point = touch->getLocation();
    _bird->mouseDown(point);
    return true;
}
void CGameView::onTouchMoved(cocos2d::Touch *touch, cocos2d::Event *unused_event)
{
}
void CGameView::onTouchEnded(Touch *touch, Event *unused_event)
{
}
bool CGameView::isInRect(Touch* touch)
{
//  Point point = touch->getLocation();
//  Point pos = _touchLayerColor->getPosition();
//  Rect rect = CCRectMake(pos.x, pos.y , _touchLayerColor->getContentSize().width, _touchLayerColor->getContentSize().height);
//  if(rect.containsPoint(point))
//  {
//      return true;
//  }
    return false;
}
void CGameView::menuCallBack(Object* obj)
{
    Node* node = (Node*)obj;
    switch (node->getTag())
    {
    case ePauese:
        {
        }
        break;;
    case eHint:
        {
        }
        break;
    default:
        break;
    }
                         
}
void CGameView::update(float delta)
{
    _backSky->setPositionX(_backSky->getPositionX() - 3);
    _backSkyRe->setPositionX(_backSkyRe->getPositionX() - 3);
    if(_backSky->getPositionX() <= -deGameManager->getVisibleSize().width)
        _backSky->setPositionX(deGameManager->getVisibleSize().width);
    if(_backSkyRe->getPositionX() <= -deGameManager->getVisibleSize().width)
        _backSkyRe->setPositionX(deGameManager->getVisibleSize().width);
}
// 这里就是视差背景了
void CGameView::createWaterPine(float delta)
{
    auto wp = CWaterPine::create();
    float fmoveWidth = deGameManager->getVisibleSize().width + wp->getWidth();
    CCLOG("width:%f", fmoveWidth);
    float fRand = deGameManager->myRand_MINUS1_1() * _fRandWPds;
    wp->setPosition(Point(fmoveWidth, deGameManager->getVisibleSize().height / 2 + fRand));
    wp->_bird = _bird;
    this->addChild(wp, 1);
}
void CGameView::setScore(int nScore)
{
    _scoreLabel->setString(String::createWithFormat("%d", nScore)->getCString());
}


小鸟类:

#include "Bird.h"
#include "GameManager.h"
USING_NS_CC;
CBird::CBird()
{
    _jumpJourney = 60.0f;
    _jumpTime = 0.3f;
    _limitDown = 115.0f;
    _downSpeed = 400.0f;
    _birdState = eBirdState::eBirdNull;
}
bool CBird::init()
{
    this->scheduleUpdate();
                    
    return initWithFile("bird1.png");
}
bool CBird::mouseDown(cocos2d::Point pos)
{
    jump();
    return true;
}
void CBird::jump()
{
    this->stopAllActions();
    _birdState = eBirdState::eBirdJump;
    auto moveJump = MoveBy::create(_jumpTime, Point(0, _jumpJourney));
    auto move = EaseOut::create(moveJump, 1.5f);
    auto cb = CallFuncN::create(CC_CALLBACK_1(CBird::jumpCB, this));
    Sequence* action = Sequence::create(move, cb, NULL);
    action->setTag((int)eBirdState::eBirdJump);
    this->runAction(action);
    this->fly();
}
void CBird::jumpCB(cocos2d::Node* sender)
{
    _birdState = eBirdState::eBirdNull;
}
void CBird::down()
{
    this->stopAllActions();
    _birdState = eBirdState::eBirdDown;
    float dt = this->getPositionY() / _downSpeed;
    auto moveDown = MoveBy::create(dt, Point(0.0f, -this->getPositionY() + _limitDown));
    auto move = EaseIn::create(moveDown, 3.0f);
    move->setTag((int)eBirdState::eBirdDown);
    /*auto cb = CallFuncN::create(CC_CALLBACK_1(CBird::jumpCB, this));*/
    this->runAction(move);
    this->fly();
}
void CBird::update(float delta)
{
    if(this->getPositionY() <= _limitDown)
    {
        deGameManager->gameOver();
        this->unscheduleUpdate();
    }
    if (eBirdState::eBirdNull == _birdState)
    {
        down();
    }
}
void CBird::fly()
{
    auto animation = Animation::create();
    for (int i = 1; i <= 3; i++)
    {
        char szName[260] = {0};
        sprintf_s(szName, "bird%d.png", i);
        animation->addSpriteFrameWithFileName(szName);
    }
    animation->setDelayPerUnit(0.3f / 3.0f);
    animation->setRestoreOriginalFrame(true);
    Animate* action = Animate::create(animation);
    action->setTag((int)eBirdState::eBirdFly);
    this->runAction(action);
}

水管类:

#include "WaterPipe.h"
#include "GameManager.h"
#include "Bird.h"
USING_NS_CC;
CWaterPine::CWaterPine()
{
    _fSpeed = 3.0f;
    _bird = NULL;
    _channelSize = 100.0f;
    _isCheckScore = false;
}
bool CWaterPine::init()
{
    _topCube = Sprite::create("tube1.png");
    _topCube->setAnchorPoint(Point::ZERO);
    _topCube->setPosition(Point(-_topCube->getContentSize().width / 2.0f, _channelSize / 2.0f));
    this->addChild(_topCube);
    _bottomCube = Sprite::create("tube2.png");
    _bottomCube->setAnchorPoint(Point::ZERO);
    _bottomCube->setPosition(Point(-_bottomCube->getContentSize().width / 2.0f, -_channelSize / 2.0f  -_bottomCube->getContentSize().height));
    this->addChild(_bottomCube);
    this->scheduleUpdate();
    return true;
}
void CWaterPine::update(float delta)
{
    if(deGameManager->_isGameOver)
        this->unscheduleUpdate();
    Rect rect1, rect2;
    rect1 = this->getCubeRect(_bottomCube);
    rect2 = this->getCubeRect(_topCube);
    if(this->isInRect(rect1) || this->isInRect(rect2))
    {
        deGameManager->gameOver();
    }
             
    if(this->getPositionX() < -getWidth())
        this->removeFromParent();
    else
        this->setPositionX(this->getPositionX() - _fSpeed);
    if(!_isCheckScore && _bird)
    {
        if (_bird->getPositionX() > this->getPositionX())
        {
            deGameManager->addScore();
            _isCheckScore = true;
        }
    }
}
bool CWaterPine::isInRect(Rect rect)
{
    Size birdSize = _bird->getContentSize();
    Rect birdRect = CCRectMake(_bird->getPositionX() -birdSize.width / 4.0, _bird->getPositionY() - birdSize.height / 4.0,  birdSize.width / 2.0, birdSize.height / 2.0);
    if(rect.intersectsRect(birdRect))
    {
        return true;
    }
    /*if(rect.containsPoint(_bird->getPosition()))
    return true;*/
    return false;
}
Rect CWaterPine::getCubeRect(Sprite* cube)
{
    Size size = cube->getContentSize();
    Rect rect = CCRectMake(this->getPositionX() + cube->getPositionX(), this->getPositionY() + cube->getPositionY(), size.width, size.height);
    return rect;
}


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。