日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

cocos2dx实现象棋之运动

發(fā)布時間:2024/7/23 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 cocos2dx实现象棋之运动 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1、頭文件

void moveStone(int moveid, int killid, int x, int y);void moveComplete(CCNode*, void*);bool canMove(int moveid, int killid, int x, int y);bool canMoveJiang(int moveid, int killid, int x, int y);bool canMoveShi(int moveid, int x, int y);bool canMoveXiang(int moveid, int x, int y);bool canMoveChe(int moveid, int x, int y);bool canMoveMa(int moveid, int x, int y);bool canMovePao(int moveid, int killid, int x, int y);bool canMoveBing(int moveid, int x, int y);int getStoneCount(int xo, int yo, int x, int y);

要實現(xiàn)象棋的運動,首先要確定選中的目標(biāo),再其次確定選中的位置,再確定選擇的位置是否有對方棋子

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 killid, int x, 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 stepStep* step = Step::create(moveid, killid, _s[moveid]->getX(),_s[moveid]->getY(), x, 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; }

不同的棋子運動規(guī)則不同,車,馬,炮等都有不同的運動方式

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

case Stone::SHI:return canMoveShi(moveid, x, y);case Stone::XIANG:return canMoveXiang(moveid, x, y);case Stone::CHE:return canMoveChe(moveid, x, y);case Stone::MA:return canMoveMa(moveid, x, y);case Stone::PAO:return canMovePao(moveid, killid, x, y);case Stone::BING:return canMoveBing(moveid, x, y);}return false; }

將的移動
bool SceneGame::canMoveJiang(int moveid, int killid, int x, int y)
{

if(killid!=-1){Stone* skill = _s[killid];if(skill->getType() == Stone::JIANG){return canMoveChe(moveid, x, y);}}//can only walk one step and can not walk out the lattic also can kill the kingStone *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 x, 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 x, 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 x, int y) {Stone *s=_s[moveid];int xo=s->getX();int yo=s->getY();if(getStoneCount(xo,yo,x,y)!=0)return false;return true; }bool SceneGame::canMoveMa(int moveid, int x, 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 killid, int x, int y)
{
Stone* s = _s[moveid];
int xo = s->getX();
int yo = s->getY();

if(killid != -1 && this->getStoneCount(xo, yo, x, y) == 1){return true;}else if(killid==-1){return canMoveChe(moveid,x,y);}return false;} 兵的移動 bool SceneGame::canMoveBing(int moveid, int x, 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; }

總結(jié)

以上是生活随笔為你收集整理的cocos2dx实现象棋之运动的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。