I learned this technique last year and haven't written it yet. Now I have taken the time to write a Tetris game.
Only simple new game, pause, continue, points function. It simply realizes the classic Russian functions.
I won’t introduce it anymore. If you are interested, run it yourself and post a picture later.
Code:
package cn.hncu;import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;import javax.swing.JFrame;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.Timer;public class Tetris extends JFrame{ public static void main(String[] args) { Tetris te = new Tetris(); te.setVisible(true); //If you add an edit box to the interface that will seize the focus, you need to use the following code //te.requestFocus(true);//Let the game panel get focus--Get the keyboard's listening} private TetrisPanel tp; JMenuItem itemPause; JMenuItem itemContinue; public Tetris() { this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setLocation(700, 200); this.setSize(220, 275); this.setResizable(false); tp = new TetrisPanel(); this.getContentPane().add(tp); //Add menu JMenuBar menubar = new JMenuBar(); this.setJMenuBar(menubar); JMenu menuGame = new JMenu("Game"); menubar.add(menuGame); JMenuItem itemNew = new JMenuItem("New Game"); itemNew.setActionCommand("new"); itemPause = new JMenuItem("Pause"); itemPause.setActionCommand("pause"); itemContinue = new JMenuItem("Continue"); itemContinue.setActionCommand("continue"); itemContinue.setEnabled(false); menuGame.add(itemNew); menuGame.add(itemPause); menuGame.add(itemContinue); MenuListener menuListener = new MenuListener(); itemNew.addActionListener(menuListener); itemPause.addActionListener(menuListener); itemContinue.addActionListener(menuListener); //Let the entire JFrame add keyboard listening this.addKeyListener(tp.listener ); } class MenuListener implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { //Play new game if(e.getActionCommand().equals("new")){ tp.newGame(); } if(e.getActionCommand().equals("pause")){ timer.stop(); itemContinue.setEnabled(true); itemPause.setEnabled(false); } if(e.getActionCommand().equals("continue")){ timer.restart(); itemContinue.setEnabled(false); itemPause.setEnabled(true); } } } private Timer timer; class TetrisPanel extends JPanel{ // Shape of the square: // The first dimension represents the block type (including 7 types: S, Z, L, J, I, O, T) // The second dimension represents the number of rotations // The third and fourth dimension represents the block matrix // shapes[type][turnState][i] i--> block[i/4][i%4] int shapes[][][] = new int[][][] { /* * Template{ {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, {0,0,0,0,0,0,0,0,0,0, * 0,0,0,0,0,0,0,0,0,0} } */ // I (※Switch the bar in version 1 from line 1 to line 2) { { 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0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 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 }, { 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } } }; private int blockType;//Block type private int turnState;//Rotation state private int x;//Block position x--Column position-Column number private int y;//Block position y--row position--row number private int map[][]=new int[13][23];//Map: 12 columns and 22 rows. To prevent cross-border, the array is opened into: 13 columns and 23 rows private int delay=1000; public TimerKeyLister listener=new TimerKeyLister(); private int score=0;//Score public TetrisPanel(){ newGame(); nextBlock(); //timer = new Timer(delay,listener); //timer.start(); } public void newGame() { blockType = (int)(Math.random()*1000)%7; turnState = (int)(Math.random()*1000)%4; x=4; y=0; for( int i=0;i<12;i++){//Line for(int j=0;j<21;j++){//Walking if(i==0 || i==11){//3 is the grid of the interface border map[i][j]=3;//Really, just use integers other than 0 and 1, but using 3 here has a special effect - use }else{ map[i][j]=0; } } map[i][21]=3;//3 is the grid of the interface border} if(timer!=null){ timer.stop(); } delay=1000; timer = new Timer(delay,listener); timer.start(); } private void nextBlock() { blockType = (int)(Math.random()*1000)%7; turnState = (int)(Math.random()*1000)%4; x=4; y=0; //game Over if(crash(x,y,blockType,turnState)==0){ timer.stop(); int option = JOptionPane.showConfirmDialog(this, "Game Over!!, do you still dare to come..."); if (option == JOptionPane.OK_OPTION) { newGame(); } else if (option == JOptionPane.NO_OPTION) { System.exit(0); } } } private void down() { if( crash(x,y+1,blockType,turnState)==0 ){//Note that y+1 is used here to determine whether the corresponding position in the map is a stacked block or frame after the block is dropped by one square;//Save the information of the current block to the map nextBlock(); }else{ y++; } repaint(); } private void left() { if(x>=0){ x -= crash(x-1,y,blockType,turnState); } repaint(); } private void right() { if(x<8){ x += crash(x+1,y,blockType,turnState); } repaint(); } private void turn() { if(crash(x,y,blockType,(turnState+1)%4)==1 ){ turnState = (turnState+1)%4; } repaint(); } private void add(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; } } } tryDelLine(); } //Eliminate private void tryDelLine(){ for(int b=0;b<21;b++){ int c=1; for(int a=0;a<12;a++){ c &= map[a][b];//All are 1, the result of c is 1 } if(c==1){//There is a line that needs to be eliminated//Move one line down in sequence for(int d=b; d>0; d--){ for(int e=0;e<11;e++){ map[e][d] = map[e][d-1]; } } //Add points score +=100; delay /=1.05; timer.setDelay(delay); } } } // Parameter example: 4,3,2,3 //Judge whether there is a collision private int crash(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] ==3 ) ){// } if( (shapes[blockType][turnState][a*4+b] & map[x+b+1][y+a]) ==1 ){ return 0;//collision} } } return 1;//No collision} @Override public void paint(Graphics g) {// blockType =6;// turnState = 3;// x=4;// y=6; super.paint(g);// Eliminate afterimage g.setColor(new Color(153,51,205)); // Draw the current block for(int j=0;j<16;j++){ if(shapes[blockType][turnState][j]==1){ g.fillRect((j%4+x+1)*10, (j/4+y)*10, 10, 10); g.setColor(Color.cyan); g.drawRect((j%4+x+1)*10, (j/4+y)*10, 10, 10); g.setColor(new Color(153,51,205)); } } //Draw the interface frame and stack blocks---the entire map g.setColor(Color.red); for( int i=0;i<12;i++){//Walking for(int j=0;j<22;j++){//Walking if(map[i][j]==3){ g.drawRect(i*10, j*10, 10, 10); }else if(map[i][j]==1){ g.fillRect(i*10, j*10, 10, 10); g.setColor(Color.GREEN); g.drawRect(i*10, j*10, 10, 10); g.setColor(Color.red); } } } //Show the score, and the layout is beautiful, add something to the interface // Draw the right part of the square area g.setColor(Color.red); g.setFont(new Font("aa", Font.BOLD, 11)); g.drawString("score=" + score, 130, 20); g.setFont(new Font("aa", Font.PLAIN, 13)); g.setColor(Color.blue); g.drawString("Reject pirated games,", 125, 70); g.drawString("Be careful of self-protection. ", 125, 90); g.drawString("Beware of being deceived.", 125, 110); g.drawString("Moderate game is good for the brain,", 125, 130); g.drawString("Add to play is harmful to the body.", 125, 150); g.drawString("Schedule time reasonably,", 125, 170); g.drawString("Enjoy a healthy life.", 125, 190); } class TimerKeyLister extends KeyAdapter implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { down(); } @Override public void keyPressed(KeyEvent e) { switch(e.getKeyCode()){ case KeyEvent.VK_DOWN: down(); break; case KeyEvent.VK_LEFT: left();break; case KeyEvent.VK_RIGHT: right();break; case KeyEvent.VK_UP: turn();break; case KeyEvent.VK_F1: plug(); case KeyEvent.VK_F2: time(); } } public void plug() { score+=100; } public void time() { delay =1000; timer.setDelay(delay); } }}Running interface:
For more exciting games, please refer to the special topic "Java Classic Games"
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.