91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Java怎樣實現俄羅斯方塊小游戲源碼

發布時間:2022-01-19 09:10:35 來源:億速云 閱讀:146 作者:kk 欄目:開發技術

這篇文章主要為大家分析了Java怎樣實現俄羅斯方塊小游戲源碼的相關知識點,內容詳細易懂,操作細節合理,具有一定參考價值。如果感興趣的話,不妨跟著跟隨小編一起來看看,下面跟著小編一起深入學習“Java怎樣實現俄羅斯方塊小游戲源碼”的知識吧。

一、最終效果

Java怎樣實現俄羅斯方塊小游戲源碼

二、功能需求

1、 在二維平面里面用各種隨機產生的方塊堆積木,每滿一行消去一行,當達到頂部時,游戲結束。
2、 通過方向鍵來控制方塊轉動,左移,右移和直落。
3、 方塊下落統一設置藍色,接觸底部變粉色。
4、 計算分數,分數是由方塊的類型決定的,每堆積一個方塊就把分數累加到總分中。
5、 游戲有開始、重新開始、降低提高級數(速度)、暫停、退出

三、程序實現

這個是最基礎的方塊素材

package 俄羅斯方塊;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*; 
import java.applet.*;
import java.lang.String.*;
import java.lang.*;
import java.io.*;

public class Block extends JPanel implements ActionListener,KeyListener//應該是繼承JPanel
{
    static Button but[] = new Button[6];
    static Button noStop = new Button("取 消 暫 停");
    static Label scoreLab = new Label("分數:");
    static Label infoLab = new Label("提示:");
    static Label speedLab = new Label("級數:");
    static Label scoreTex = new Label("0");
    static Label infoTex = new Label(" ");
    static Label speedTex = new Label("1");
    
    static JFrame jf = new JFrame();
    static MyTimer timer; 
    static ImageIcon icon=new ImageIcon("resource/Block.jpg");
    static JMenuBar mb = new JMenuBar();
    static JMenu menu0 = new JMenu("游  戲 ");
    static JMenu menu1 = new JMenu("幫  助 ");
    static JMenuItem mi0 = new JMenuItem("新 游 戲");
    static JMenuItem mi1 = new JMenuItem("退  出");
    static JMenuItem mi1_0 = new JMenuItem("關  于");
   static JDialog dlg_1;
    static JTextArea dlg_1_text = new JTextArea();
    static int startSign = 0;//游戲開始標志 0 未開始 1 開始 2 暫停
    static String butLab[] = {"開 始 游 戲","重 新 開 始","降 低 級 數","提 高 級 數","游 戲 暫 停","退 出 游 戲"};
    static int game_body[][] = new int[19][10];
    static int game_sign_x[] = new int[4];//用于記錄4個方格的水平位置
    static int game_sign_y[] = new int[4];//用于記錄4個方格的垂直位置
    static boolean downSign = false;//是否落下
    static int blockNumber = 1;//磚塊的編號
    static int gameScore = 0;//游戲分數
    static int speedMark = 1;
    
    public static void main(String args[]) 
    {
        Block myBlock = new Block();
        mb.add(menu0);
        mb.add(menu1);
        menu0.add(mi0);
        menu0.add(mi1);
        menu1.add(mi1_0);
        jf.setJMenuBar(mb);    
        
        myBlock.init();
        jf.add(myBlock);
        jf.setSize(565,501);
        jf.setResizable(false);
        jf.setTitle("俄羅斯方塊");
        jf.setIconImage(icon.getImage());
        jf.setLocation(200,100);
        jf.show();
        timer = new MyTimer(myBlock); //啟動線程
       timer.setDaemon(true); 
       timer.start();
       timer.suspend();
    }
    public void init()
    {
       setLayout(null);
       for(int i = 0;i < 6;i++)
       {
           but[i] = new Button(butLab[i]);
           add(but[i]);
           but[i].addActionListener(this);
           but[i].addKeyListener(this);
           but[i].setBounds(360,(240 + 30 * i),160,25);
       }
       
       add(scoreLab);
       add(scoreTex);
       add(speedLab);
       add(speedTex);
       add(infoLab);
       add(infoTex);
       add(scoreLab);
       scoreLab.setBounds(320,15,30,20);
       scoreTex.setBounds(360,15,160,20);
        scoreTex.setBackground(Color.white);
        speedLab.setBounds(320,45,30,20);
        speedTex.setBounds(360,45,160,20);
        speedTex.setBackground(Color.white);
        
        but[1].setEnabled(false);
        but[4].setEnabled(false);
        
        infoLab.setBounds(320,75,30,20);
        infoTex.setBounds(360,75,160,20);
        infoTex.setBackground(Color.white);
        noStop.setBounds(360,360,160,25);
        noStop.addActionListener(this);
        noStop.addKeyListener(this);
        mi0.addActionListener(this);
        mi1.addActionListener(this);
        mi1_0.addActionListener(this);
        num_csh_game();
        rand_block();
   }
   
