Document directory
- Flash expert Tutorial: game preparation wuziqi
Flash expert Tutorial: game preparation wuziqi
Wuzilianzhu, also known as "wuziqi", is a widely spread puzzle board game. The game rules are very simple and suitable for friends of all ages, I believe that you must have played this game before. The traditional gameplay is that two players are playing on the go board. Here, the player's opponent will become a computer. Do you want to compete with the computer? Next we will learn how to use Flash to create the game of five sub-beads.
The main interface 1 of the game is shown below:
Figure 1
Gameplay:
The game requires two contestants to complete the game. One of them is a computer, and the computer uses the black game in the game. You use the white game in the game, the purpose of the game is to arrange the game on the Board as a straight line where five pieces are connected. The horizontal direction, vertical direction, or oblique direction are acceptable, but there is no inflection point, it must be a straight line. If either party connects first, it will win. Players should try their best to connect their chess pieces in the game. On the other hand, they also need to prevent the opponent from connecting the chess pieces easily until one party first connects five chess pieces, the game ends. In the game, to prevent one party from seeing the position of the other party in the previous step, the previous position is displayed in the upper right corner of the Board, the position of the piece is determined by the vertical logo on the board (1-15 in the horizontal direction, the vertical is the A-O ).
Game Production ideas:
This game is difficult to create. The key to the difficulty lies in that one of the players is a computer, and how to properly control the computer playing chess is the most critical part. If you want to enable the computer to automatically calculate chess and board games and decide sub-positions, you need to use some Ai knowledge, which will be explained in detail during game production.
Chess games are generally carried out on a two-dimensional board. Therefore, a two-dimensional array is used to record the position of a piece and determine the position of a piece, arrays are also widely used in chess games.
Game preparation steps:
The following describes how to create a game:
Game material preparation:
1. preparation of chess pieces: Of course, chess pieces are indispensable for chess games. In this game, two types of chess pieces are required, one is black and the other is white. By default, the computer uses black chess pieces, we can import two pieces of pictures from the external, or manually draw them in Flash. In this example, we will create two pawns by ourselves.
Create a new Graphic component and name it black graphic. Use the elliptical tool to draw a circle and fill it with black, so that a simple black pawn is ready, as shown in Figure 2:
Figure 2
Because we need to use Action to control the pawns in the game, we need to convert the Graphic component we made above into a Movie clip component. Therefore, create a new movie clip component named black. Drag the component black graphic to the editing area of the component black.
Create a white graphic component in the same way, draw a white chess piece, and then create the corresponding white movie clip component. The drawn white chess piece 3 is shown as follows:
Figure 3
2. control Button creation: in the game, you need to use the mouse to place the pawns on the board. Therefore, you need to use the buttons in combination with the pawns, because what we see in the game is actually the pawns, but the button is controlled, so here we only need to make a simple invisible button, the size of the button is similar to the size of the pawn, as shown in 4:
Figure 4
After the game ends, a Replay button is required to allow the player to play the game again. Therefore, you can create a simple button to control the re-running of the game, as shown in Replay button 5:
Figure 5
3. controlling the MC of a game: a large number of Action programs are indispensable in chess games. To facilitate control of the game, the Action is generally placed in a specialized Movie clip, then you can call it anywhere in the game.
A: Create A Movie clip component named blk. after entering the component editing status, add the following AS in the first key region:
Stop ();
Function makelist (maker)
// The makelist custom function. The parameter is maker, which generates a list.
{
If (maker = "white ")
// If the parameter passed to the function is white
{
Color = 2;
Color_bad = 1;
// Set two variable values
}
Else
// If the parameter passed to the function is black
{
Color = 1;
Color_bad = 2;
// Set two variable values
}
For (I = 0; I <15; I ++)
// External Loop, with the variable I and the number of cycles being 15
{
For (j = 0; j <15; j ++)
// External Loop. The cycle variable is j, the number of cycles is 15, and the total number of cycles is 15*15. This is equal to the size of the checkerboard.
{
If (_ root. table [I] [j] <> 0)
// The table array is used to check whether the Board is empty. The value is 1. Otherwise, it is 0.
{
For (k = 0; k <4; k ++)
{
If (maker = "white ")
{
_ Root. computer [I * 15 + j] [k] = 0;
}
Else
{
_ Root. player [I * 15 + j] [k] = 0;
}
} // If the player in the previous step is playing white games, the computer in the next step will be playing. Otherwise, the player will be playing white games.
} Else {
// If the chessboard is empty
For (k = 0; k <4; k ++ ){
X = I;
Y = j;
Score = 5;
While (_ root. table [x + _ root. dir [k] [0] [y + _ root. dir [k] [1] = color) and (x ++ _ root. dir [k] [0] <15) and (y + _ root. dir [k] [1]> = 0) and (y + _ root. dir [k] [1] <15) and (x + _ root. dir [k] [0]> = 0 )){
X = x + _ root. dir [k] [0];
Y = y + _ root. dir [k] [1];
Score = score * 2;
}
If (_ root. table [x + _ root. dir [k] [0] [y + _ root. dir [k] [1] = color_bad) and (x ++ _ root. dir [k] [0] <15) and (y + _ root. dir [k] [1]> = 0) and (y + _ root. dir [k] [1] <15) and (x + _ root. dir [k] [0]> = 0 )){
Score = score/2;
}
X = I;
Y = j;
While (_ root. table [x + _ root. dir [k + 4] [0] [y + _ root. dir [k + 4] [1] = color) and (x + _ root. dir [k + 4] [0] <15) and (y + _ root. dir [k + 4] [1]> = 0) and (y + _ root. dir [k + 4] [1] <15) and (x + _ root. dir [k + 4] [0]> = 0 )){
X = x + _ root. dir [k + 4] [0];
Y = y + _ root. dir [k + 4] [1];
Score = score * 2;
}
If (score> 30 ){
Score = score * 2;
}
If (_ root. table [x + _ root. dir [k + 4] [0] [y + _ root. dir [k + 4] [1] = color_bad) and (x + _ root. dir [k + 4] [0] <15) and (y + _ root. dir [k + 4] [1]> = 0) and (y + _ root. dir [k + 4] [1] <15) and (x + _ root. dir [k + 4] [0]> = 0 )){
Score = score/2;
}
If (maker = "white "){
_ Root. computer [I * 15 + j] [k] = score;
} Else {
_ Root. player [I * 15 + j] [k] = score;
}
} // Initialize the checkboard. Calculate the checkboard according to the difference in the previous step.
}
}
}
}
B: Create a new Movie clip component named blk2. after entering the component editing status, add as: stop () in the first key region ();
Insert a key secret in the sixth region and add the following:
If (_ root. playnow = "computer "){
// If the first child is a computer, the chessboard is initialized.
_ Root. func. makelist ("white ");
_ Root. func. makelist ("black ");
// Call the previously defined UDF and pass different parameters respectively.
Max_computer = 0;
Max_player = 0;
K = 0;
// Define three variables
For (j = 0; j <15; j ++ ){
For (I = 0; I <15; I ++ ){
For (k = 0; k <4; k ++ ){
If (_ root. computer [j * 15 + I] [k] = max_computer ){
Rndnum = random (100) + 1;
If (rndnum> 50 ){
Max_computer = _ root. computer [j * 15 + I] [k];
Xc = I;
Yc = j;
}
}
If (_ root. computer [j * 15 + I] [k]> max_computer ){
Max_computer = _ root. computer [j * 15 + I] [k];
Xc = I;
Yc = j;
}
If (_ root. player [j * 15 + I] [k]> max_player ){
Max_player = _ root. player [j * 15 + I] [k];
Xp = I;
Yp = j;
}
}
}
}
If (max_computer> max_player ){
X = xc;
Y = yc;
} Else {
X = xp;
Y = yp;
}
// Draw a pawn
Num = y * 15 + x;
RemoveMovieClip (_ root. B add num );
DuplicateMovieClip (_ root. white, "white" add num, num + 300 );
SetProperty (_ root. white add num, _ x, 34.7 + x * 20 );
SetProperty (_ root. white add num, _ y, 34.7 + y * 20 );
_ Root. table [y] [x] = 2;
// Display pawns
_ Root. display = "Xpos:" + (x + 1 );
Temp = chr (y + 65 );
_ Root. display1 = "Ypos:" + temp;
_ Root. playnow = "player ";
// Determine whether to win
Xx = x;
Yy = y;
For (k = 0; k <4; k ++ ){
If (_ root. a_result <> 6 ){
_ Root. a_result = 0;
X = xx;
Y = yy;
While (_ root. table [y] [x] = 2) and (y + _ root. dir [k] [1] <16) and (x + _ root. dir [k] [0]> =-1) and (x + _ root. dir [k] [0] <16) and (y + _ root. dir [k] [1]> =-1 )){
X = x + _ root. dir [k] [0];
Y = y + _ root. dir [k] [1];
// "_ Root. table [" + y + "] [" + x + "] =" + _ root. table [y] [x]
_ Root. a_result = _ root. a_result + 1;
}
X = xx;
Y = yy;
While (_ root. table [y] [x] = 2) and (y + _ root. dir [k + 4] [1] <16) and (x + _ root. dir [k + 4] [0]> =-1) and (x + _ root. dir [k + 4] [0] <16) and (y + _ root. dir [k + 4] [1]> =-1 )){
X = x + _ root. dir [k + 4] [0];
Y = y + _ root. dir [k + 4] [1];
_ Root. a_result = _ root. a_result + 1;
}
If (_ root. a_result = 6 ){
_ Root. playnow = "nobody ";
}
}
}
If (_ root. playnow = "nobody ")
}
C: Create a Movie clip component and name it bb. This component is used to calculate how the computer moves. after entering the component editing state, draw a filled rectangle in layer layer1, insert a key rectangle in the 5th rectangle and draw a rectangle of the same size and fill color at the same position. Create a new layer, enter the prompt text in the rectangle: Go to the computer, and then create a new layer Action, add AS: stop (); In the first step, and insert a key phrase in 8th seconds, add the following AS in this region:
If (_ root. playnow = "computer "){
// Initialize the chessboard
_ Root. func. makelist ("white ");
_ Root. func. makelist ("black ");
// Call a custom function
Max_computer = 0;
Max_player = 0;
K = 0;
// Define the three variables
For (j = 0; j <15; j ++ ){
For (I = 0; I <15; I ++ ){
For (k = 0; k <4; k ++ ){
If (_ root. computer [j * 15 + I] [k] = max_computer ){
Rndnum = random (100) + 1;
If (rndnum> 50 ){
Max_computer = _ root. computer [j * 15 + I] [k];
Xc = I;
Yc = j;
}
}
If (_ root. computer [j * 15 + I] [k]> max_computer ){
Max_computer = _ root. computer [j * 15 + I] [k];
Xc = I;
Yc = j;
}
If (_ root. player [j * 15 + I] [k]> max_player ){
Max_player = _ root. player [j * 15 + I] [k];
Xp = I;
Yp = j;
}
}
}
}
If (max_computer> max_player ){ X = xc; Y = yc; } Else { X = xp; Y = yp; } // Draw a pawn Num = y * 15 + x; RemoveMovieClip (_ root. B add num ); DuplicateMovieClip (_ root. white, "white" add num, num + 300 ); SetProperty (_ root. white add num, _ x, 34.7 + x * 20 ); SetProperty (_ root. white add num, _ y, 34.7 + y * 20 ); _ Root. table [y] [x] = 2; // Display pawns _ Root. display = "Xpos:" + (x + 1 ); Temp = chr (y + 65 ); _ Root. display1 = "Ypos:" + temp; _ Root. playnow = "player "; // Determine whether to win Xx = x; Yy = y; For (k = 0; k <4; k ++ ){ If (_ root. a_result <> 6 ){ _ Root. a_result = 0; X = xx; Y = yy; While (_ root. table [y] [x] = 2) and (y + _ root. dir [k] [1] <16) and (x + _ root. dir [k] [0]> =-1) and (x + _ root. dir [k] [0] <16) and (y + _ root. dir [k] [1]> =-1 )){ X = x + _ root. dir [k] [0]; Y = y + _ root. dir [k] [1]; // "_ Root. table [" + y + "] [" + x + "] =" + _ root. table [y] [x] _ Root. a_result = _ root. a_result + 1; } X = xx; Y = yy; While (_ root. table [y] [x] = 2) and (y + _ root. dir [k + 4] [1] <16) and (x + _ root. dir [k + 4] [0]> =-1) and (x + _ root. dir [k + 4] [0] <16) and (y + _ root. dir [k + 4] [1]> =-1 )){ X = x + _ root. dir [k + 4] [0]; Y = y + _ root. dir [k + 4] [1]; _ Root. a_result = _ root. a_result + 1; } If (_ root. a_result = 6 ){ _ Root. playnow = "nobody "; } } } // The game ends If (_ root. playnow = "nobody ") } The last timeline 6 is shown below: Figure 6 D: Create a new movie clip component named buttonclip. This component is used to control the sub-nodes on both sides. after entering the component editing status, drag and drop the preceding invisible button to the editing area, then add the AS: stop ();. E: Finally, create a movie clip named "gameover". This component is used to determine whether the game is over or not, that is, to determine whether a player has connected five pawns. After entering the component editing status, four layers are created consecutively, namely layer1, layer2, layer3, and action. The first layer of layer1, layer2, and layer3 is inserted with a blank space, then, draw the computer's winning prompt in 2nd workshop, drag the Replay button to the editing area, and add the As to the button: On (release) 7: Figure 7 In the Third Region of layer1, layer2, and layer3, follow the same method to set your victory prompt information, as shown in Figure 8: Figure 8 Return to the layer action, and then add the specific control Action. Add as: stop () to the first three key actions of the layer, and add the following AS to the fourth key role: For (I = 0; I <= 225; I ++ ){ RemoveMovieClip (_ root. black add I ); RemoveMovieClip (_ root. white add I ); RemoveMovieClip (_ root. B add I ); } // Clear the pawns on the board _ Root. gotoAndPlay ("run "); // Jump to the beginning of the game The last timeline 9 is shown below: Figure 9 Main scenarios: After the main work of the game is completed, you need to deploy the main scenario. First, use the drawing tool to draw the board in the game. The size of the board is 15*15, and then mark the Board horizontally and vertically, as shown in 10: Figure 10 Drag components white, black, buttonclip, blk, bb, and gameover to the appropriate positions on the stage, as shown in Figure 11: Figure 11 Instance name of component white: black Instance name of component black: white The instance name of the component buttonclip is B. The instance name of the component blk is func. Instance name of component bb: computerrun The instance name of the component gameover is gameoverman. Then, draw two Dynamic text boxes on the board to display the positions of the previous step, and set the variables to display and display1 respectively. Finally, add the as to the first key region: Stop (); Urls = "the prodigal son of the border city-the flash Empire "; Num = 0; Table = new Array (); Computer = new Array (); Player = new Array (); A_result = 0; // Table array For (I = 0; I <15; I ++ ){ Table [I] = new Array (0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ); } // Computer and player table list For (j = 0; j <15; j ++ ){ For (I = 0; I <15; I ++ ){ Computer [j * 15 + I] = new Array (0, 0, 0 ); Player [j * 15 + I] = new Array (0, 0, 0 ); } } // Make buttons Count = 0; For (j = 0; j <15; j ++ ){ For (I = 0; I <15; I ++ ){ DuplicateMovieClip (_ root. B, "B" add count, count + 30 ); SetProperty ("B" add count, _ x, 34.7 + I * 20 ); SetProperty ("B" add count, _ y, 34.7 + j * 20 ); Count ++; } } // Eight directions ctions Var dir = new Array (); Dir [0] = new Array (1, 0 ); Dir [1] = new Array (1, 1 ); Dir [2] = new Array (0, 1 ); Dir [3] = new Array (-1, 1 ); Dir [4] = new Array (-1, 0 ); Dir [5] = new Array (-1,-1 ); Dir [6] = new Array (0,-1 ); Dir [7] = new Array (1,-1 ); Playnow = "player "; RemoveMovieClip (_ root. b112 ); DuplicateMovieClip (_ root. white, "white112", 142 ); SetProperty ("white112", _ x, 174.7 ); SetProperty ("white112", _ y, 174.7 ); // The first black point Table [7] [7] = 2; So far, the game "wuzilianzhu" has been created. This game is a relatively advanced game and requires a certain foundation for Action programming. However, if you do not know much about Action, I believe that after learning this instance, you can improve your Action level to a certain extent. |