Java homework - teach you how to write Tetris

content

Design of Tetris cube body:

Control main logic block:

Visual interface:

Cover Design:

Game Chassis:

Game main interface:


Link to source code at the end of the article

Tetris:

        Plates of different shapes composed of small squares fall from the top of the screen one after another. Players can make one or several complete pieces at the bottom of the screen by adjusting the position and direction of the plates. These complete bars will disappear immediately to make room for the newly fallen tiles, and at the same time, the player is rewarded with points. The blocks that have not been eliminated continue to pile up. Once they reach the top of the screen, the player loses and the game ends.    

        The author believes that many friends have played or heard of this little game. Today, the author will take you through the writing process (the writing time is a bit long, there may be some oversights, and criticisms and corrections are welcome).

Design of Tetris cube body:

//Blocks.java

public class Blocks {
   int[][] t={  {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,0},
                {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},
    };
   int[][] l={                          { 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
					{ 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
					{ 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
					{ 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } };
   int[][] m={                          { 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0 },
					{ 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
					{ 1, 1, 0, 0, 0, 1, 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 } };    
   
   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, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0 } };
   
}
           

Control main logic block:

//Controller.java

import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;


public class Controller extends Thread{
    GamePanel gp;
    
    int i;
    int hua;
    int fen=0;
 int[][] blocks;
    Random r=new Random();
    
    int status=r.nextInt(4);
int po;
    //int[] block;
    public Controller(GamePanel gp){
        this.gp=gp;
        gp.status=r.nextInt(4);
         status=r.nextInt(4);
         hua=r.nextInt(4);
       if(hua==1) blocks =new Blocks().t;
         else if (hua==2) blocks =new Blocks().l;
         else if(hua==3) blocks =new Blocks().m;
         else blocks =new Blocks().i;
        gp.block=blocks[status];
        start();
    }
    
    public boolean isOverStep(int x,int y,int [] block){
        boolean flag=false;
        for(int i=0;i<4;i++)
            for(int j=0;j<4;j++){
               if(block[4*i+j]==1 &&  ((x+j*20)<0 || (x+j*20)>180 || (y+i*20)>=400 || gp.fixBlocks[y/20+i][x/20+j]==1)  ){
            flag=true;
        }
            }
        return flag;
    }
    
    public void theBlockFinish(){
         for(int i=0;i<4;i++)
            for(int j=0;j<4;j++)
        if(gp.block[4*i+j]==1){
           gp.fixBlocks[gp.y/20+i][gp.x/20+j]=1;
        }
        
         nextgo();
        
    }
    
    
       public void nextgo(){        
        
         gp.x=40;gp.y=0;
         status=r.nextInt(4);
         hua=r.nextInt(4);
         if(hua==1) blocks =new Blocks().t;
         else if (hua==2) blocks =new Blocks().l;
         else if(hua==3) blocks =new Blocks().m;
         else blocks =new Blocks().i;
         
         xiaohang();
          
         
    }
    
       public void xiaohang(){
           int m=0;
         for(int i=19;i>=0;i--){
             m=0;
             for(int j=0;j<=9;j++){
             m=m+gp.fixBlocks[i][j];
             }  
             if(m==10){
             for(int s=0;s<=9;s++) {
                 gp.fixBlocks[i][s]=0;    
                 
                 for(int k=i;k>0;k--){
                  gp.fixBlocks[k][s]=gp.fixBlocks[k-1][s];
                 }
             }
             gp.fen=gp.fen+1;
             
             
             }
                 }
         
           up();
       }
       
       
      
   public void up(){
       if(!isOverStep(gp.x-20, gp.y+20, gp.block)){
       
        if (hua==1) gp.block=new Blocks().t[status];
        else if (hua==2) gp.block=new Blocks().l[status];
        else if(hua==3) gp.block=new Blocks().m[status];
        else gp.block=new Blocks().i[status];
       
       gp.repaint();
       status++;
       if (status==blocks.length) status=0;
       }
   }
    
    
    
    public void left(){
        if(!isOverStep(gp.x-20, gp.y, gp.block)){
        gp.x-=20;
        gp.repaint();
        }
        
    }
    public void right(){if(!isOverStep(gp.x+20, gp.y, gp.block)){gp.x+=20;gp.repaint();}}
    public void down(){if(!isOverStep(gp.x, gp.y+20, gp.block)){gp.y+=20;gp.repaint();}else {theBlockFinish();} }
    public int cedown(){if(!isOverStep(gp.x, gp.y+20, gp.block)){gp.y+=20;gp.repaint();return 1;}else {theBlockFinish();return 0;} }
    public void gameover(){
        JOptionPane.showMessageDialog(null, "GAME OVER");
    }
    
    public void run(){
     

    while(!isOverStep(gp.x, gp.y+20, gp.block)){
       
        try {
           
            {gp.y+=20;
            gp.repaint();
            }
            sleep(1000);
        
        } catch (InterruptedException ex) {
            Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
        }
         
 
      
     
      
  
}
    }}

Visual interface:

Cover Design:

//FenMian.java

public class FenMian extends javax.swing.JPanel {
int panduan=0;
    /**
     * Creates new form FenMian
     */
    public FenMian() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jButton1 = new javax.swing.JButton();

        setBackground(new java.awt.Color(255, 102, 102));

        jButton1.setText("jButton1");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(161, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addGap(142, 142, 142))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(89, 89, 89)
                .addComponent(jButton1)
                .addContainerGap(184, Short.MAX_VALUE))
        );
    }// </editor-fold>                        

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {                                      
 panduan=1;
 jButton1.setVisible(false);

        // TODO add your handling code here:
    }                                     


    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    // End of variables declaration                   
}

