Project combat: Simple Tetris (with source code)

bg

Foreword

Learn java, has been looking for a real project, Tetris is a real good project, it is relatively simple principle. Having said that, I still have no idea initially, until the tubing to the point of view of a Tetris video source and watch him achieve in GitHub, only ideas. I did some processing based on his program, made the following semi-finished products. Tetris is more suitable for the novice to practice, I'll share some tips and some of the experience I was doing this project below, welcomed the discussion!

ready

Not swing but relatively new javafx I use, but in fact, javafx I just learned I need to use the place, so if you used the swing, started making a javafx Tetris is quite fast, after all, we just need to javafx few of the more common API.

Design ideas

Tetris has the following seven kinds of shapes:

Their name: O, J, L, Z, S, T, I (the letters and figures compare what you know why so called). Second, each box has up to four morphological transformation. Here is what I see in a blog so transform:

First, the class is designed to represent the seven shapes, where I started to want to inherit a base class with seven classes to implement, but later found not as good as write a class with relatively simple, the idea is to design a specific shape class, it has attribute name, etc., and when the program when new, randomly selects a name in the name of seven, and the subsequent operation of the method and places the name basis. Shape class initialization and reversing its name because of the different methods and different. Part of the code as follows:

After that, we generate a two-dimensional array as a matrix to indicate the shape of a zero current location, the shape of the move, in addition to the checks do not cross the border, also need to check whether the move will encounter other shapes, this is our two-dimensional array role, and it should be easy to understand.

The method of moving a shape in three ways: right, left, up and down. The first two relatively simple to implement, the last one because of the need to consider more factors and more complicated. Here, I need to predict the next shape, which means I must not move in the shape of the current control is switched control to the next shape. Each time it actually has two top shape generation, only one of which is stationary and not visible. When switching control, you must check for an arc line, part of the code as follows:

I think the back is more difficult to eliminate rows of the operation, if not handled properly there will be a lot of strange bug, I realize now the general idea is as follows: first check the entire two-dimensional array, you can find eliminate the "line" and will need to eliminate all the rows and eliminate, after elimination, there will be no elimination of the box corresponding to the distance down, and finally updated two-dimensional array.

to sum up

The above is the overall design and implementation of ideas, leaving other elements of interface design and other details of the design are relatively simple, no matter how tired later, I finally share the link to see the video of tubing for reference, as well as my project Source.

link

I refer to video:

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

My project source code:

github.com/ZT-XU/Tetri…

Guess you like

Origin juejin.im/post/5d2a01ae6fb9a07ed6580174
Recommended