   public void actionPerformed(ActionEvent e)
   {
       if(e.getSource() == but[0])//開始游戲
       {
           startSign = 1;
           infoTex.setText("游戲已經開始!");
           but[0].setEnabled(false);
           but[1].setEnabled(true);
            but[4].setEnabled(true);
            timer.resume(); 
       }
       if(e.getSource() == but[1]||e.getSource() == mi0)//重新開始游戲
       {
           startSign = 0;
           gameScore = 0;
           timer.suspend();
           num_csh_restart();
           repaint();
           rand_block();
           scoreTex.setText("0");
           infoTex.setText("新游戲!");
           but[0].setEnabled(true);
           but[1].setEnabled(false);
            but[4].setEnabled(false);
       }
       if(e.getSource() == but[2])//降低級數
       {
           infoTex.setText("降低級數!");
           speedMark--;
           if(speedMark <= 1)
           {
               speedMark = 1;
               infoTex.setText("已經是最低級數!");
           }
           speedTex.setText(speedMark + "");
       }
       if(e.getSource() == but[3])//提高級數
       {
           infoTex.setText("提高級數!");
           speedMark++;
           if(speedMark >= 9)
           {
               speedMark = 9;
               infoTex.setText("已經是最高級數!");
           }
           speedTex.setText(speedMark + "");
       }
       if(e.getSource() == but[4])//游戲暫停
       {
           this.add(noStop);
           this.remove(but[4]);
           infoTex.setText("游戲暫停!");
           timer.suspend();
       }
       if(e.getSource() == noStop)//取消暫停
       {
           this.remove(noStop);
           this.add(but[4]);
           infoTex.setText("繼續游戲!");
           timer.resume();
       }
       if(e.getSource() == but[5]||e.getSource() == mi1)//退出游戲
       {
           jf.dispose();
       }
       if(e.getSource() == mi1_0)//退出游戲
       {
           dlg_1 = new JDialog(jf,"關 于");
            try{
                FileInputStream io = new FileInputStream("resource/guanyu.txt");//得到路徑
                byte a[] = new byte[io.available()];
                io.read(a);
                io.close();
                String str = new String(a);
                dlg_1_text.setText(str);
                }
                catch(Exception g){}
                dlg_1_text.setEditable(false);
               dlg_1.add(dlg_1_text);
                dlg_1.pack();
               dlg_1.setResizable(false);
               dlg_1.setSize(200, 120);
               dlg_1.setLocation(400, 240);
               dlg_1.show();
       }
   }
   
   public void rand_block()//隨機產生磚塊
   {
       int num;
        num = (int)(Math.random() * 6) + 1;//產生0~6之間的隨機數
        blockNumber = num;
        switch(blockNumber)
        {
            case 1: block1(); blockNumber = 1; break;
            case 2: block2(); blockNumber = 2; break;
            case 3: block3(); blockNumber = 3; break;
            case 4: block4(); blockNumber = 4; break;
            case 5: block5(); blockNumber = 5; break;
            case 6: block6(); blockNumber = 6; break;
            case 7: block7(); blockNumber = 7; break;
        }
   } 
   
