Cocos2d-X development of Chinese chess "five" playing chess, cocos2d-x "five"
In the previous blog, game scenarios and buttons were created. As buttons implement specific response events, you need to use some functions in playing chess, for this reason, I specifically put the specific implementation method of the button at the end,
The idea of clearing the hem before playing the game:
1. Create a Stone class to process chess pieces.
2. Create a table to save chess pieces.
3. Create a pawn
4. initialize the pawns
5. Place pawns
The effect of playing chess:
Players play the game
Players play black games
Create a Stone class to process the information of the pawns and set the Stone class to be derived from CCSprite
// Chess pieces class Stone: public CCSprite {public: Stone (); // chess piece TYPE: Convert, Shi, Xiang, car, horse, cannon, and soldier enum TYPE {JIANG, SHI, XIANG, CHE, MA, PAO, BING}; // create a pawn. // The first parameter is the type of the pawn. // The second parameter is as follows: color of the pawnpiece static Stone * create (int id, bool red); // initialize the pawnpiece bool init (int id, bool red); // place the pawnpiece void reset (bool red ); // Save the initial position of the Mei pawns static struct InitPos {int _ x; int _ y; Stone: TYPE _ type ;}_ initPos [16]; CC_SYNTHESIZE (TYPE, _ type, Type) CC_SYNTHESIZE (int, _ x , X) CC_SYNTHESIZE (int, _ y, Y) CC_SYNTHESIZE (int, _ id, ID) CC_SYNTHESIZE (bool, _ dead, Dead) CC_SYNTHESIZE (bool, _ red, Red) /* // The position (coordinate) of the chess piece int x; int y; int _ id; // the ID of the chess piece 0 ~ 31 (a total of 32 pawns) bool dead; // determine whether the pawns have been eaten bool _ red; // determine the color of the pawns */};
Create a table to save the information of pawns
// Create a table Stone: InitPos Stone: _ initPos [16] ={// location of the car () {0, 0, Stone: CHE }, // The Position of the horse () {1, 0, Stone: MA}, // The Position of the phase () {2, 0, Stone: XIANG }, // SHI's position () {3, 0, Stone: SHI}, // The position () {4, 0, Stone: JIANG }, // SHI's position (5, 0) {5, 0, Stone: SHI}, // phase position (6, 0) {6, 0, Stone: XIANG }, // horse location () {7, 0, Stone: MA}, // car location () {8, 0, Stone: CHE }, // gun position (1, 2) {1, 2, Stone: PAO}, // gun position (7, 2) {7, 2, Stone: PAO }, // The Position of the soldier () {0, 3, Stone: BING}, // The Position of the soldier () {2, 3, Stone: BING }, // The Position of the soldier () {4, 3, Stone: BING}, // The Position of the soldier (6, 3) {6, 3, Stone: BING }, // position (8, 3) {8, 3, Stone: BING },};
Create a pawn
// Create a pawn // The first parameter: the type of the pawn // The second parameter: The color of the pawn Stone * Stone: create (int id, bool red) {Stone * s = new Stone (); s-> init (id, red); s-> autorelease (); return s ;}
Initialize pawns
/Initialize chess pieces // One-time initialization bool Stone: init (int id, bool red) {_ id = id; // obtain the id of the pawn. // when the id of the pawn is less than 16, the pawn is red _ red = _ id <16; // initialize the red pawn if (_ id <16) {_ type = _ initPos [_ id]. _ type;} else // initialize the black pawns {// type (CAR, horse, and gun) the IDS of two chess pieces in the same color differ by 16 _ type = _ initPos [_ id-16]. _ type;} const char * stonePic [14] = {"rshuai.png", // (red) Shuai "rshi.png", // (red) Shi "rxiang.png ", // (red) rche.png ", // (red) Car" rma.png ", // (red) horse" rpao.png ", // (red) Gun" Rbing.png ", // (red) bing" bjiang.png ", // (black)" bshi.png ", // (black) Shi" bxiang.png ", // (black) "bche.png", // (black) Car "bma.png", // (black) horse "bpao.png", // (black) Gun "bzu.png" // (black) pawn }; // calculate the subscript of the image. // If the pawns are red idx = _ type // If the pawns are black idx = 7 + _ type // The subscript of the two pawns varies by 7 int idx = (_ red? 0: 1) * 7 + _ type; // create the genie (create a pawn) CCSprite: initWithFile (stonePic [idx]); // compress the pawn setScale (. 8f); // set the position of a pawn (set the position of the pawn) reset (red); return true ;}
Placement of pawns
// Place the pawns. // parameter: used to determine whether a player selects to enter the game scene. // click the red pawns void Stone: reset (bool red) {// The pawn was not eaten when it was just set. this-> setDead (false); if (red) // when a player clicks a red pawn to enter the game scene {// the color of the player's pawn is red if (_ id <16) // place the red pawns {// set the x coordinate of the pawns. this-> setX (_ initPos [_ id]. _ x); // set the y coordinate of the chess piece this-> setY (_ initPos [_ id]. _ y);} else // place the Black Pawn {this-> setX (8-_ initPos [_ id-16]. _ x); // The y coordinate difference between two pawns of the same color 9 // For example: the y coordinates of the red and black cars differ 9 this-> setY (9-_ initPos [_ id-16]. _ y) ;}} else // when a player clicks a Black Pawn to enter the game scene {// the color of the player's pawn is black if (_ id <16) // place the Black Pawn {this-> setX (8-_ initPos [_ id]. _ x); this-> setY (9-_ initPos [_ id]. _ y);} else // place the red pawns. {// the IDs of two pawns of the same color are different. 16 // For example: the id of a red car differs from that of a black car by 16 this-> setX (_ initPos [_ id-16]. _ x); this-> setY (_ initPos [_ id-16]. _ y );}}}