I was idle during the holidays, so I spent more than a week watching a video posted by a master alwing from Baidu Java Bar. I learned to write a Tetris in Java. Here I would like to thank him for his help in the form of publishing source code and necessary explanations. Of course, I have also made some changes here, and the program interface and functions I made are not exactly the same as his.
The interface for running the entire program is as follows:
The functions included in the program are:
one,
The design of the entire program is shown in the figure below, with a total of seven packages. Because the original design of the program was to connect to the database to save user names and scores, and my computer failed to install the database software, I made adjustments to the functions. Therefore, some package names and class names will not match their own functions.
two,
The most important interface programming in the entire program is established by the Layer class in the UI package. It defines a method to make a 64-by-64 pixel image cut and stretch it into a predetermined width and height using the drawImage function, and display it in the specified coordinates, thus becoming a window. Each Layer***.java class in the UI package inherits the Layer class and is used to implement each window interface, such as the game main window (LayerGame), level window (LayerLevel), score window (LayerPoint), etc. In addition, I also covered the window surface with a layer of translucent pictures, thus achieving a "haze".
three,
The Img class is used to declare all image objects. When I declare the border image WINDOW, I deliberately wrote the wrong path, thus achieving the borderless effect.
Four,
The drawing of falling squares is also done by picture cutting.
Each block of different colors is the same size, and each time you use the drawImage function to cut blocks of different colors. First of all, you should regard the main game window as a coordinate system with the upper left corner as the origin. The main game window has a width of 9 and a height of 16, so the coordinates of the lower right corner should be (15, 8), so that you can define the coordinates for the falling square. Then use List < Point[] > to save the coordinates of each small square in the falling block.
The falling square should be in the following seven shapes:
Then the initial coordinates when blocks 0 to 6 fall freely should be:
TYPE_CONFIG=new ArrayList<Point[]>(7);TYPE_CONFIG.add(new Point[]{new Point(4,0),new Point(3,0),new Point(5,0),new Point(6,0)});TYPE_CONFIG.add(new Point[]{new Point(4,0),new Point(3,0),new Point(5,0),new Point(4,1)});TYPE_CONFIG.add(new Point[]{new Point(4,0),new Point(3,0),new Point(4,1)});TYPE_CONFIG.add(new Point[]{new Point(4,0),new Point(3,0),new Point(5,0),new Point(3,1)});TYPE_CONFIG.add(new Point[]{new Point(4,0),new Point(5,0),new Point(3,1),new Point(4,1)});TYPE_CONFIG.add(new Point[]{new Point(4,0),new Point(5,0),new Point(4,1),new Point(5,1)});TYPE_CONFIG.add(new Point[]{new Point(4,0),new Point(3,0),new Point(5,0),new Point(5,1)});TYPE_CONFIG.add(new Point[]{new Point(4,0),new Point(3,0),new Point(4,1),new Point(5,1)});Each square has a center point. When the square rotates, the square rotates 90° at the center of the circle at that center point. The rotation method is as follows:
five,
The logic control of the game is mainly in the GameService class, which realizes the determination of whether the line can be eliminated, line operation, keyboard corresponding operation, etc.
six,
The program uses two additional jar packages, dom4j-1.6.1.jar and jl1.0.1.jar. dom4j-1.6.1.jar is used to read xml files. There is a cfg.xml file in the config folder, which sets the size and location of each window. Use dom4j-1.6.1.jar in the program to read the xml data, and then configure the program interface based on this data. Therefore, as long as the value in xml is changed, the size and coordinates of the window can be changed.
jl1.0.1.jar is used to play background music. This is a function I added myself. Through the method provided by this jar file, the function of playing music can be simply implemented. In the MP3Player class and Main class, a new thread needs to be created in the Main class. Specific reference method: JavaSwing background playback music
seven,
Source code program download: Source code
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.