Android realizes crazy game logic (5)

The two classes mentioned in the previous "my android advanced journey -- > Android crazy Lianliankan game implementation loading interface picture and implementation of game activity (IV): gameconf: is responsible for managing the initialization setting information of the game. Gameservice: responsible for the logical implementation of the game. The code of gameconf is as follows: cn \ oyp \ link \ utils \ gameconf.java

Gameservice is the core of the whole game logic implementation, and gameservice is a reusable business logic class. It has nothing to do with the game platform. It can be used in both Java swing and Android games. Even with a little modification, gameservice can also be transplanted to Lianliankan games on the c# platform. Considering the extensible line of the program, first define an interface for the gameservice component. The code is as follows: cn \ oyp \ link \ board \ gameservice.java

Let's specifically implement the gameservice component. First, the public void start() method, public piece [] [] getpieces() method and public Boolean haspieces() method are easy to implement. The specific implementation is as follows: cn \ oyp \ link \ board \ impl \ gameserviceimpl.java

@H_ 404_ 26@1 . get the square of touch point

First, when the user touches the game interface, the event listener obtains the X and Y coordinates of the touch on the game interface, but what the program needs is to obtain which square the user touches. Therefore, the program must convert the X and Y coordinates on the interface into two index values in the piece [] [] two-dimensional array. Considering that the height and width of each box on the game interface are the same, it is easy to convert the X and Y coordinates on the interface into the index in the piece [] [] two-dimensional array. Just divide the X and Y coordinate values by the width and height of the picture. The following is the code for the block obtained according to the X and Y coordinates of the contact:

The above method calls the getindex (int relative, int size) method. The implementation of this method is to divide relative by size. The program needs to judge whether it can be divided or not: if it can be divided, it means it is still in the previous box; If it cannot be divided, the following is the code of getindex (int relative, int size) method for the next block:

@H_ 404_ 26@2 Judge whether the two blocks can be connected

The situations where two blocks can be connected can be roughly divided into the following:

The following link (piece P1, piece P2) method processes these four cases separately. The code is as follows:

@H_ 404_ 26@3 Define the method of obtaining channels

The so-called channel refers to a blank box in the upper, lower, left and right directions of a box, as shown in the following figure:

Here are four methods to obtain the channels around a coordinate point:

The haspiece (int x, int y) method called above is to judge whether there is a piece object in the X and Y coordinates in the gamepanel. The code is as follows:

@H_ 404_ 26@4 Horizontal connection without turning point

If the 2D index values of two piece objects in the piece [] [] array are equal, then the two pieces are in the same row. At this time, it is necessary to judge whether the two pieces have obstacles directly, and call the isxblock (point P1, point P2, int piece width) method. The code is as follows:

If two squares are on the same line and there is no obstacle between them, the two squares can be eliminated, and the connection information of the two squares is their center.

@H_ 404_ 26@5 Vertical connection without turning point

If the first dimension index values of two piece objects in the piece [] [] array are equal, the two pieces are in the same column. At this time, you need to judge whether the two pieces have obstacles directly, and call the isyblock (point P1, int piece width) method. The code is as follows:

If two squares are in the same column and there are no obstacles between them, the two squares can be eliminated, and the connection information of the two squares is their center.

@H_ 404_ 26@6 Connection of a turning point

When there is only one turning point on the connecting line of two blocks, the program needs to find the turning point first. In order to find this turning point, the program defines a method to traverse two channels and obtain their intersection, getwrappoint (list < point > p1chanel, list < point > p2chanel), with the following code:

In order to find the connection point on the connection line of two blocks, the program needs to analyze the position distribution of P1 and P2. So we can analyze that P2 is either in the upper right corner of P1 or in the lower right corner of P1. As for the case where P2 is located in the upper left corner and lower left corner of P1, just exchange P1 and P2, as shown in the following figure:

When P2 is located in the upper right corner of P1, it should be calculated whether there is an intersection between the right channel of P1 and the lower channel of P2, and whether there is an intersection between the upper channel of P1 and the left channel of P2. When P2 is located in the lower right corner of P1, it should be calculated whether there is an intersection between the right channel of P1 and the upper channel of P2, and whether there is an intersection between the lower channel of P1 and the left channel of P2.

The following is the code for implementing the method getcornerpoint (point point1, point point2, int piecewidth, int pieceheight):

The above method calls the following four methods:

@H_ 404_ 26@7 Connection of two turning points

The two turning points can be discussed as follows:

As for the case where P2 is located in the upper left corner and lower left corner of P1, just exchange P1 and P2.

1) , P1 and P2 are in the same line and cannot be directly connected. There must be two turning points, as shown in the figure below

When P1 and P2 are in the same row and cannot be directly connected, the two points can be connected either above or below. Both cases mean that they can be connected. First add these two cases to the result, and finally calculate the nearest distance. When implementing, first build a map. The key of the map is the first turning point and the value of the map is the second turning point. If the size () of the map is greater than 1, it indicates that the two points have multiple connection ways, and the program also needs to calculate the connection method with the smallest path.

2) P1 and P2 are located in the same line and cannot be directly connected. There must be two turning points, as shown in the above figure. When P1 and P2 are in the same column and cannot be directly connected, the two points can be connected on the left or right. Both cases mean that they can be connected. First add these two cases to the result, and finally calculate the nearest distance. When implementing, first build a map. The key of the map is the first turning point and the value of the map is the second turning point. If the size () of the map is greater than 1, it indicates that the two points have multiple connection ways, and the program also needs to calculate the connection method with the smallest path.

3) There are six turning situations where P2 is located at the lower right corner of P1, as shown in the following figure:

Define a method to handle the above two connection points, getlinkpoints (point point1, int piecewidth, int pieceheight). The code is as follows:

The codes of the getxlinkpoints and getylinkpoints methods called above are as follows:

@H_ 404_ 26@8 Find the shortest distance

In order to find the shortest path in all connection cases, the program can be divided into the following 2 steps:

Traverse all key value pairs in the turning point map to form a linkinfo with the originally selected two points. Each linkinfo represents a complete connection path and collects these linkinfo into a list collection.

Traverse the list < linkinfo > set obtained in the first step, calculate the total distance connecting all connection points in each linkinfo, and select the linkinfo with the smallest difference from the shortest distance to return.

For specific implementation steps, please refer to the following link:

My advanced journey of Android ------ > preview of the game effect of Android crazy game (I)

My advanced journey of Android ------ > Android crazy watch the implementation of the game and develop the game interface (2)

My advanced journey of Android ------ > Android crazy continuous watching of the implementation state data model of the game (3)

My advanced journey of Android ------ > Android crazy watch the implementation of the game, load the interface pictures and realize the game activity (4)

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>