How to realize the Classic Tetris Game by Java
Java how to achieve the classic Tetris game, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.
Preface
Tetris is a puzzle video game originally designed and programmed by Alexei Pajitnov in the Soviet Union.
The basic rule of Tetris is to move, rotate, and place the various squares automatically output by the game, so that they are arranged into a complete one or more lines and eliminate scores.
It is implemented in java language, the interface processing is carried out by using swing technology, and the object-oriented idea is used in the design.
Main demand
Plates of different shapes composed of small squares fall from the top of the screen one after another, and players adjust the position and direction of the plates to make them form a complete one or more at the bottom of the screen. These complete stripes will then disappear, making room for the newly fallen plate, while players will be rewarded with points. Squares that have not been eliminated continue to pile up, and once they reach the top of the screen, the player loses and the game is over.
Main design
1. Use the keyboard to move "←" to the left, "→" to the right, "↑" to rotate the square, and ↓ to drop the square (the square falls to the bottom)
2. Once heaped to the top of the screen, the game is over
3. Design different squares
4. The falling speed of the design box
5. Design the score system
Function screenshot
Game launch page
Start the game.
Game over
Code implementation
Window design class
Public class Windows extends JFrame {private Toolkit took = null; private static int width= 470; / / wide private static int height = 680; / / High private static int width_g = 10; / / Game area private static int height_g = 22; / / private static int size = 30; / / Square size private static int space= 10 / / coordinate distance boundary interval Map map = new Map (width_g,height_g); / / Map coordinates ArrayList ds = new ArrayList (); / / Square array private boolean game=false; / / Game start private int flag = 0; / / Game status [pause: 0, continue: 1] private JLabel jl2; private JButton jb1 Private int speed = 500; / / Speed private int score = 0; / / score private int [] scores = new int [4]; / / ranking public static void main (String [] args) {Windows win = new Windows (); win.run () } public void run () {init (); try {while (true) {if (game&&flag==1) {ds.get (ds.size ()-2) .Movement (map) If (! ds.get (ds.size ()-2). IsStatus () {/ / judge whether the game fails if (ds.get ((ds.size ()-2)) .getY ()