   public void change_body(int blockNumber)//改變磚塊狀態
   {
       dingwei();
       if(blockNumber == 1&&downSign == false)//變換長條2種情況
       {
           if(game_sign_y[0] == game_sign_y[1]&&game_sign_y[3] <= 16)//說明長條是橫著的
           {
               if(game_body[game_sign_y[0] - 1][game_sign_x[0] + 1] != 2&&game_body[game_sign_y[3] + 2][game_sign_x[3] - 2] != 2)
               {
                   num_csh_game();
                   game_body[game_sign_y[0] - 1][game_sign_x[0] + 1] = 1;
                   game_body[game_sign_y[1]][game_sign_x[1]] = 1;
                   game_body[game_sign_y[2] + 1][game_sign_x[2] - 1] = 1;
                   game_body[game_sign_y[3] + 2][game_sign_x[3] - 2] = 1;
                   infoTex.setText("游戲進行中!");
                   repaint();
               }
           }
           if(game_sign_x[0] == game_sign_x[1]&&game_sign_x[0] >= 1&&game_sign_x[3] <= 7)//說明長條是豎著的
           {
               if(game_body[game_sign_y[0] + 1][game_sign_x[0]-1] != 2&&game_body[game_sign_y[3] - 2][game_sign_x[3] + 2] != 2)
               {
                   num_csh_game();
                   game_body[game_sign_y[0] + 1][game_sign_x[0] - 1] = 1;
                   game_body[game_sign_y[1]][game_sign_x[1]]=1;
                   game_body[game_sign_y[2] - 1][game_sign_x[2] + 1] = 1;
                   game_body[game_sign_y[3] - 2][game_sign_x[3] + 2] = 1;
                   infoTex.setText("游戲進行中!");
                   repaint();
               }
           }
       }
       if(blockNumber == 3&&downSign == false)//變換轉彎1有4種情況
       {
           if(game_sign_x[0] == game_sign_x[1]&&game_sign_x[0] == game_sign_x[2]&&game_sign_y[2] == game_sign_y[3]&&game_sign_x[0] >= 1)
           {
               if(game_body[game_sign_y[0] + 1][game_sign_x[0] - 1] != 2&&game_body[game_sign_y[2] - 1][game_sign_x[2] + 1] != 2&&game_body[game_sign_y[3] - 2][game_sign_x[3]] != 2)
               {
                   num_csh_game();
                   game_body[game_sign_y[0] + 1][game_sign_x[0] - 1] = 1;
                   game_body[game_sign_y[1]][game_sign_x[1]] = 1;
                   game_body[game_sign_y[2] - 1][game_sign_x[2] + 1] = 1;
                   game_body[game_sign_y[3] - 2][game_sign_x[3]] = 1;
                   infoTex.setText("游戲進行中!");
                   repaint();
               }    
           }
           if(game_sign_y[1] == game_sign_y[2]&&game_sign_y[2] == game_sign_y[3]&&game_sign_x[0] == game_sign_x[3]&&game_sign_y[1] <= 17)
           {
               if(game_body[game_sign_y[0]][game_sign_x[0] - 2] != 2&&game_body[game_sign_y[1] + 1][game_sign_x[1] + 1] != 2&&game_body[game_sign_y[3] - 1][game_sign_x[3] - 1] != 2)
               {
                   num_csh_game();
                   game_body[game_sign_y[0]][game_sign_x[0] - 2] = 1;    
                   game_body[game_sign_y[1] + 1][game_sign_x[1] + 1] = 1;
                   game_body[game_sign_y[2]][game_sign_x[2]] = 1;
                   game_body[game_sign_y[3] - 1][game_sign_x[3] - 1] = 1;
                   infoTex.setText("游戲進行中!");
                   repaint();
               }    
           }
           if(game_sign_x[1] == game_sign_x[2]&&game_sign_x[1] == game_sign_x[3]&&game_sign_y[0] == game_sign_y[1]&&game_sign_x[3] <= 8)
           {
               if(game_body[game_sign_y[0] + 2][game_sign_x[0]] != 2&&game_body[game_sign_y[1] + 1][game_sign_x[1] - 1] != 2&&game_body[game_sign_y[3] - 1][game_sign_x[3] + 1] != 2)
               {
                   num_csh_game();
                   game_body[game_sign_y[0] + 2][game_sign_x[0]] = 1;    
                   game_body[game_sign_y[1] + 1][game_sign_x[1] - 1] = 1;
                   game_body[game_sign_y[2]][game_sign_x[2]] = 1;
                   game_body[game_sign_y[3] - 1][game_sign_x[3] + 1] = 1;
                   infoTex.setText("游戲進行中!");
                   repaint();
               }    
           }
           if(game_sign_y[0] == game_sign_y[1]&&game_sign_y[1] == game_sign_y[2]&&game_sign_x[0] == game_sign_x[3])
           {
               if(game_body[game_sign_y[0] + 1][game_sign_x[0] + 1] != 2&&game_body[game_sign_y[2] - 1][game_sign_x[2] - 1] != 2&&game_body[game_sign_y[3]][game_sign_x[3] + 2] != 2)
               {
                   num_csh_game();
                   game_body[game_sign_y[0] + 1][game_sign_x[0] + 1] = 1;
                   game_body[game_sign_y[1]][game_sign_x[1]] = 1;
                   game_body[game_sign_y[2] - 1][game_sign_x[2] - 1] = 1;
                   game_body[game_sign_y[3]][game_sign_x[3] + 2] = 1;
                   infoTex.setText("游戲進行中!");
                   repaint();
               }    
           }
       }
       if(blockNumber == 4&&downSign == false)//變換轉彎2有4種情況
       {
           if(game_sign_x[0] == game_sign_x[1]&&game_sign_x[0] == game_sign_x[3]&&game_sign_y[1] == game_sign_y[2]&&game_sign_x[3] <= 7)
           {
               if(game_body[game_sign_y[0] + 2][game_sign_x[0]] != 2&&game_body[game_sign_y[1] + 1][game_sign_x[1] + 1] != 2&&game_body[game_sign_y[3]][game_sign_x[3] + 2] != 2)
               {
                   num_csh_game();
                   game_body[game_sign_y[0] + 2][game_sign_x[0]] = 1;
                   game_body[game_sign_y[1] + 1][game_sign_x[1] + 1] = 1;
                   game_body[game_sign_y[2]][game_sign_x[2]] = 1;
                   game_body[game_sign_y[3]][game_sign_x[3] + 2] = 1;
                   infoTex.setText("游戲進行中!");
                   repaint();
               }    
           }
           if(game_sign_y[1] == game_sign_y[2]&&game_sign_y[1] == game_sign_y[3]&&game_sign_x[0] == game_sign_x[2])
           {
               if(game_body[game_sign_y[1]][game_sign_x[1] + 2] != 2&&game_body[game_sign_y[2] - 1][game_sign_x[2] + 1] != 2&&game_body[game_sign_y[3] - 2][game_sign_x[3]] != 2)
               {
                   num_csh_game();
                   game_body[game_sign_y[0]][game_sign_x[0]] = 1;
                   game_body[game_sign_y[1]][game_sign_x[1] + 2] = 1;
                   game_body[game_sign_y[2] - 1][game_sign_x[2] + 1] = 1;
                   game_body[game_sign_y[3] - 2][game_sign_x[3]] = 1;
                   infoTex.setText("游戲進行中!");
                   repaint();
               }    
           }
           if(game_sign_x[0] == game_sign_x[2]&&game_sign_x[0] == game_sign_x[3]&&game_sign_y[1] == game_sign_y[2]&&game_sign_x[0] >= 2)
           {
               if(game_body[game_sign_y[0]][game_sign_x[0] - 2] != 2&&game_body[game_sign_y[2] - 1][game_sign_x[2] - 1] != 2&&game_body[game_sign_y[3] - 2][game_sign_x[3]] != 2)
               {
                   num_csh_game();
                   game_body[game_sign_y[0]][game_sign_x[0] - 2] = 1;
                   game_body[game_sign_y[1]][game_sign_x[1]] = 1;
                   game_body[game_sign_y[2] - 1][game_sign_x[2] - 1] = 1;
                   game_body[game_sign_y[3] - 2][game_sign_x[3]] = 1;
                   infoTex.setText("游戲進行中!");
                   repaint();
               }    
           }
           if(game_sign_y[0] == game_sign_y[1]&&game_sign_y[0] == game_sign_y[2]&&game_sign_x[1] == game_sign_x[3]&&game_sign_y[0] <= 16)
           {
               if(game_body[game_sign_y[0] + 2][game_sign_x[0]] != 2&&game_body[game_sign_y[1] + 1][game_sign_x[1] - 1] != 2&&game_body[game_sign_y[2]][game_sign_x[2] - 2] != 2)
               {
                   num_csh_game();
                   game_body[game_sign_y[0] + 2][game_sign_x[0]] = 1;
                   game_body[game_sign_y[1] + 1][game_sign_x[1] - 1] = 1;
                   game_body[game_sign_y[2]][game_sign_x[2] - 2] = 1;
                   game_body[game_sign_y[3]][game_sign_x[3]] = 1;
                   infoTex.setText("游戲進行中!");
                   repaint();
               }    
           }
       }
       if(blockNumber == 5&&downSign == false)//變換轉彎3有4種情況
       {
           if(game_sign_x[0] == game_sign_x[2]&&game_sign_x[2] == game_sign_x[3]&&game_sign_y[0] == game_sign_y[1]&&game_sign_x[1] >= 2)
           {
               if(game_body[game_sign_y[0] + 1][game_sign_x[0] - 1] != 2&&game_body[game_sign_y[1]][game_sign_x[1] - 2] != 2&&game_body[game_sign_y[3] - 1][game_sign_x[3] + 1] != 2)
               {
                   num_csh_game();
                   game_body[game_sign_y[0] + 1][game_sign_x[0] - 1] = 1;
                   game_body[game_sign_y[1]][game_sign_x[1] - 2] = 1;
                   game_body[game_sign_y[2]][game_sign_x[2]] = 1;
                   game_body[game_sign_y[3] - 1][game_sign_x[3] + 1] = 1;
                   infoTex.setText("游戲進行中!");
                   repaint();
               }
           }
           if(game_sign_y[1] == game_sign_y[2]&&game_sign_y[2] == game_sign_y[3]&&game_sign_x[0] == game_sign_x[1]&&game_sign_y[0] <= 16)
           {
               if(game_body[game_sign_y[0] + 2][game_sign_x[0]] != 2&&game_body[game_sign_y[1] + 1][game_sign_x[1] + 1] != 2&&game_body[game_sign_y[3] - 1][game_sign_x[3] - 1] != 2)
               {
                      num_csh_game();
                   game_body[game_sign_y[0] + 2][game_sign_x[0]] = 1;
                    game_body[game_sign_y[1] + 1][game_sign_x[1] + 1] = 1;
                   game_body[game_sign_y[2]][game_sign_x[2]] = 1;
                   game_body[game_sign_y[3] - 1][game_sign_x[3] - 1] = 1;
                   infoTex.setText("游戲進行中!");
                   repaint();
               }
           }
           if(game_sign_x[0] == game_sign_x[1]&&game_sign_x[1] == game_sign_x[3]&&game_sign_y[2] == game_sign_y[3])
           {
               if(game_body[game_sign_y[0] + 1][game_sign_x[0] - 1] != 2&&game_body[game_sign_y[2]][game_sign_x[2] + 2] != 2&&game_body[game_sign_y[3] - 1][game_sign_x[3] + 1] != 2)
               {
                   num_csh_game();
                   game_body[game_sign_y[0] + 1][game_sign_x[0] - 1] = 1;
                   game_body[game_sign_y[1]][game_sign_x[1]] = 1;
                   game_body[game_sign_y[2]][game_sign_x[2] + 2] = 1;
                   game_body[game_sign_y[3] - 1][game_sign_x[3] + 1] = 1;
                   infoTex.setText("游戲進行中!");
                   repaint();
               }
           }
           if(game_sign_y[0] == game_sign_y[1]&&game_sign_y[1] == game_sign_y[2]&&game_sign_x[2] == game_sign_x[3])
           {
               if(game_body[game_sign_y[0] + 1][game_sign_x[0] + 1] != 2&&game_body[game_sign_y[2] - 1][game_sign_x[2] - 1] != 2&&game_body[game_sign_y[3] - 2][game_sign_x[3]] != 2)
               {
                   num_csh_game();
                   game_body[game_sign_y[0] + 1][game_sign_x[0] + 1] = 1;
                   game_body[game_sign_y[1]][game_sign_x[1]] = 1;
                   game_body[game_sign_y[2] - 1][game_sign_x[2] - 1] = 1;
                   game_body[game_sign_y[3] - 2][game_sign_x[3]] = 1;
                   infoTex.setText("游戲進行中!");
                   repaint();
               }
           }
       }
       if(blockNumber == 6&&downSign == false)//變換兩層磚塊1的2種情況
       {
           if(game_sign_x[0] == game_sign_x[2]&&game_sign_x[0] >= 2)
           {
               if(game_body[game_sign_y[0]][game_sign_x[0] - 2] != 2&&game_body[game_sign_y[2] - 1][game_sign_x[2] -1 ] != 2&&game_body[game_sign_y[3] - 1][game_sign_x[3] + 1] != 2)
               {
                   num_csh_game();
                   game_body[game_sign_y[0]][game_sign_x[0] - 2] = 1;
                   game_body[game_sign_y[1]][game_sign_x[1]] = 1;
                   game_body[game_sign_y[2] - 1][game_sign_x[2] - 1] = 1;
                   game_body[game_sign_y[3] - 1][game_sign_x[3] + 1] = 1;
                   infoTex.setText("游戲進行中!");
                   repaint();
               }
           }
           if(game_sign_y[0] == game_sign_y[1]&&game_sign_y[3] <= 17)
           {
               if(game_body[game_sign_y[0]][game_sign_x[0] + 2] != 2&&game_body[game_sign_y[1] + 1][game_sign_x[1] + 1] != 2&&game_body[game_sign_y[3] + 1][game_sign_x[3] - 1] != 2)
               {
                      num_csh_game();
                   game_body[game_sign_y[0]][game_sign_x[0] + 2] = 1;
                   game_body[game_sign_y[1] + 1][game_sign_x[1] + 1] = 1;
                   game_body[game_sign_y[2]][game_sign_x[2]] = 1;
                   game_body[game_sign_y[3] + 1][game_sign_x[3] - 1] = 1;
                   infoTex.setText("游戲進行中!");
                   repaint();
               }
           }
       }
       if(blockNumber == 7&&downSign == false)//變換兩層磚塊2的2種情況
       {
           if(game_sign_x[0] == game_sign_x[1]&&game_sign_x[0] <= 16)
           {
               if(game_body[game_sign_y[0]][game_sign_x[0] + 2] != 2&&game_body[game_sign_y[1] - 1][game_sign_x[1] + 1] != 2&&game_body[game_sign_y[3] - 1][game_sign_x[3] - 1] != 2)
               {
                   num_csh_game();
                   game_body[game_sign_y[0]][game_sign_x[0] + 2] = 1;
                   game_body[game_sign_y[1] - 1][game_sign_x[1] + 1] = 1;
                   game_body[game_sign_y[2]][game_sign_x[2]] = 1;
                   game_body[game_sign_y[3] - 1][game_sign_x[3] - 1] = 1;
                   infoTex.setText("游戲進行中!");
                   repaint();
               }
           }
           if(game_sign_y[0] == game_sign_y[1]&&game_sign_y[2] <= 17)
           {
               if(game_body[game_sign_y[0] + 1][game_sign_x[0] - 1] != 2&&game_body[game_sign_y[1]][game_sign_x[1] - 2] != 2&&game_body[game_sign_y[2] + 1][game_sign_x[2] + 1] != 2)
               {
                   num_csh_game();
                   game_body[game_sign_y[0] + 1][game_sign_x[0] - 1] = 1;
                   game_body[game_sign_y[1]][game_sign_x[1] - 2] = 1;
                   game_body[game_sign_y[2] + 1][game_sign_x[2] + 1] = 1;
                   game_body[game_sign_y[3]][game_sign_x[3]] = 1;
                   infoTex.setText("游戲進行中!");
                   repaint();
               }
           }
       }
   }
   
