Java實(shí)現(xiàn)俄羅斯方塊小游戲源碼
本文實(shí)例為大家分享了Java實(shí)現(xiàn)俄羅斯方塊小游戲的具體代碼,供大家參考,具體內(nèi)容如下
一、最終效果
二、功能需求
1、 在二維平面里面用各種隨機(jī)產(chǎn)生的方塊堆積木,每滿一行消去一行,當(dāng)達(dá)到頂部時(shí),游戲結(jié)束。
2、 通過方向鍵來控制方塊轉(zhuǎn)動(dòng),左移,右移和直落。
3、 方塊下落統(tǒng)一設(shè)置藍(lán)色,接觸底部變粉色。
4、 計(jì)算分?jǐn)?shù),分?jǐn)?shù)是由方塊的類型決定的,每堆積一個(gè)方塊就把分?jǐn)?shù)累加到總分中。
5、 游戲有開始、重新開始、降低提高級(jí)數(shù)(速度)、暫停、退出
三、程序?qū)崿F(xiàn)
這個(gè)是最基礎(chǔ)的方塊素材
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//應(yīng)該是繼承JPanel { ?? ?static Button but[] = new Button[6]; ?? ?static Button noStop = new Button("取 消 暫 停"); ?? ?static Label scoreLab = new Label("分?jǐn)?shù):"); ?? ?static Label infoLab = new Label("提示:"); ?? ?static Label speedLab = new Label("級(jí)數(shù):"); ?? ?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("關(guān) ?于"); ? ?static JDialog dlg_1; ?? ?static JTextArea dlg_1_text = new JTextArea(); ?? ?static int startSign = 0;//游戲開始標(biāo)志 0 未開始 1 開始 2 暫停 ?? ?static String butLab[] = {"開 始 游 戲","重 新 開 始","降 低 級(jí) 數(shù)","提 高 級(jí) 數(shù)","游 戲 暫 停","退 出 游 戲"}; ?? ?static int game_body[][] = new int[19][10]; ?? ?static int game_sign_x[] = new int[4];//用于記錄4個(gè)方格的水平位置 ?? ?static int game_sign_y[] = new int[4];//用于記錄4個(gè)方格的垂直位置 ?? ?static boolean downSign = false;//是否落下 ?? ?static int blockNumber = 1;//磚塊的編號(hào) ?? ?static int gameScore = 0;//游戲分?jǐn)?shù) ?? ?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); //啟動(dòng)線程 ? ? ? ?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("游戲已經(jīng)開始!"); ? ??? ??? ?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])//降低級(jí)數(shù) ? ??? ?{ ? ??? ??? ?infoTex.setText("降低級(jí)數(shù)!"); ? ??? ??? ?speedMark--; ? ??? ??? ?if(speedMark <= 1) ? ??? ??? ?{ ? ??? ??? ??? ?speedMark = 1; ? ??? ??? ??? ?infoTex.setText("已經(jīng)是最低級(jí)數(shù)!"); ? ??? ??? ?} ? ??? ??? ?speedTex.setText(speedMark + ""); ? ??? ?} ? ??? ?if(e.getSource() == but[3])//提高級(jí)數(shù) ? ??? ?{ ? ??? ??? ?infoTex.setText("提高級(jí)數(shù)!"); ? ??? ??? ?speedMark++; ? ??? ??? ?if(speedMark >= 9) ? ??? ??? ?{ ? ??? ??? ??? ?speedMark = 9; ? ??? ??? ??? ?infoTex.setText("已經(jīng)是最高級(jí)數(shù)!"); ? ??? ??? ?} ? ??? ??? ?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("繼續(xù)游戲!"); ? ??? ??? ?timer.resume(); ? ??? ?} ? ??? ?if(e.getSource() == but[5]||e.getSource() == mi1)//退出游戲 ? ??? ?{ ? ??? ??? ?jf.dispose(); ? ??? ?} ? ??? ?if(e.getSource() == mi1_0)//退出游戲 ? ??? ?{ ? ??? ??? ?dlg_1 = new JDialog(jf,"關(guān) 于"); ?? ??? ? ? ?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()//隨機(jī)產(chǎn)生磚塊 ? ?{ ? ??? ?int num; ?? ??? ?num = (int)(Math.random() * 6) + 1;//產(chǎn)生0~6之間的隨機(jī)數(shù) ?? ??? ?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)//改變磚塊狀態(tài) ? ?{ ? ??? ?dingwei(); ? ??? ?if(blockNumber == 1&&downSign == false)//變換長(zhǎng)條2種情況 ? ??? ?{ ? ??? ??? ?if(game_sign_y[0] == game_sign_y[1]&&game_sign_y[3] <= 16)//說明長(zhǎng)條是橫著的 ? ??? ??? ?{ ? ??? ??? ??? ?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("游戲進(jìn)行中!"); ? ??? ??? ??? ? ? ?repaint(); ? ??? ??? ??? ?} ? ??? ??? ?} ? ??? ??? ?if(game_sign_x[0] == game_sign_x[1]&&game_sign_x[0] >= 1&&game_sign_x[3] <= 7)//說明長(zhǎng)條是豎著的 ? ??? ??? ?{ ? ??? ??? ??? ?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("游戲進(jìn)行中!"); ? ??? ??? ??? ? ? ?repaint(); ? ??? ??? ??? ?} ? ??? ??? ?} ? ??? ?} ? ??? ?if(blockNumber == 3&&downSign == false)//變換轉(zhuǎn)彎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("游戲進(jìn)行中!"); ? ??? ??? ??? ? ? ?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("游戲進(jìn)行中!"); ? ??? ??? ??? ? ? ?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("游戲進(jìn)行中!"); ? ??? ??? ??? ? ? ?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("游戲進(jìn)行中!"); ? ??? ??? ??? ? ? ?repaint(); ? ??? ??? ??? ?}?? ? ? ??? ??? ?} ? ??? ?} ? ??? ?if(blockNumber == 4&&downSign == false)//變換轉(zhuǎn)彎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("游戲進(jìn)行中!"); ? ??? ??? ??? ? ? ?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("游戲進(jìn)行中!"); ? ??? ??? ??? ? ? ?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("游戲進(jìn)行中!"); ? ??? ??? ??? ? ? ?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("游戲進(jìn)行中!"); ? ??? ??? ??? ? ? ?repaint(); ? ??? ??? ??? ?}?? ? ? ??? ??? ?} ? ??? ?} ? ??? ?if(blockNumber == 5&&downSign == false)//變換轉(zhuǎn)彎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("游戲進(jìn)行中!"); ? ??? ??? ??? ? ? ?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("游戲進(jìn)行中!"); ? ??? ??? ??? ? ? ?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("游戲進(jìn)行中!"); ? ??? ??? ??? ? ? ?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("游戲進(jìn)行中!"); ? ??? ??? ??? ? ? ?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("游戲進(jìn)行中!"); ? ??? ??? ??? ? ? ?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("游戲進(jìn)行中!"); ? ??? ??? ??? ? ? ?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("游戲進(jìn)行中!"); ? ??? ??? ??? ? ? ?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("游戲進(jìn)行中!"); ? ??? ??? ??? ? ? ?repaint(); ? ??? ??? ??? ?} ? ??? ??? ?} ? ??? ?} ? ?} ? ? ? ?public void num_csh_game()//數(shù)組清零 ? ?{ ? ??? ?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()//重新開始時(shí)數(shù)組清零 ? ?{ ? ??? ?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)//處理上鍵轉(zhuǎn)換 ? ??? ?{ ? ??? ??? ?this.change_body(blockNumber); ? ??? ?} ? ??? ?if(startSign == 0) ? ??? ?{ ? ??? ??? ?infoTex.setText("游戲未開始或已結(jié)束!"); ? ??? ?} ? ?} ? ? ? ?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()//向左移動(dòng) ?? ?{ ?? ??? ?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("向左移動(dòng)!"); ?? ??? ? ? ?repaint(); ?? ??? ?} ?? ?} ?? ? ?? ?public void right()//向右移動(dòng) ?? ?{ ?? ??? ?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("向右移動(dòng)!"); ?? ??? ? ? ?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("游戲進(jìn)行中!"); ?? ??? ? ? ?repaint(); ?? ??? ?} ?? ?} ?? ? ?? ?public boolean game_over()//判斷游戲是否結(jié)束 ?? ?{ ?? ??? ?int sign=0; ?? ??? ?for(int i = 0;i < 10;i++) ?? ??? ?{ ?? ??? ??? ?if(game_body[4][i] == 2) ?? ??? ??? ?{ ?? ??? ??? ??? ?sign = 1; ?? ??? ??? ?} ?? ??? ?} ?? ??? ?if(sign == 1) ?? ??? ?{ ?? ??? ??? ?infoTex.setText("游戲結(jié)束!"); ?? ??? ??? ?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()//給已經(jīng)落下的塊換色 ?? ?{ ?? ??? ?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()//將定位數(shù)組初始化 ?? ?{ ?? ??? ?for(int k = 0;k < 4;k++) ?? ??? ?{ ?? ??? ??? ?game_sign_x[k] = 0; ?? ??? ??? ?game_sign_y[k] = 0; ?? ??? ?} ?? ?} ?? ? ?? ?public void block1()//長(zhǎng)條 ?? ?{ ?? ??? ?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()//轉(zhuǎn)折1 ?? ?{ ?? ??? ?game_body[1][5] = 1; ?? ??? ?game_body[2][5] = 1; ?? ??? ?game_body[2][4] = 1; ?? ??? ?game_body[3][4] = 1; ?? ?} ?? ?public void block7()//轉(zhuǎn)折2 ?? ?{ ?? ??? ?game_body[1][4] = 1; ?? ??? ?game_body[2][4] = 1; ?? ??? ?game_body[2][5] = 1; ?? ??? ?game_body[3][5] = 1; ?? ?} } //定時(shí)線程? 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){} ? ? ? ?}? ? } }?
備注:上鍵變換方向
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot詳細(xì)探究講解默認(rèn)組件掃描
在項(xiàng)目中我們創(chuàng)建了Controller,這個(gè)Controller是如何被spring自動(dòng)加載的呢?為什么Controller必須放在啟動(dòng)類的同級(jí)目錄下呢2022-06-06Java利用LocalDate類實(shí)現(xiàn)日歷設(shè)計(jì)
java中做時(shí)間處理時(shí)一般會(huì)采用java.util.Date,但是相比于Date來說,還有更好的選擇--java.time.LocalDate。本文就來用LocalDate類實(shí)現(xiàn)日歷設(shè)計(jì),感興趣的可以動(dòng)手嘗試一下2022-07-07JAVA實(shí)現(xiàn)數(shù)字大寫金額轉(zhuǎn)換的方法
這篇文章主要介紹了JAVA實(shí)現(xiàn)數(shù)字大寫金額轉(zhuǎn)換的方法,涉及java針對(duì)字符串與數(shù)組的遍歷與轉(zhuǎn)換相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07