俄罗斯方块源码

移动开发
有背景音乐 有运动特效 每次落地产生的颜色都不一样,当然很多资源文件这里不能分享

 

完整源码:

【文件】 Fangkuai.java

  1. package com; 
  2.   
  3.   
  4. class Fangkuai { 
  5.   
  6.       
  7.      int rgb; 
  8.       
  9.      int run_x=5,run_y; 
  10.        
  11.      int d=1
  12.        
  13.      int xingzhuang[][]; 
  14.        
  15.      static final int width=20,height=20
  16.        
  17.     public static final int  xingzhuangs[][][]={ 
  18.         {{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0}, 
  19.             {0,0,0,0,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}},     
  20.                
  21.             {{0,0,1,0},{0,1,1,1},{0,0,0,0},{0,0,0,0}},//мапн 
  22.             {{0,0,0,0},{1,0,0,0},{1,1,0,0},{0,1,0,0}}, 
  23.             {{1,1,1,1},{1,0,0,0},{0,0,0,0},{0,0,0,0}},//Tпн 
  24.             {{1,1},{1,1}} 
  25.                
  26.     }; 
  27.    
  28.        

3. [文件] Mypanel.java ~ 2KB

  1. package com; 
  2.   
  3. import java.awt.Color; 
  4. import java.awt.Graphics; 
  5. import java.awt.Image; 
  6. import java.util.Random; 
  7.   
  8. import javax.swing.JPanel; 
  9.   
  10. class Mypanel extends JPanel{ 
  11.       
  12.   
  13.       
  14.     protected  Fangkuai fangkuai; 
  15.       
  16.      protected Image image; 
  17.       
  18.      protected int w, h; 
  19.       
  20.        /** 
  21.          * 画一个方块 
  22.          * @param g 
  23.          * @param curt_xing 
  24.          */ 
  25.        protected  void drawFangkui(Graphics g,int curt_xing[][]){ 
  26.   
  27.   
  28.             if(curt_xing!=null){ 
  29.                 int y=0;boolean b=false
  30.                 for (int i = 0; i < curt_xing.length; i++) { 
  31.                     for (int j = 0; j < curt_xing[i].length; j++) { 
  32.                         if(curt_xing[i][j]!=0
  33.                         {                            
  34.                             g.fill3DRect((fangkuai.run_x+j)*Fangkuai.width, (fangkuai.run_y+y)*Fangkuai.height, 
  35.                                     Fangkuai.width-1, Fangkuai.height-1,true); 
  36.                             b=true
  37.                         } 
  38.                            
  39.                     } 
  40.                     if(b) 
  41.                         y++; 
  42.                            
  43.                    
  44.                 } 
  45.                
  46.             } 
  47.          } 
  48.         /** 
  49.          * 创建一个方块 
  50.          * @return 
  51.          */ 
  52.         protected Fangkuai createNewFangkui(){ 
  53.                
  54.             int index=0
  55.             Random random=new Random(); 
  56.             Fangkuai fangkuai=new Fangkuai(); 
  57.             Color color=new Color(random.nextInt(255), 
  58.                     random.nextInt(255),random.nextInt(255));            
  59.             index=random.nextInt(4); 
  60.           
  61.             
  62.             fangkuai.xingzhuang=Fangkuai.xingzhuangs[index]; 
  63.             fangkuai.rgb=color.getRGB(); 
  64.             return fangkuai; 
  65.         } 
  66.         
  67.     @Override 
  68.     public void paint(Graphics g) { 
  69.         super.paint(g);     
  70.           
  71.         if(image!=null
  72.             g.drawImage(image, 00,w,h, null); 
  73.           
  74.         Color color=new Color(fangkuai.rgb); 
  75.         
  76.         g.setColor(color); 
  77.            drawFangkui(g,fangkuai.xingzhuang); 
  78.     } 

4. [文件] Eluo_panel.java

  1. package com; 
  2.   
  3. import java.applet.Applet; 
  4. import java.applet.AudioClip; 
  5. import java.awt.Color; 
  6. import java.awt.Graphics; 
  7. import java.awt.Image; 
  8. import java.io.File; 
  9. import java.io.FileInputStream; 
  10. import java.io.FileNotFoundException; 
  11. import java.io.IOException; 
  12. import java.net.MalformedURLException; 
  13. import java.net.URL; 
  14. import java.util.Random; 
  15.   
  16. import javax.imageio.ImageIO; 
  17. import javax.swing.JLabel; 
  18. import javax.swing.JOptionPane; 
  19.   
  20. import sun.audio.AudioPlayer; 
  21. import sun.audio.AudioStream; 
  22.   
  23. class Eluo_panel extends Mypanel implements Runnable{ 
  24.        
  25.       
  26.    int huatu[][][]; 
  27.    long now_time=0
  28.    Random random=new Random(); 
  29.    Color color=new Color(0); 
  30.  static final int MOVE_LEFT=1
  31.  static final int MOVE_RIGHT=2
  32.     
  33.  final int array_length; 
  34.  final int array_length0; 
  35.   
  36.  Mypanel next; 
  37.  int score_int; 
  38.       
  39.  AudioClip left,right,down; 
  40.  JLabel score; 
  41.    
  42.  AudioPlayer backplayer; 
  43.    
  44.  AudioStream backstream; 
  45.    
  46.  private int draw_speed=500,down_speed=1000
  47.  private long now=down_speed,last; 
  48.  Thread thread; 
  49.    
  50. public  boolean game_over=false,stop; 
  51.    public Eluo_panel(int w,int h,Mypanel next,JLabel score){ 
  52.           
  53.     this.w=w; 
  54.     this.h=h; 
  55.     this.next=next; 
  56.     this.score=score; 
  57.      
  58.     try { 
  59.         image=ImageIO.read(new File("images/panel.jpg")); 
  60.     } catch (IOException e) { 
  61.         e.printStackTrace(); 
  62.     } 
  63.     array_length=h/Fangkuai.height; 
  64.     array_length0=w/Fangkuai.width; 
  65.        initMusic(); 
  66.       
  67.          
  68.        initGame(); 
  69.    } 
  70.      
  71.    public void initGame(){ 
  72.          score_int=0
  73.         game_over=false
  74.        fangkuai=createNewFangkui(); 
  75.           
  76.         this.next.fangkuai=next.createNewFangkui(); 
  77.         next.fangkuai.run_x=2
  78.         next.fangkuai.run_y=3
  79.         
  80.       huatu=new int[array_length][array_length0][2]; 
  81.         
  82.       if(thread==null||thread.getState()==Thread.State.TERMINATED) 
  83.       { 
  84.        thread=new Thread(this); 
  85.        thread.start(); 
  86.       } 
  87.    } 
  88.    @Override 
  89.    public void paint(Graphics g) { 
  90.        super.paint(g); 
  91.   
  92.     g.drawLine(0,h-2, w,h-2); 
  93.     g.drawLine(0,00,h-2); 
  94.          
  95.        drawBack(g); 
  96.          
  97.        if(now-last>=down_speed) 
  98.        { 
  99.             moveDown(fangkuai.xingzhuang); 
  100.             last=now; 
  101.        } 
  102.        now=System.currentTimeMillis(); 
  103.    } 
  104.       
  105.    /** 
  106.     * 画背景 
  107.     * @param g 
  108.     */ 
  109.    void drawBack(Graphics g){ 
  110.   
  111.        for (int i = 0; i < huatu.length; i++) { 
  112.            for (int j = 0; j < huatu[i].length; j++) { 
  113.                Color color=new Color(huatu[i][j][1]); 
  114.                g.setColor(color); 
  115.                if(huatu[i][j][0]!=0
  116.                    g.fillRect(j*Fangkuai.width, 
  117.                         i*Fangkuai.width, Fangkuai.width-1,Fangkuai.height-1); 
  118.            } 
  119.        } 
  120.    } 
  121.       
  122.   
  123.       
  124.    /** 
  125.     * 判断是否能够向下移动 
  126.     * @param xingzhuang 
  127.     * @return 
  128.     */ 
  129.    boolean isCan_down(int xingzhuang[][]){ 
  130.           
  131.           
  132.        int y=0;boolean b=false
  133.        for (int i = 0; i < xingzhuang.length; i++) { 
  134.            for (int j = 0; j < xingzhuang[i].length; j++) { 
  135.                if(xingzhuang[i][j]!=0
  136.                { 
  137.                    b=true
  138.                    if(fangkuai.run_y+y>=array_length-1 
  139.                         ||huatu[fangkuai.run_y+y+1][fangkuai.run_x+j][0]!=0){ 
  140.                        return false
  141.                    } 
  142.                       
  143.                } 
  144.                       
  145.            } 
  146.            if(b) 
  147.                y++; 
  148.           
  149.        } 
  150.           
  151.        return true
  152.    } 
  153.    /** 
  154.     * 变形 
  155.     */ 
  156.       
  157.    public int[][] bianXing(int d,int arr[][]){ 
  158.           
  159.        if(arr==null||arr[0]==null
  160.            return null
  161.           
  162.        int arr2[][]=new int[arr.length][arr[0].length]; 
  163.           
  164.            
  165.        switch (d) { 
  166.        case 1
  167.            for (int i = 0; i < arr.length; i++) { 
  168.                for (int j = 0; j < arr[i].length; j++) { 
  169.                    arr2[j][arr[i].length-1-i]=arr[i][j];       
  170.                } 
  171.            } 
  172.            break
  173.   
  174.        default
  175.            break
  176.        } 
  177.           
  178.        for (int i = 0; i < arr2.length; i++) { 
  179.               
  180.            for (int j = 0; j < arr2[i].length; j++) { 
  181.                   
  182.                if(arr2[i][j]!=0
  183.                { 
  184.                if(fangkuai.run_x+j>array_length0-1 
  185.                        ||fangkuai.run_y+i>array_length-1||fangkuai.run_x+j<0 
  186.                    ||huatu[fangkuai.run_y+i][fangkuai.run_x+j][0]!=0
  187.                    return arr; 
  188.                } 
  189.            } 
  190.        } 
  191.           
  192.        return arr2; 
  193.           
  194.    } 
  195.      
  196.    void moveDownInterface(){ 
  197.        moveDown(fangkuai.xingzhuang); 
  198.    } 
  199.    /** 
  200.     * 向下移动 
  201.     * @param xingzhuang 
  202.     */ 
  203.    private void moveDown(int xingzhuang[][]){ 
  204.   
  205.        if(isCan_down(xingzhuang)) 
  206.            fangkuai.run_y++; 
  207.           
  208.        else 
  209.        { 
  210.            /** 
  211.             * 如果不能向下移动就把当前方块的0和1 映射到整个面板上,重新创建一个方块 
  212.             */ 
  213.            int y=0;boolean b=false
  214.            for (int i = 0; i < xingzhuang.length; i++) { 
  215.                for (int j = 0; j < xingzhuang[i].length; j++) { 
  216.                    if(xingzhuang[i][j]!=0
  217.                    { 
  218.                        huatu[fangkuai.run_y+y][fangkuai.run_x+j][0]=1
  219.                        huatu[fangkuai.run_y+y][fangkuai.run_x+j][1]=fangkuai.rgb; 
  220.                        b=true
  221.                    } 
  222.                           
  223.                } 
  224.                if(b) 
  225.                    y++; 
  226.               
  227.            } 
  228.               
  229.               
  230.            xiaoChu(); 
  231.            for (int i = 0; i < huatu[0].length; i++) { 
  232.                if(huatu[0][i][0]!=0
  233.                { 
  234.                    game_over=true
  235.                  JOptionPane.showMessageDialog(null"游戏结束"); 
  236.                } 
  237.            } 
  238.            down.play(); 
  239.            fangkuai=next.fangkuai; 
  240.            fangkuai.run_x=5
  241.            fangkuai.run_y=0
  242.              
  243.            next.fangkuai=next.createNewFangkui(); 
  244.            next.fangkuai.run_x=2
  245.            next.fangkuai.run_y=3
  246.            next.repaint(); 
  247.        } 
  248.               
  249.    } 
  250.    public void xiaoChu(){ 
  251.           
  252.        boolean xiao=false
  253.           
  254.        for (int i = huatu.length-1; i >=0; i--) { 
  255.               
  256.            xiao=false
  257.            int j=0
  258.            for ( j = 0; j < huatu[i].length; j++) { 
  259.                if(huatu[i][j][0]==0
  260.                    break
  261.            } 
  262.               
  263.            if(j==huatu[i].length) 
  264.                xiao=true
  265.               
  266.               
  267.            if(xiao){ 
  268.                score_int++; 
  269.                for ( j = i; j >0; j--) { 
  270.                    for (int j2 = 0; j2 < huatu[j].length; j2++) { 
  271.                        huatu[j][j2]=huatu[j-1][j2]; 
  272.                    } 
  273.                } 
  274.                for ( j = 0; j <huatu[0].length; j++) { 
  275.                    huatu[0][j][0]=0
  276.                } 
  277.                 i++; 
  278.                   
  279.                 score.setText(score_int+""); 
  280.            } 
  281.               
  282.        } 
  283.    } 
  284. /** 
  285. * 
  286. * @param d 
  287. * @param xingzhuang 
  288. */ 
  289.     void moveOther(int d,int xingzhuang[][]){ 
  290.           
  291.        int dx=d==MOVE_LEFT?1:-1
  292.        if(is_CanMoveOther(d, xingzhuang)){ 
  293.            fangkuai.run_x+=dx; 
  294.        } 
  295.    } 
  296.    private boolean is_CanMoveOther(int d,int xingzhuang[][]){ 
  297.           
  298.        int dx=d==MOVE_LEFT?1:-1
  299.        int y=0;boolean has=false
  300.        for (int i = 0; i < xingzhuang.length; i++) { 
  301.            has=false
  302.            for (int j = 0; j < xingzhuang[i].length; j++) { 
  303.                   
  304.                if(xingzhuang[i][j]!=0
  305.                { 
  306.                    if(d==MOVE_LEFT&&fangkuai.run_x+j>=array_length0-1 
  307.                         ||d==MOVE_RIGHT&&fangkuai.run_x+j<=0
  308.                           
  309.                            return false
  310.                    has=true
  311.                   if(huatu[fangkuai.run_y+y][fangkuai.run_x+j+dx][0]!=0){ 
  312.                       return false
  313.                   }                    
  314.                } 
  315.            } 
  316.            if(has) 
  317.                y++; 
  318.        } 
  319.       
  320.        return true
  321.    } 
  322.       
  323.    public void initMusic(){ 
  324.        URL classpath=getClass().getClassLoader().getResource(""); 
  325.        try { 
  326.             left=Applet.newAudioClip(new URL(classpath, "sounds/SOUND8.WAV")); 
  327.             down=left=Applet.newAudioClip(new URL(classpath, "sounds/SOUND104.WAV")); 
  328.         } catch (MalformedURLException e) { 
  329.             e.printStackTrace(); 
  330.         } 
  331.       
  332.        try { 
  333.         FileInputStream fileInputStream= new FileInputStream("sounds/without_you.wav"); 
  334.         backstream=new AudioStream(fileInputStream); 
  335.         backplayer=AudioPlayer.player; 
  336.         backplayer.start(backstream); 
  337.     } catch (Exception e) { 
  338.         e.printStackTrace(); 
  339.     } 
  340.       
  341.          
  342.    } 
  343.       
  344.       
  345.    @Override 
  346.    public void run() { 
  347.      while(!game_over) 
  348.      { 
  349.          if(!stop) 
  350.          this.repaint(); 
  351.          try { 
  352.            Thread.sleep(draw_speed); 
  353.        } catch (InterruptedException e) { 
  354.            e.printStackTrace(); 
  355.        } 
  356.      } 
  357.    } 
  358.       

5. [文件] Eluos.java

  1. package com; 
  2.    
  3. import java.awt.Color; 
  4. import java.awt.Graphics; 
  5. import java.awt.event.ActionEvent; 
  6. import java.awt.event.ActionListener; 
  7. import java.awt.event.KeyEvent; 
  8. import java.awt.event.KeyListener; 
  9. import java.io.File; 
  10. import java.io.IOException; 
  11.   
  12. import javax.imageio.ImageIO; 
  13. import javax.swing.ImageIcon; 
  14. import javax.swing.JButton; 
  15. import javax.swing.JFrame; 
  16. import javax.swing.JLabel; 
  17. import javax.swing.JOptionPane; 
  18. import javax.swing.JTextArea; 
  19.    
  20. public class Eluos extends JFrame{ 
  21.        
  22.        
  23.     private Eluo_panel jPanel; 
  24.       
  25.     private Mypanel panel_next; 
  26.        
  27.     private int this_width=600,this_height=600
  28.       
  29.     private int panel_width=300,panel_height=400
  30.     
  31.     JButton button,button_stop,button_help; 
  32.       
  33.     JLabel score_label,label_score; 
  34.       
  35.     public Eluos(){ 
  36.       this.setSize(this_width, this_height); 
  37.         
  38.       panel_next=new Mypanel(); 
  39.       panel_next.setBackground(Color.RED); 
  40.       panel_next.setBounds(400200200200); 
  41.         
  42.         
  43.       label_score=new JLabel("0"); 
  44.       label_score.setBounds(4501004020); 
  45.         
  46.       jPanel=new Eluo_panel(panel_width,panel_height,panel_next,label_score); 
  47.       jPanel.setBounds(22, panel_width, panel_height); 
  48.       jPanel.setBackground(Color.red); 
  49.         
  50.         
  51.        button=new JButton(new ImageIcon("images/start.png")); 
  52.       button.setBounds(4001010030); 
  53.         
  54.       button_help=new JButton("帮助"); 
  55.       button_help.setBounds(40016010030); 
  56.         
  57.       button_stop=new JButton(new ImageIcon("images/stop.png")); 
  58.      button_stop.setBounds(4006010030); 
  59.       
  60.        
  61.      score_label=new JLabel("分数"); 
  62.      score_label.setBounds(4001004020); 
  63.       
  64.      this.add(button); 
  65.      this.add(button_help); 
  66.      this.add(button_stop); 
  67.       this.setTitle("My 俄罗斯"); 
  68.       this.setLayout(null); 
  69.       this.add(jPanel); 
  70.        this.add(panel_next); 
  71.        this.add(score_label); 
  72.        this.add(label_score); 
  73.       this.setDefaultCloseOperation(EXIT_ON_CLOSE); 
  74.       this.setVisible(true); 
  75.   
  76.       addListener(); 
  77.         
  78.      
  79.     
  80.   } 
  81.       
  82.     public void addListener(){ 
  83.           
  84.           
  85.          button_help.addActionListener(new ActionListener() { 
  86.                   
  87.                 @Override 
  88.                 public void actionPerformed(ActionEvent e) { 
  89.                       
  90.                       
  91.                     String message= "游戏规则:游戏开始后,不同形状的方块会一个一个依次地随机往下掉,玩家要通过操作使一行凑满消掉,当屏幕满列时,游戏结束。\n"
  92.                             "玩家进入俄罗斯方块游戏界面后,界面如图2-1所示。以下是详细的游戏操作使用说明:\n"
  93.                             "键盘操作:→:往右移动方块。\n" + 
  94.                             "←:往左移动方块。\n"
  95.                             "↓:加快方块往下掉的速度。\n"+   
  96.                             "↑:旋转正在往下掉的方块。\n"
  97.                             "单击“重新开始”按钮则重新再来。\n"
  98.                             "单击“暂停”按钮则暂停游戏。\n"
  99.                       
  100.                     JTextArea area=new JTextArea(); 
  101.                     area.setText(message); 
  102.                     area.setSize(400,400); 
  103.                       
  104.                     JOptionPane.showMessageDialog(Eluos.this, area); 
  105.                       
  106.                       
  107.                 } 
  108.             }); 
  109.          button.addActionListener(new ActionListener() { 
  110.               
  111.             @Override 
  112.             public void actionPerformed(ActionEvent e) { 
  113.                 jPanel.initGame(); 
  114.                  Eluos.this.requestFocus(); 
  115.             } 
  116.         }); 
  117.           
  118.          button_stop.addActionListener(new ActionListener() { 
  119.                   
  120.                 @Override 
  121.                 public void actionPerformed(ActionEvent e) { 
  122.                    jPanel.stop=!jPanel.stop; 
  123.                    if(!jPanel.stop) 
  124.                        Eluos.this.requestFocus(); 
  125.                 } 
  126.             }); 
  127.           
  128.         this.addKeyListener(new KeyListener() { 
  129.               
  130.             @Override 
  131.             public void keyTyped(KeyEvent e) { 
  132.             } 
  133.                
  134.             @Override 
  135.             public void keyReleased(KeyEvent e) { 
  136.                 
  137.             } 
  138.                
  139.             @Override 
  140.             public void keyPressed(KeyEvent e) { 
  141.                   
  142.                 if(jPanel.game_over) 
  143.                     return
  144.                   switch (e.getKeyCode()) { 
  145.                   case KeyEvent.VK_LEFT: 
  146.                          
  147.                      jPanel.left.play(); 
  148.                       Eluos.this.jPanel.moveOther(Eluo_panel.MOVE_RIGHT, Eluos.this.jPanel.fangkuai.xingzhuang); 
  149.                       break
  150.                   case  KeyEvent.VK_RIGHT: 
  151.                      jPanel.left.play(); 
  152.                       Eluos.this.jPanel.moveOther(Eluo_panel.MOVE_LEFT, Eluos.this.jPanel.fangkuai.xingzhuang); 
  153.                       break
  154.                   case KeyEvent.VK_UP: 
  155.                   Eluos.this.jPanel.fangkuai.xingzhuang= 
  156.                   Eluos.this.jPanel.bianXing(Eluos.this.jPanel.fangkuai.d, Eluos.this.jPanel.fangkuai.xingzhuang); 
  157.                       break
  158.                         
  159.                   case KeyEvent.VK_DOWN: 
  160.                       Eluos.this.jPanel.moveDownInterface(); 
  161.                       break
  162.                   } 
  163.                jPanel.repaint(); 
  164.             } 
  165.         }); 
  166.       
  167.        this.requestFocusInWindow(); 
  168.          
  169.          
  170.          
  171.          
  172.     } 
  173.      
  174.       
  175.     @Override 
  176.    public void paint(Graphics g) { 
  177.         super.paint(g); 
  178.       
  179.         try { 
  180.             g.drawImage(ImageIO.read(new File("images/c.jpg")) 
  181.                     , 00null); 
  182.         } catch (IOException e) { 
  183.             e.printStackTrace(); 
  184.         } 
  185.           
  186.           
  187.         panel_next.repaint(); 
  188.         button.repaint(); 
  189.         button_stop.repaint(); 
  190.         button_help.repaint(); 
  191.         score_label.repaint(); 
  192.         label_score.repaint(); 
  193.     } 
  194.   public static void main(String[] args) { 
  195.        
  196.       new Eluos(); 
  197.        
  198.        
责任编辑:chenqingxiang 来源: 网络整理
相关推荐

2011-06-13 18:21:12

2014-10-08 10:04:14

代码解释俄罗斯方块

2020-02-27 13:43:14

Emacs俄罗斯方块应用

2021-01-12 12:16:55

鸿蒙HarmonyOS游戏

2021-12-29 11:56:16

Linux俄罗斯方块

2020-05-19 17:26:21

Python俄罗斯方块游戏开发

2014-05-26 10:07:18

Javascript俄罗斯方块

2016-06-13 10:21:49

二维码条形码二进制

2015-04-28 09:21:28

JSJS俄罗斯方块游戏帝国

2023-09-26 08:51:29

PygamePython语言

2023-09-25 12:35:27

Python

2020-12-11 12:45:04

鸿蒙Hi3861游戏

2014-06-09 12:47:35

俄罗斯方块

2012-11-05 10:50:50

程序员万圣节俄罗斯方块

2020-12-17 10:02:16

鸿蒙Hi3861开发板

2009-06-08 09:59:24

谷歌俄罗斯方块版权

2011-11-17 16:14:25

Jscex

2023-10-17 10:20:53

VueReact

2022-11-29 16:35:02

Tetris鸿蒙

2011-07-06 10:12:26

Objective-CCSSJavaScript
点赞
收藏

51CTO技术栈公众号