   public void num_csh_game()//數組清零
   {
       for(int i = 0;i < 19;i++)
       {
           for(int j = 0;j < 10;j++)
           {
               if(game_body[i][j] == 2)
               {
                   game_body[i][j] = 2;
               }
               else
               {
                   game_body[i][j] = 0;
               }
           }
       }
   }
   
   public void num_csh_restart()//重新開始時數組清零
   {
       for(int i = 0;i < 19;i++)
       {
           for(int j = 0;j < 10;j++)
           {
               game_body[i][j] = 0;
           }
       }
   }
   
   public void keyTyped(KeyEvent e){}    
   
   public void keyPressed(KeyEvent e)
   {
       if(e.getKeyCode() == KeyEvent.VK_DOWN&&startSign == 1)//處理下鍵
       {
           this.down();
       }
       if(e.getKeyCode() == KeyEvent.VK_LEFT&&startSign == 1)//處理左鍵
       {
           this.left();
       }
       if(e.getKeyCode() == KeyEvent.VK_RIGHT&&startSign == 1)//處理右鍵
       {
           this.right();
       }
       if(e.getKeyCode() == KeyEvent.VK_UP&&startSign == 1)//處理上鍵轉換
       {
           this.change_body(blockNumber);
       }
       if(startSign == 0)
       {
           infoTex.setText("游戲未開始或已結束!");
       }
   }
   
