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){} } } }
備註:上鍵變換方向
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。