preface

Learned Java, always want to find a project actual combat, Tetris is a good actual combat project, its principle is relatively simple. Having said that, I had no idea until I watched a Tetris video on YouTube and saw the source code of his implementation on GitHub. I did some processing based on his program and made the following semi-finished products. Tetris is still more suitable for novice practice, I will share some of my skills and experience in doing this project below, welcome to discuss!

To prepare

I’m using newer JavafX instead of Swing, but the truth is, JAVafX is just what I need to learn, so if you’ve ever used Swing, it’s faster to make a Tetris using JavafX. After all, we only need javafX’s few commonly used apis.

Design ideas

Tetris has the following seven shapes:

Their names are: O, J, L, Z, S, T, I (compare the letters to the shapes and you’ll see why). Second, each square can have up to 4 morphs. Here are all the changes I saw in a blog post:

First, the design classes to represent the seven kinds of shapes, here I want to use seven started out as a class inherits a base class to implement that later found together as a class to write is simpler, the concrete way of thinking is to design a shape class, it has the properties such as name, but when the program in the new name will randomly choose one of the seven name, Subsequent methods and operations are then based on this name. The initialization and flip methods of a shape class differ depending on its name. Part of the code is as follows:

After that, we generate a two-dimensional array as a zero-one matrix to represent the current position of the shape. When the shape moves, in addition to checking not to cross the boundary, we also need to check whether the move touches other shapes. This is the purpose of our two-dimensional array, which should be easy to understand.

There are three ways to move shapes: right, left, and down. The first two are easy to implement, while the last one is more complicated because there are more factors to consider. In this case, I need to predict the next shape, which means I have to transfer control to the next shape if the current shape I control cannot move. So there are actually two shapes at the top at every moment, but one of them is still and invisible. When control is switched, it is necessary to check if line elimination is required, with the following code:

Is behind I feel difficult line elimination, if processes is not good there will be a lot of strange bugs, I realize now the general idea is as follows: first check the two-dimensional arrays, find can disappear “line”, then all need to eliminate rows and eliminate, eliminate, after will not eliminate the square down corresponding to the distance, and then update the 2 d array.

conclusion

Above, is the overall design and implementation of the idea, the rest of the interface design of other elements and other details are relatively simple, will not be too tired to describe, finally share the YouTube video I see the link for reference, as well as my project source code.

link

The video I referenced:

www.youtube.com/watch?v=boA…

My project source code:

Github.com/ZT-XU/Tetri…