   public void keyReleased(KeyEvent e){}
   
   public void paint(Graphics g)
    {
        g.setColor(Color.black);
        g.fill3DRect(0,0,300,450,true);
        for(int i = 0;i < 19;i++)
        {
            for(int j = 0;j < 10;j++)
            {
                if(game_body[i][j] == 1)
                {
                    g.setColor(Color.blue);
                    g.fill3DRect(30*j,30*(i-4),30,30,true);
                }
                if(game_body[i][j] == 2)
                {
                    g.setColor(Color.magenta);
                    g.fill3DRect(30*j,30*(i-4),30,30,true);
                }
            }
        }    
    }
    
    public void left()//向左移動
    {
        int sign = 0;
        dingwei();
        for(int k = 0;k < 4;k++)
        {
            if(game_sign_x[k] == 0||game_body[game_sign_y[k]][game_sign_x[k] - 1] == 2)
            {
                sign = 1;
            }
        }
        if(sign == 0&&downSign == false)
        {
            num_csh_game();
            for(int k = 0;k < 4;k++)
            {
                game_body[game_sign_y[k]][game_sign_x[k] - 1] = 1;
            }
            infoTex.setText("向左移動!");
            repaint();
        }
    }
    
    public void right()//向右移動
    {
        int sign = 0;
        dingwei();
        for(int k = 0;k < 4;k++)
        {
            if(game_sign_x[k] == 9||game_body[game_sign_y[k]][game_sign_x[k] + 1] == 2)
            {
                sign = 1;
            }
        }
        if(sign == 0&&downSign == false)
        {
            num_csh_game();
            for(int k = 0;k < 4;k++)
            {
                game_body[game_sign_y[k]][game_sign_x[k] + 1] = 1;
            }
            infoTex.setText("向右移動!");
            repaint();
        }
    }
    
