cocos2dx实现象棋之运动

前端之家收集整理的这篇文章主要介绍了cocos2dx实现象棋之运动前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1、头文件

void moveStone(int moveid,int killid,int x,int y);
    void moveComplete(CCNode*,void*);

    bool canMove(int moveid,int y);
    bool canMoveJiang(int moveid,int y);
    bool canMoveShi(int moveid,int y);
    bool canMoveXiang(int moveid,int y);
    bool canMoveChe(int moveid,int y);
    bool canMoveMa(int moveid,int y);
    bool canMovePao(int moveid,int y);
    bool canMoveBing(int moveid,int y);

    int getStoneCount(int xo,int yo,int y);

要实现象棋的运动,首先要确定选中的目标,再其次确定选中的位置,再确定选择的位置是否有对方棋子

void SceneGame::setSelectId(int id)
{
    if(id==-1)
    {
        return;
    }
    if(_s[id]->getRed()!=_redTrun)
        return;

    _selectid=id;

    _selectSprite->setVisible(true);
    _selectSprite->setPosition(_s[_selectid]->getPosition());
}

void SceneGame::moveStone(int moveid,int y)
{
if(killid != -1&&_s[moveid]->getRed()==_s[killid]->getRed())
{
setSelectId(killid);
return;
}

bool bCanMove = canMove(moveid,killid,x,y);
    if(bCanMove == false)
        return;
    //set step
    Step* step = Step::create(moveid,_s[moveid]->getX(),_s[moveid]->getY(),y);
    _steps->addObject(step);


    _s[moveid]->setX(x);
    _s[moveid]->setY(y);
    //_s[moveid]->setPosition(getStonePos(x,y));
    CCMoveTo *moveto=CCMoveTo::create(.5f,getStonePos(x,y));
    CCCallFuncND *call=CCCallFuncND::create(this,callfuncND_selector(SceneGame::moveComplete),(void*)(intptr_t)killid);

    CCSequence *sequence=CCSequence::create(moveto,call,NULL);
    _s[moveid]->setZOrder(_s[moveid]->getZOrder()+1);
    _s[moveid]->runAction(sequence);

    //_selectid=-1;
    //_selectSprite->setVisible(false);
    //_redTrun=!_redTrun;
}

不同的棋子运动规则不同,车,马,炮等都有不同的运动方式

函数
bool SceneGame::canMove(int moveid,int y)
{
Stone *s=_s[moveid];
switch(s->getType())
{
case Stone::JIANG:
return canMoveJiang(moveid,y);

case Stone::SHI:
        return canMoveShi(moveid,y);

    case Stone::XIANG:
        return canMoveXiang(moveid,y);

    case Stone::CHE:
        return canMoveChe(moveid,y);

    case Stone::MA:
        return canMoveMa(moveid,y);

    case Stone::PAO:
        return canMovePao(moveid,y);

    case Stone::BING:
        return canMoveBing(moveid,y);
    }

    return false;
}

将的移动
bool SceneGame::canMoveJiang(int moveid,int y)
{

if(killid!=-1)
    {
    Stone* skill = _s[killid];
    if(skill->getType() == Stone::JIANG)
    {
        return canMoveChe(moveid,y);
    }
    }
    //can only walk one step and can not walk out the lattic also can kill the king
    Stone *s=_s[moveid];
    int xo=s->getX();
    int yo=s->getY();
    int xoff=abs(xo-x);
    int yoff=abs(yo-y);
    int d = xoff*10 + yoff;
    if(d != 1 && d != 10) return false;
    if(x<3||x>5) return false;
    if(_redSide==s->getRed())
    {
        if(y<0||y>2) return false;
    }else
    {
        if(y < 7 || y > 9) return false;
    }

    return true;
}

士的移动
bool SceneGame::canMoveShi(int moveid,int y)
{
Stone *s=_s[moveid];
int xo=s->getX();
int yo=s->getY();
int xoff=abs(xo-x);
int yoff=abs(yo-y);
int d = xoff*10 + yoff;
if(d != 11) return false;

if(x<3||x>5) return false;
    if(_redSide==s->getRed())
    {
        if(y<0||y>2) return false;
    }else
    {
        if(y < 7 || y > 9) return false;
    }


    return true;
}

相的移动
bool SceneGame::canMoveXiang(int moveid,int y)
{
Stone *s=_s[moveid];
int xo=s->getX();
int yo=s->getY();
int xoff=abs(xo-x);
int yoff=abs(yo-y);
int d = xoff*10 + yoff;
if(d != 22) return false;

int xm=(xo+x)/2;
    int ym=(yo+y)/2;
    int id=getStone(xm,ym);
    if(id!=-1)
    {
        return false;
    }
    if(_redSide == s->getRed())
    {
        if(y>4) return false;
    }
    else
    {
        if(y<5) return false;
    }
    return true;
}
车的移动
bool SceneGame::canMoveChe(int moveid,int y)
{
    Stone *s=_s[moveid];
    int xo=s->getX();
    int yo=s->getY();
    if(getStoneCount(xo,yo,y)!=0)
        return false;

    return true;
}

bool SceneGame::canMoveMa(int moveid,int y)
{
    Stone* s = _s[moveid];
    int xo = s->getX();
    int yo = s->getY();
    int xoff = abs(xo-x);
    int yoff = abs(yo-y);
    int d = xoff*10 + yoff;
    if(d != 12 && d != 21) return false;

    int xm,ym;
    if(d==12)
    {
        xm=xo;
        ym=(yo+y)/2;
    }else
    {
        xm=(xo+x)/2;
        ym=yo;

    }
    if(getStone(xm,ym)!=-1) return false;
    return true;
}

炮的移动
bool SceneGame::canMovePao(int moveid,int y)
{
Stone* s = _s[moveid];
int xo = s->getX();
int yo = s->getY();

if(killid != -1 && this->getStoneCount(xo,y) == 1)
        {
        return true;
        }

     else if(killid==-1)
     {
    return canMoveChe(moveid,y);
    }

    return false;

}
兵的移动
bool SceneGame::canMoveBing(int moveid,int y)
{
    Stone* s = _s[moveid];
    int xo = s->getX();
    int yo = s->getY();
    int xoff = abs(xo-x);
    int yoff = abs(yo-y);
    int d = xoff*10 + yoff;
    if(d != 1 && d != 10) return false;

    if(_redSide==s->getRed())
    {
        if(y<yo) return false;
        if(yo<=4&&y==yo) return false;
    }else
    {
        if(y>yo) return false;
        if(yo>=5&&y==yo) return false;
    }

    return true;
}

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