Cocos2d-X development of Chinese chess game 8

Source: Internet
Author: User

Cocos2d-X development of Chinese chess game 8

In the previous section, we achieved a new game. Now we have almost completed the preparations for Chinese chess. In the next blog, we will introduce the players' playing chess and game attribute settings, today, we will introduce the rules for playing games and playing games.

Old Rules: Let's take a look at the game, analyze the game logic step by step, and finally paste the code


Playing chess involves two steps:
Step 1: select a chess piece by clicking the chess piece. When a chess piece is selected, the selection box is set on the selected chess piece.


<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + tdq2/rK919/G5aO6zai5/bXju/fg5cxyy + 1xM671sO/far + fill + 1 vbXju/e1xM671sM8L3A + fill = "http://www.2cto.com/uploadfile/Collfiles/20141119/2014111908390750.gif" alt = "\">


Go chess implementation

1. When playing games, the principle of red first and black is adopted.
2. Click to select the required pawns

3. When there is a pawn on the click position, the selection box is displayed and the record is clicked once.

4. When there are no pawns in the position you click, determine whether or not you have clicked the pawns last time. If you have clicked the pawns last time, move the pawns to the position in the second position.

5. When playing games, the principle of red first and black is adopted.


Implementation Code

First declare a ccTouchBegan (CCTouch * pTouch, CCEvent * pEvent) function in SceneGame. h, and then define ccTouchBegan (CCTouch * pTouch, CCEvent * pEvent) in SceneGame. cpp)

// Click and select the pawns to go To the pawns bool SceneGame: ccTouchBegan (CCTouch * pTouch, CCEvent * pEvent) {CCObject * obj = (CCObject *) pTouch; // obtain the window coordinate CCPoint ptInWin = pTouch-> getLocation () of the touch point. // if (sprite-> boundingBox () when the result box of the game is touched (). containsPoint (ptInWin) & visible = true) {// hide the result HideResult (sprite, obj);} else if (sprite1-> boundingBox (). containsPoint (ptInWin) & visible = true) {// hide the result HideResult (sprite1, obj);} int x, Y; // Save the checkboard coordinates of the touch point. // obtain the x and y coordinates of the checkboard using the window coordinates of the touch point. if (! GetClickPos (ptInWin, x, y) {return false;} // obtain the id of the selected chess piece, int clickid = getStone (x, y), by touching the coordinates in the chessboard ); // when there is a pawn at the position of the touch point, clickid is the id of the selected pawn, indicating that the player is selecting the chess. // when there is no pawn at the position of the touch point, clickid is-1, indicating that the player is playing chess //-1 = _ selectid indicates that no pawn is selected if (-1 = _ selectid) {setSelectId (clickid );} else {// move the pawns // The first parameter: the id of the moved pawns // The second parameter: Determine whether there are pawns on the touch point by the position of the touch point // The third parameter: x coordinate of the touch point // The fourth parameter: y coordinate of the touch point // moveStone performs two steps: Select chess and go chess // select a pawn: When _ selectid = clickid, indicates that the selected The id is _ selectid. // when selectid! = When clickid is used, it means to move the pawn with id _ selectid to the position where (x, y) moveStone (_ selectid, clickid, x, y );} // CCLog ("_ selectid = % d, clickid = % d", _ selectid, clickid); // CCLog ("x = % d, y = % d ", x, y); return true ;}


Declare a member function MoveStone () in SceneGame. h to implement moving pawns.

// Move the pawns // The first parameter: the id of the moved pawns // The second parameter: Determine whether there are pawns on the touch point by the position of the touch point // The third parameter: x coordinate of the touch point // fourth parameter: y coordinate of the touch point void SceneGame: moveStone (int moveId, int killId, int x, int y) {// killId! =-1 indicates that there is a pawn in the position of the touch point // _ s [moveId]-> getRed () = _ s [killId]-> getRed () indicates that the color of the pawns on the touch point is the same as that on the go chess. if (killId! =-1 & _ s [moveId]-> getRed () = _ s [killId]-> getRed () {// change the selection box setSelectId (killId ); return;} // CCLog ("killId = % d, moveId = % d", killId, moveId); // CCLog ("_ s [moveId]-> getRed () = % d ", _ s [moveId]-> getRed (); // go chess rule bool bCanMove = canMove (moveId, killId, x, y ); // if bCanMove is false // you cannot play chess. if (false = bCanMove) {return;} // you can record the information of a pawn before playing chess. // The first parameter: id of the pawn to be moved // second parameter: determine whether there is a pawn on the touch point by the position of the touch point // third parameter: x coordinate of the current position of the chess piece // fourth parameter: y coordinate of the current position of the chess piece // fifth parameter: x coordinate of the position after the chess piece moves // sixth parameter: step * step = Step: create (moveId, killId, _ s [moveId]-> getX (), _ s [moveId]-> getY (), x, y); // Add the information of the pawns to the array _ steps-> addObject (step ); // set the coordinates (move the pawns) _ s [moveId]-> setX (x); _ s [moveId]-> setY (y ); // _ s [moveId]-> setPosition (getStonePos (x, y); // SetRealPos (_ s [moveId]); // set the action CCMoveTo * move = CCMoveTo: create (. 5f, getStonePos (x, y); // action callback CCCallFuncND * call = CCCallFuncND: create (this, callfuncND_selector (SceneGame: moveComplete), (void *) (intptr_t) killId); // set the execution sequence of the action (move the pawns first and then call the callback function) CCSequence * seq = CCSequence: create (move, call, NULL ); // set the priority of the moved pawns _ s [moveId]-> setZOrder (_ s [moveId]-> getZOrder () + 1 ); // execute the Action _ s [moveId]-> runAction (seq );}


Zookeeper

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.