Original: Java Swing development of short and concise Tetris game source code download, only 300 lines of code
Source code: Http://www.zuidaima.com/share/1550463495146496.htm
Java Swing development of short and short Tetris game source code download, only 300 lines of code, a long time ago to find a swing implementation of the Russian block, the shorter, the algorithm is worth a look
Verified code available, indeed short and concise, worth downloading.
Package Com.zuidaima.swing.game;import Java.awt.*;import Java.awt.event.actionevent;import Java.awt.event.actionlistener;import Java.awt.event.keyevent;import Java.awt.event.keylistener;import javax.swing . *;import javax.swing.timer;/*** @author http://www.zuidaima.com**/public class Tetris extends JFrame {public Tetris () {Tetrisblok a = new Tetrisblok (); Addkeylistener (a); Add (a); } public static void Main (string[] args) {Tetris frame = new Tetris (); JMenuBar menu = new JMenuBar (); Frame.setjmenubar (menu); JMenu game = new JMenu ("Games"); JMenuItem newgame = Game.add ("New Game"); JMenuItem pause = Game.add ("pause"); JMenuItem goon = Game.add ("continue"); JMenuItem exit = Game.add ("Exit"); JMenu helper = new JMenu ("help"); JMenuItem about = Help.add ("about"); Menu.add (game); Menu.add (Help); Frame.setlocationrelativeto (NULL); Frame.setdefaultcloseoperation (Jframe.exit_on_close); Frame.setsize (220, 275); Frame.settitle ("Tetris beta"); Frame.setundecorated (TRUE); Frame.setvisible (TRUE); Frame.setresizable (FALSE); }}//Create a Tetris class Tetrisblok extends JPanel implements KeyListener {//Blocktype for block type//turnstate for block status PR ivate int blocktype; private int score = 0; private int turnstate; private int x; private int y; private int i = 0; int j = 0; int flag = 0; Define the blocks that have been dropped x=0-11,y=0-21; int[][] map = new INT[13][23]; The shape of the block the first group represents the block type has S, Z, L, J, I, O, T 7 kinds of second group representing rotation several times the 34th group is the block matrix private final int shapes[][][] = new int[][][] {//I {0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0}, "{0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1 , 0, 0}, {0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 1, 0, 0, 0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0, 0},//t {0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0}, {1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 , 0, 0}, {0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0}}; Method for generating a new block public void Newblock () {blocktype = (int) (Math.random () * 1000)% 7; Turnstate = (int) (Math.random () * 1000)% 4; x = 4; y = 0; if (Gameover (x, y) = = 1) {newmap (); Drawwall (); Score = 0; Joptionpane.showmEssagedialog (NULL, "GAME over"); }}//Draw fence public void Drawwall () {for (i = 0; i <; i++) {map[i][21] = 2; } for (j = 0; J <; J + +) {Map[11][j] = 2; MAP[0][J] = 2; }}//Initialize map public void Newmap () {for (i = 0; i < n; i++) {for (j = 0; J <; J + +) { MAP[I][J] = 0; }}}//Initialize constructor method Tetrisblok () {newblock (); Newmap (); Drawwall (); Timer timer = new Timer (+, new TimerListener ()); Timer.start (); }//Rotation method public void turn () {int tempturnstate = turnstate; Turnstate = (turnstate + 1)% 4; if (Blow (x, Y, Blocktype, turnstate) = = 1) {} if (Blow (x, Y, Blocktype, turnstate) = = 0) {TurnS Tate = Tempturnstate; } repaint (); }//Left Shift method public void Ieft () {if (Blow (X-1, y, Blocktype, turnstate) = = 1) { x = x-1; } ; Repaint (); }//Right-move the method public void-() {if (Blow (x + 1, y, blocktype, turnstate) = = 1) {x = x + 1; } ; Repaint (); }//Drop method public void down () {if (Blow (x, y + 1, blocktype, turnstate) = = 1) {y = y + 1; Delline (); } ; if (Blow (x, y + 1, blocktype, turnstate) = = 0) {Add (x, Y, Blocktype, turnstate); Newblock (); Delline (); } ; Repaint (); }//is a valid method public int blow (int x, int y, int blocktype, int. turnstate) {for (int a = 0; a < 4; a++) { for (int b = 0; b < 4; b++) {if ((Shapes[blocktype][turnstate][a * 4 + b] = = 1) && ( Map[x + B + 1][y + a] = = 1)) | | ((Shapes[blocktype][turnstate][a * 4 + b] = = 1) && (map[x + B + 1][y + a] = = 2))) { return 0; }}} return 1; }//The method of eliminating the line public void Delline () {int c = 0; for (int b = 0, b < b++) {for (int a = 0; a < a++) {if (map[a][b] = = 1) { c = c + 1; if (c = = ten) {score + = 10; for (int d = B, D > 0; d--) {for (int e = 0; e < one; e++) { MAP[E][D] = map[e][d-1]; }}}}} c = 0; }}//Judge the method you hang public int gameover (int x, int y) {if (Blow (x, Y, Blocktype, turnstate) = = 0) { return 1; } return 0; }//Put the current add map public void Add (int x, int y, int blocktype, int turnstate) {int j = 0; for (int a = 0, a < 4; a++) {for (int b =0; b < 4; b++) {if (map[x + B + 1][y + A] = = 0) {map[x + B + 1][y + A] = Shapes[blocktype][turns TATE][J]; } ; j + +; }}}//The method of drawing squares public void paintcomponent (Graphics g) {super.paintcomponent (g); Draw the current square for (j = 0; J <; J + +) {if (shapes[blocktype][turnstate][j] = = 1) {G.fill Rect ((j% 4 + x + 1) * Ten, (J/4 + y) * 10, 10, 10); }}//Draw the fixed box for (j = 0; J <; J + +) {for (i = 0; i < i++) { if (map[i][j] = = 1) {G.fillrect (i *, J * 10, 10, 10); } if (map[i][j] = = 2) {G.drawrect (i *, J * 10, 10, 10); }}} g.drawstring ("score=" + score, 125, 10); g.DrawString ("Boycott Bad Game,", 125, 50); g.DrawString ("Reject pirated games. ", 125, 70); G.draWString ("Pay attention to self-protection,", 125, 90); g.DrawString ("Beware of being fooled. ", 125, 110); g.DrawString ("Moderate game of the brain,", 125, 130); g.DrawString ("Addicted game kills. ", 125, 150); g.DrawString ("Reasonable arrangement of time,", 125, 170); g.DrawString ("Enjoy a healthy life. ", 125, 190); }//keyboard listener public void keypressed (KeyEvent e) {switch (E.getkeycode ()) {keyevent.vk_down: Down (); Break Case KeyEvent.VK_UP:turn (); Break Case KeyEvent.VK_RIGHT:right (); Break Case KeyEvent.VK_LEFT:left (); Break }}//Useless public void keyreleased (KeyEvent e) {}//useless public void keytyped (KeyEvent e) {}//Timing Monitor/*** @author http://www.zuidaima.com**/class TimerListener implements ActionListener {public void Actionperf Ormed (ActionEvent e) {repaint (); if (Blow (x, y + 1, blocktype, turnstate) = = 1) {y = y + 1; Delline(); } ; if (Blow (x, y + 1, blocktype, turnstate) = = 0) {if (flag = = 1) {Add (x, Y, Blocktype, t Urnstate); Delline (); Newblock (); Flag = 0; } flag = 1; } ; } }}
Java Swing develops short and concise Tetris Games source code download, only 300 lines of code