Game Chassis:

//GamePanel.java

import java.awt.Color;
import java.awt.Graphics;


public class GamePanel extends javax.swing.JPanel {
    MainFrame main;
    int panduan=0;
    int status;
    int x=40,y=0;
    int i;
    int flag=1;
    int jia=0;
    int fen=0;
    int[] block;
    int[][] fixBlocks=new int[20][10];
    /**
     * Creates new form GamePanel
     */
    public GamePanel() {
        initComponents();
    }
    public void drawBlock(Graphics g){
      
        for(int i=0;i<4;i++){
            for(int j=0;j<4;j++){
                if(block[i*4+j]==1) g.fill3DRect(x+j*20,y+i*20, 20, 20,true);
            }
        }
       
    }
  

    
    
    
    public  void drawFixBlocks(Graphics g){
         for(int i=0;i<20;i++){
            for(int j=0;j<10;j++){
                if(fixBlocks[i][j]==1) g.fill3DRect(j*20,i*20, 20, 20,true);
            }
        }
    }
   
  
    
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        g.setColor(Color.BLUE);
        drawBlock(g);
        g.setColor(Color.GRAY);
        drawFixBlocks(g);
        String ma=String.valueOf(fen*10);
       g.drawString("score=" + ma, 125, 10);
        
        
       
        //g.fill3DRect(x, y, 20, 20,true);
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        setBackground(new java.awt.Color(255, 255, 204));
        setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                formKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 396, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 296, Short.MAX_VALUE)
        );
    }// </editor-fold>                        

    private void formKeyPressed(java.awt.event.KeyEvent evt) {                                
        
        // TODO add your handling code here:
    }                               


    // Variables declaration - do not modify                     
    // End of variables declaration                   

    /**
     * @param jLabel1 the jLabel1 to set
     */
    
}

Game main interface:

//MainFrame.java

public class MainFrame extends javax.swing.JFrame {
Controller controller;
int i;
int jia=0;
int  panduan=0;
 String ma=String.valueOf(jia*10);
    /**
     * Creates new form MainFrame
     */
    public MainFrame() {
        initComponents();
        FenMian fm=new FenMian();
        
       
        
        
        
      
       
        GamePanel gp=new GamePanel();
       controller=new Controller(gp);
        gp.setSize(200, 400);
        gp.setLocation(50, 50);
        this.setSize(400, 600);
        this.getContentPane().add(gp);  //将panel放入frame中
        setTitle("yyf nb");
      
       
        
        if(!controller.isOverStep(gp.x+20, gp.y+20, gp.block)) {gp.x=40;gp.y=0; controller=new Controller(gp);}
    }

    
  
    
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jCheckBoxMenuItem1 = new javax.swing.JCheckBoxMenuItem();
        jMenu2 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                formKeyPressed(evt);
            }
        });

        jLabel2.setText("score");

        jMenu1.setText("菜单");

        jCheckBoxMenuItem1.setSelected(true);
        jCheckBoxMenuItem1.setText("开始");
        jMenu1.add(jCheckBoxMenuItem1);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("选项");
        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(213, Short.MAX_VALUE)
                .addComponent(jLabel2)
                .addGap(147, 147, 147))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addComponent(jLabel2)
                .addContainerGap(229, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    private void formKeyPressed(java.awt.event.KeyEvent evt) {                                
    switch(evt.getKeyCode()){
        case 37: controller.left();break;
        case 40: {controller.down();};break;
        case 39: controller.right();break; 
        case 38: controller.up(); break;
        
    }
        
        // TODO add your handling code here:
    }                               

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MainFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JCheckBoxMenuItem jCheckBoxMenuItem1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    // End of variables declaration                   

    /**
     * @param jLabel1 the jLabel1 to set
     */
 
}

That's it for this issue, welcome to leave a message for discussion.

Source code link: Tetris source code, please see my blog for details - Java document resources - CSDN download

Guess you like

Origin blog.csdn.net/yyfloveqcw/article/details/123877884