    public void down()//下落
    {
        int sign = 0;
        dingwei();
        for(int k = 0;k < 4;k++)
        {
            if(game_sign_y[k] == 18||game_body[game_sign_y[k] + 1][game_sign_x[k]] == 2)
            {
                sign = 1;
                downSign = true;
                changeColor();
                cancelDW();
                getScore();
                if(game_over() == false)
                {
                    rand_block();
                    repaint();
                }
            }
        }
        if(sign == 0)
        {
            num_csh_game();
            for(int k = 0;k < 4;k++)
            {
                game_body[game_sign_y[k] + 1][game_sign_x[k]] = 1;
            }
            infoTex.setText("游戲進行中!");
            repaint();
        }
    }
    
    public boolean game_over()//判斷游戲是否結束
    {
        int sign=0;
        for(int i = 0;i < 10;i++)
        {
            if(game_body[4][i] == 2)
            {
                sign = 1;
            }
        }
        if(sign == 1)
        {
            infoTex.setText("游戲結束!");
            changeColor();
            repaint();
            startSign = 0;
            timer.suspend();
            return true;
        }
        else
        return false;
    }
    
    public void getScore()//滿行消除方法
    {
        for(int i = 0;i < 19;i++)
        {
            int sign = 0;
            for(int j = 0;j < 10;j++)
            {
                if(game_body[i][j] == 2)
                {
                    sign++;
                }
            }
            if(sign == 10)
            {
                gameScore += 100;
                scoreTex.setText(gameScore+"");
                infoTex.setText("恭喜得分!");
                for(int j = i;j >= 1;j--)
                {
                    for(int k = 0;k < 10;k++)
                    {
                        game_body[j][k] = game_body[j - 1][k];
                    }
                }
            }
        }
    }
        
