This type of games must have been played by everyone, a very interesting little game. The operation is simple and logical. It is suitable for recreation in boring times. :)
First, we will briefly introduce the general development process of mobile games. First, you need to plan a game solution, that is, to give a game's overall image. Of course. For example, game
Drama subject, background, operation method, character, and reward mechanism. Then, analyze the feasibility of the plan, including technology, market, and available resources. The next step is the encoding phase,
Generally, encoding and production resources can be synchronized. After the program and resources are completed, the prototype of a game is roughly displayed. The next step is testing and porting.
What we need to do is to let a squirrel push the box. The operation is to move the squirrel up, down, left, right through the four direction keys. During the moving process, we can push the boxes we encounter to any
One Direction. Of course, some obstacles need to be set in the game to make the game playable. The game uses a pass system. When a squirrel pushes all the boxes to a specified position, the game passes. Therefore
Different scenarios. This requires you to orchestrate the map of each level. Well, our game planning is complete, hey! It's easy. Imagine how beautiful it is.
The following is not so easy. We need to perform technical analysis. Is how the specific code is implemented. First, let's determine the development difficulties. The operation on squirrels is simple.
Moving, squirrel moving, and box moving, it is easier to process keys. When the box reaches a certain position, a game clearance event will occur. A logical judgment is required. So
Let's take a closer look at all these events in a map. This map includes the initial location of the box, the final location of the box, and obstacles. Map of each level
Must be replaced. These locations also need to be changed. Therefore, we find that the map data at each level is the most critical. It determines the location of different scenes and objects at each level. Okay. So let's focus on the analysis.
Map.
We think of a map as a grid. Each grid is the step size of the squirrel moving each time and the distance of the box moving, which simplifies the problem. First, we design an 8*8 Data knot.
Structure. Follow this framework. What are the attributes of each grid? The first is the grid coordinates, including the x and y values, and some map attributes. For example, the grid is
Whether it is an obstacle, whether it is the initial box location, or whether it is the terminal location of the box. Because our data structure is two-dimensional, but there is still one-dimensional representation, so we design
3D data:
Private Static int [] [] [] map_data = new int [8] [8] [5];
// The first dimension-[8] indicates the height of the map, which consists of eight grids.
// The second-[8] indicates the map width, which consists of eight grids.
// 3D-[0] x coordinate of each grid
//-[1] Y coordinate of each grid
//-[2] indicates whether an obstacle is found. 0 indicates an open space. 1 indicates an obstacle.
//-[3] The terminal coordinate of the box, 0 is not the terminal coordinate, 1 Green Box, 2 red box
//-[4] the initial position of the Box [0] is not the initialization coordinate [1] Green Box 2 red box
The map is roughly like this data structure. We design two color boxes to increase the difficulty of the game.
Now, with the map, we can implement the logic. Using a little time, I drew pictures of the game. Fortunately, I graduated from an art major and those small pixel pictures were quite smooth.
^ + ^.
Let's take a look at the game interface. The interface is the menu, progress bar, and draw in the game. Menus and progress bars are skipped because they can be designed by yourself.
Below is a map and squirrel. box and a method to indicate the destination position.
// Draw a map
Private void drawmap (Graphics g)
{
For (INT m = 0; m <map_data.length; m ++) // y coordinate
{
For (INT n = 0; n <map_data [M]. length; n ++) // X coordinate
{
If (map_data [N] [m] [2] = 1) // if it is an open space
{
G. drawimage (Map [0], map_data [N] [m] [0], map_data [N] [m] [1], 0 );
} Else if (map_data [N] [m] [2] = 0) // if it is an obstacle
{
G. drawimage (Map [1], map_data [N] [m] [0], map_data [N] [m] [1], 0 );
}
}
}
}
The map array is an array of images with only two elements. There are two images in the array, which are open spaces and obstacles respectively.
// Draw a squirrel
Private void drawsqu (Graphics g)
{
G. drawimage (Squirrel [sdir], map_data [(sprel [1])] [(splace [0])] [0], map_data [(splace [1])] [(splace [0])] [1], 0 );
}
The sp [] Here is the initialization position of the squirrel, which varies with each other.
Sdir indicates the direction of the squirrel, corresponding to the squirrel image in the squirre [] Array
// Draw a box
Private void drawboxs (Graphics g)
{
For (INT I = 0; I <nplace. length; I ++)
{
G. drawimage (nut [(nplace [I] [2]-1)], map_data [(nplace [I] [1])] [(nplace [I] [0])] [0], map_data [(nplace [I] [1])]
[(Nplace [I] [0])] [1], 0 );
}
}
Nplace [] [] is the initial coordinate of the box. The first dimension indicates several boxes, and the second dimension indicates the X, Y, and color attributes.
Nut [] is a box Image array
// Specify the terminal position of the box
Private void drawnutdest (Graphics g)
{
For (INT I = 0; I <ndplace. length; I ++)
{
G. drawimage (nut [(ndplace [I] [2] + 1)], map_data [(ndplace [I] [1])] [(ndplace [I] [0])] [0], map_data [(ndplace [I] [1])]
[(Ndplace [I] [0])] [1], 0 );
}
}
We also use images for the box's key indicators to make the screen more beautiful.
Similarly, ndplace [] [] and nplace [] [] have the same attributes. It only indicates the end coordinate.
An additional element of the nut [] array represents an image indicating the coordinates of the key.
The above is our main game interface. It looks quite simple. Haha. The following describes how to implement the operation control method and the logic of deciding whether to win or lose. Hey, be patient.
As we mentioned above, when the squirrel moves, the box moves, so we will write two separate methods to move them.
/* Move squirrel */
Private Static final void movesqu ()
{
/* Indicates whether a box exists in the moving direction of the squirrel */
Boolean hit = false;
/* Indicates that a box can be moved */
Boolean mf = false;
/* When it is up */
If (moves [0])
{
/* When moving up, adjust the image to the second squirrel figure */
Sdir = 2;
/* Set the portable representation to false */
Moves [0] = false;
/* Check whether a box exists near the squirrel */
For (INT r = 0; r <nplace. length; r ++)
{
/* If there is a box */
If (splace [1]-1) = nplace [r] [1]) & (splace [0] = nplace [r] [0]) {
/* Move the box and return a Judgment indicating whether the box can be moved */
Mf = moveboxs (0, R );
/* Set the collision flag */
Hit = true;
}
}
/* If there are no boxes, it is an open space */
If (map_data [(splace [1]-1)] [(splace [0])] [2] = 0 )&&(! Hit ))
{
/* Move squirrel */
Splace [1]-= 1;
}
/* If you encounter a box */
If (hit)
{
/* And the box can be pushed */
If (MF ){
/* Move squirrel */
Splace [1]-= 1;
/* Set collision to no */
Hit = false;
}
}
}
/* Downward */
If (moves [1])
{
// Debug ("down ");
Sdir = 3;
Moves [1] = false;
For (INT r = 0; r <nplace. length; r ++)
{
If (splace [1] + 1) = nplace [r] [1]) & (splace [0] = nplace [r] [0]) {
Mf = moveboxs (1, R );
Hit = true;
}
}
If (map_data [(splace [1] + 1)] [(splace [0])] [2] = 0 )&&(! Hit ))
{
Splace [1] + = 1;
}
If (hit)
{
If (MF)
{
Splace [1] + = 1;
Hit = false;
}
}
}
/* To the left */
If (moves [2])
{
// Debug ("Left ");
Sdir = 0;
Moves [2] = false;
For (INT r = 0; r <nplace. length; r ++)
{
If (splace [0]-1) = nplace [r] [0]) & (splace [1] = nplace [r] [1]) {
Mf = moveboxs (2, R );
Hit = true;
}
}
If (map_data [(splace [1])] [(splace [0]-1)] [2] = 0 )&&(! Hit )){
Splace [0]-= 1;
}
If (hit)
{
If (MF)
{
Splace [0]-= 1;
Hit = false;
}
}
}
/* To the right */
If (moves [3])
{
// Debug ("right ");
Sdir = 1;
Moves [3] = false;
For (INT r = 0; r <nplace. length; r ++)
{
If (splace [0] + 1) = nplace [r] [0]) & (splace [1] = nplace [r] [1]) {
Mf = moveboxs (3, R );
Hit = true;
}
}
If (map_data [(splace [1])] [(splace [0] + 1)] [2] = 0 )&&(! Hit )){
Splace [0] + = 1;
}
If (hit)
{
If (MF)
{
Splace [0] + = 1;
Hit = false;
}
}
}
}
The above method looks long. In fact, the logic is relatively simple. It is determined by moving in four directions. You can see the comments that move up. Moves [] is a blooean number.
Group, used to indicate the moving direction of the squirrel. This array will be assigned a value in keypressed (). When a direction key is pressed, the flag in the corresponding direction is set to true. The moveboxs () method below is
Determine whether there are other obstacles to a box in the push direction. If yes, true is returned. Otherwise, false is returned and the box is moved.
/*
*
* Method function: Move the box
*
* Parameter: Dir: Push direction. NM: indicates which box
*
* Return value: whether the box can be pushed
*/
Private Static final Boolean movenuts (INT Dir, int nm)
{
/* Indicate that another box is beside the box */
Boolean hit = false;
/* Temporary Variable */
Boolean mf = false;
Switch (DIR)
{
Case 0: // 'up'
/* Traverse the coordinates of all boxes */
For (INT I = 0; I <nplace. length; I ++)
{
/* Use coordinates to determine if there are other boxes in the direction of the current box to be moved */
If (nplace [Nm] [1]-1) = nplace [I] [1]) & (nplace [Nm] [0] = nplace [I] [0])
Hit = true;
}
/* If it is an open space */
If (map_data [(nplace [Nm] [1]-1)] [(nplace [Nm] [0])] [2] = 0 )&&(! Hit ))
{
/* Move the box and set the return value to movable */
Nplace [Nm] [1]-= 1;
Mf = true;
}
Break;
Case 1: // 'low'
For (INT I = 0; I <nplace. length; I ++)
{
If (nplace [Nm] [1] + 1) = nplace [I] [1]) & (nplace [Nm] [0] = nplace [I] [0])
Hit = true;
}
If (map_data [(nplace [Nm] [1] + 1)] [(nplace [Nm] [0])] [2] = 0 )&&(! Hit ))
{
Nplace [Nm] [1] + = 1;
Mf = true;
}
Break;
Case 2: // 'left'
For (INT I = 0; I <nplace. length; I ++)
{
If (nplace [Nm] [1]) = nplace [I] [1]) & (nplace [Nm] [0]-1) = nplace [I] [0])
Hit = true;
}
If (map_data [(nplace [Nm] [1])] [(nplace [Nm] [0]-1)] [2] = 0 )&&(! Hit ))
{
Nplace [Nm] [0]-= 1;
Mf = true;
}
Break;
Case 3: // 'right'
For (INT I = 0; I <nplace. length; I ++)
{
If (nplace [Nm] [1]) = nplace [I] [1]) & (nplace [Nm] [0] + 1) = nplace [I] [0])
Hit = true;
}
If (map_data [(nplace [Nm] [1])] [(nplace [Nm] [0] + 1)] [2] = 0 )&&(! Hit ))
{
Nplace [Nm] [0] + = 1;
Mf = true;
}
Break;
}
Return MF;
}
This method is the same as determining whether a squirrel can move. In fact, it can be combined with the moving squirrel method to deal with it. In addition, it can optimize many things, but to make the logic clearer,
It is done separately.
/* Determine the Pass */
Private Static final Boolean iswin ()
{
Boolean temp = false;
Int nn = 0;
For (INT r = 0; r <nplace. length; r ++)
{
For (INT m = 0; m <ndplace. length; m ++)
{
If (nplace [r] [0] = ndplace [m] [0]) & (nplace [r] [1] = ndplace [m] [1])
Nn ++;
}
}
If (nn = nplace. length)
Temp = true;
Else
Temp = false;
Return temp;
}
The logic for determining the pass is very simple. It is to compare the coordinates of the destination with the coordinates of the current box one by one. If all the boxes have been correctly pushed to the destination, the pass is considered.
Now, our game interface and game logic implementation have been completed, and it feels very easy. Well, of course you have to write some fixed methods to complete the game. For example
Keypressed (); obtain the key value painting () to draw the thread run () to achieve screen swiping and so on. Due to limited space, I will not introduce them one by one. The following is a completed game.
Let's see the effect.
Download Code and images: movebox.rar
Squirrel push box game
Program Design and image production: Guan wenbai Email: k7sem_88@hotmail.com