design
Operation process
The operation process of this game is very simple. After the user starts the MIDlet startup, you will enter the game main screen, and the screen starts to display as a welcome screen. After the user presses the [Start] button, you can start playing the game. When the user wants to pause, press the [Start] button again, the game is suspended, press the [Start] button in the case of suspending, the game continues to run . Press the [Exit] button at any time, the game MIDlet will terminate.
The game screen flow chart is as follows:
2. Algorithm
MIDP game design, essentially uses a thread or timer to generate a redraw event, and change the game status with thread and user input. This game is no exception. After starting the MIDET, you immediately generate a redraw thread that draws a screen every 50ms. Of course, there are some optimization measures when redraw, not all pixels on the screen need to be re-painted, but there is a choice, such as those who have fixed down on the game (there are 7 kinds of falls, from 4 Small bricks composition, each fall color is fixed, can rotate up and down left and right) no need to redraw. The game canvas is a commandListener, accept the user keyboard command, control the left shift of the fall, right movement, down, and rotate. The flow control of the entire game is embodied in the Paint () method of the game canvas object. Paint () draws the game screen at the time according to the current game state. Welcome screens and Game Over pictures are quite simple. The drawing of the game pause screen is also very easy, that is, set the flag to let Paint () do not really perform the redrawing action when it is executed. For the drawing of the game in the running state, you need to draw down the fall in the current location of the fall. Before drawing the fall, it is determined whether the fall is still falling. If you get falling, let it fall a place, then draw, if the fall is can't fall, it is determined whether the game is in the Game over state, if it is in GAME If the OVER state is set, set the game status as the Game Over status so that the canvas draws the picture of the Game over, if the game is not in the Game Over status, then check the game canvas up and down All rows below, see if you need to delete actions, if you need to delete, clear the data that is deleted on the game map, and then draw the built line to make a background color. Then initialize a new fall, draw this new fall. The flowchart of the PAINT method is as follows:
3. Data structure
This game involves the following data structures.
Game area
The game area is part of the mobile phone or the PDA screen, the area is square, and the side length must be resolved by 16 (because the Russian game area is just 16 small bricks long, 16 small bricks wide). Regardless of the horizontal or vertical direction, the area is in the center of place of the screen. The game area is divided into two parts in the horizontal direction, part of 12 small bricks wide, used to show the game container, the other part is 4 small bricks wide, used to display the next falling and score.
Small brick
Small brick blocks are components of falling objects and game containers. The manifestation is a square, and the side length is 1/16 of the game area edge. Each little brick is drawn, 4 sides will leave 1 quadprone wide, drawn into white or gray so that there is a gap between the bricks. Each small brick is also an ID, from 1 to 8, respectively. We can store these 8 colors with a color array (programs called brick_colors). If a small brick's ID is 3, the color of the tile is brick_colors [3-1].
Falling
The lower fall is essentially a square of 16 small bricks. There are 7 kinds of falls, such as "field" glyphs, "L" glyphs, etc. There are 4 rotational changes in each fall. Each falling material has an ID, from 1 to 7, respectively. Because for a falling object, its color is fixed. We can also use this color to add 1, as a subscript value in the brick_colors array, as the id of the fall, such as "L", the ID of the fall, its change form is:
So what data structure stores a falling material, we use "L"-shaped falling object as an example:
Because each falling object has four states, we can consider 4 states of a falling object with an array of length 4, and each element in the array indicates a state of the lower fall. So what is used to represent a certain state of a fall, it can be seen from the above figure, and a state in which a falling object is stored in a 4X4 two-dimensional array is the most suitable. At the position of the colored bricks, the value is 1, but only the background color, the value does not need to be drawn, the value is 0. Therefore, the four states of the entire "L"-shaped fall can be represented by a 3-dimensional group:
protected int blockpattern3 [] [] [] = {
{{0, 1, 0, 0}, {0, 1, 0, 0}, {0, 1, 1, 0}, {0, 0, 0, 0}},
{{0, 0, 0, 0}, {0, 1, 1, 1}, {0, 1, 0, 0}, {0, 0, 0, 0}},
{0, 0, 0, 0}, {0, 1, 1, 0}, {0, 0, 1, 0}, {0, 0, 1, 0}},
{{0, 0, 0, 0}, {0, 0, 1, 0}, {1, 1, 1, 0}, {0, 0, 0, 0}}}
}
Game map
The game map is used to store a fixed brick block on the game container. The game container is a wide range of 12 small bricks, which is 16 small bricks, including left and right 2 walls and bottom containers. So use a 16x12 two-dimensional array (programs called MapData) to store fixed bricks. If MAPDATA [i] [j] = k (k! = 0). Then indicate that there is a fixed small brick block on the i line j column of the game container, the color value of the small brick is brick_colors [k-1]. If K = 0, the i line J column has no bricks.
Therefore, for the following game running moments, the value of Mapdata is {{8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8} {8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 8} {8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 8} {8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 8} {8, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0,0,0,0,8} {8, 0, 0, 0, 0, 8} {8,0,0,0, 0, 0, 0, 0, 0, 0, 8} {8, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 8} {8, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 8} {8, 0, 0, 0, 0, 0, 7, 7, 5, 1, 1, 8} {8, 0, 5, 0, 0, 7, 2, 5, 5, 1, 1, 8} {8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8}}
Source code and executable code
A total of 3 files: src.rar, ketrisgame.jad, Ketrisgame.jar Description: There is all source code in src.rar. There is also the resource file required to run in KETRISGAME.JAR. After installing WTK2.1, let KETRISGAME.JAD and KETRISGAME.JAR are in the same directory (the directory path cannot contain Chinese and space remember), double-click the ketrisgame.jad file, You can run the game in the simulator.
Reference
http://www.javadrive.jp/j2me/game/3/index.html
About the Author
Chen Wanfei, male, scholar number of Soft Biliners in Central South University, served as senior programmer of Beijing Great Wall Software, system analyst. There is a relatively rich J2SE, J2EE development experience. Currently working on J2ME research. Can contact him via chen_cwf@163.net
This article original from http://gceclub.sun.com.cn/yuanchuang/week-10/game.html