    public void changeColor()//給已經落下的塊換色
    {
        downSign = false;
        for(int k = 0;k < 4;k++)
        {
            game_body[game_sign_y[k]][game_sign_x[k]] = 2;
        }
    }
    
    public void dingwei()//確定其位置
    {
        int k = 0;
        cancelDW();
        for(int i = 0;i < 19;i++)
        {
            for(int j = 0;j < 10;j++)
            {
                if(game_body[i][j] == 1)
                {
                    game_sign_x[k] = j;
                    game_sign_y[k] = i;
                    k++;
                }
            }
        }
    }
    
    public void cancelDW()//將定位數組初始化
    {
        for(int k = 0;k < 4;k++)
        {
            game_sign_x[k] = 0;
            game_sign_y[k] = 0;
        }
    }
    
    public void block1()//長條
    {
        game_body[0][4] = 1;
        game_body[1][4] = 1;
        game_body[2][4] = 1;
        game_body[3][4] = 1;
    }
    
    public void block2()//正方形
    {
        game_body[3][4] = 1;
        game_body[2][4] = 1;
        game_body[3][5] = 1;
        game_body[2][5] = 1;
    }
    public void block3()//3加1(下)
    {
        game_body[1][4] = 1;
        game_body[2][4] = 1;
        game_body[3][4] = 1;
        game_body[3][5] = 1;
    }
    public void block4()//3加1(中)
    {
        game_body[1][4] = 1;
        game_body[2][4] = 1;
        game_body[3][4] = 1;
        game_body[2][5] = 1;
    }
    public void block5()//3加1(上)
    {
        game_body[1][4] = 1;
        game_body[2][4] = 1;
        game_body[3][4] = 1;
        game_body[1][5] = 1;
    }
    public void block6()//轉折1
    {
        game_body[1][5] = 1;
        game_body[2][5] = 1;
        game_body[2][4] = 1;
        game_body[3][4] = 1;
    }
    public void block7()//轉折2
    {
        game_body[1][4] = 1;
        game_body[2][4] = 1;
        game_body[2][5] = 1;
        game_body[3][5] = 1;
    }
}

//定時線程 
class MyTimer extends Thread
{
    Block myBlock; 
   public MyTimer(Block myBlock)
   {
       this.myBlock = myBlock;
   }
   public void run()
   {
       while(myBlock.startSign == 1)
       {
           try{
               sleep((10-myBlock.speedMark + 1)*100); 
               myBlock.down();
           }
           catch(InterruptedException e){}
       } 
  }
}

備注:上鍵變換方向

Java的特點有哪些

Java的特點有哪些 1.Java語言作為靜態面向對象編程語言的代表,實現了面向對象理論,允許程序員以優雅的思維方式進行復雜的編程。 2.Java具有簡單性、面向對象、分布式、安全性、平臺獨立與可移植性、動態性等特點。 3.使用Java可以編寫桌面應用程序、Web應用程序、分布式系統和嵌入式系統應用程序等。

關于“Java怎樣實現俄羅斯方塊小游戲源碼”就介紹到這了,更多相關內容可以搜索億速云以前的文章,希望能夠幫助大家答疑解惑,請多多支持億速云網站!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

瑞丽市| 南昌县| 宣汉县| 稷山县| 汉中市| 驻马店市| 台江县| 师宗县| 扶风县| 平江县| 庐江县| 健康| 聂荣县| 孝义市| 德江县| 吴江市| 齐河县| 鲜城| 巢湖市| 宣城市| 甘洛县| 桃源县| 遂宁市| 青河县| 广西| 杨浦区| 黔西县| 独山县| 若尔盖县| 定日县| 那坡县| 黎平县| 邹平县| 喀喇沁旗| 安溪县| 盐山县| 木兰县| 黑山县| 景洪市| 合肥市| 铁岭市|