版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、東西絕對不多說,直接看!看了拷貝就懂了!,直接可以拷貝下面的東西,然后記得把那個BLOCK的名字改成你自己的類名,這個很關(guān)鍵哦,不然是錯的可別怪我,呵呵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)該是繼承
2、JPanelstatic Button but = new Button6;static Button noStop = new Button(取 消 暫 停);static Label scoreLab = new Label(分?jǐn)?shù):);static Label infoLab = new Label(提示:);static Label speedLab = new Label(級數(shù):);static Label scoreTex = new Label(0);static Label infoTex = new Label( );static Label speedTex = new La
3、bel(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 JMen
4、uItem(退出);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 = 開 始 游 戲,重 新 開 始,降 低 級 數(shù),提 高 級 數(shù),游 戲 暫 停,退 出 游 戲;static int game_body = new int1910;static int game_sign_x =
5、new int4;/用于記錄4個方格的水平位置static int game_sign_y = new int4;/用于記錄4個方格的垂直位置static boolean downSign = false;/是否落下static int blockNumber = 1;/磚塊的編號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.a
6、dd(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(); time
7、r.suspend();public void init() setLayout(null); for(int i = 0;i 6;i+) buti = new Button(butLabi); add(buti); buti.addActionListener(this); buti.addKeyListener(this); buti.setBounds(360,(240 + 30 * i),160,25); add(scoreLab); add(scoreTex); add(speedLab); add(speedTex); add(infoLab); add(infoTex); add
8、(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);but1.setEnabled(false);but4.setEnabled(false);infoLab.setBounds(320,75,30,20)
9、;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 actionPerfor
10、med(ActionEvent e) if(e.getSource() = but0)/開始游戲 startSign = 1; infoTex.setText(游戲已經(jīng)開始!); but0.setEnabled(false); but1.setEnabled(true); but4.setEnabled(true); timer.resume(); if(e.getSource() = but1|e.getSource() = mi0)/重新開始游戲 startSign = 0; gameScore = 0; timer.suspend(); num_csh_restart(); repain
11、t(); rand_block(); scoreTex.setText(0); infoTex.setText(新游戲!); but0.setEnabled(true); but1.setEnabled(false);推薦精選 but4.setEnabled(false); if(e.getSource() = but2)/降低級數(shù) infoTex.setText(降低級數(shù)!); speedMark-; if(speedMark = 9) speedMark = 9; infoTex.setText(已經(jīng)是最高級數(shù)!); speedTex.setText(speedMark + ); if(e
12、.getSource() = but4)/游戲暫停 this.add(noStop); this.remove(but4); infoTex.setText(游戲暫停!); timer.suspend(); if(e.getSource() = noStop)/取消暫停 this.remove(noStop); this.add(but4); infoTex.setText(繼續(xù)游戲!); timer.resume(); if(e.getSource() = but5|e.getSource() = mi1)/退出游戲 jf.dispose(); if(e.getSource() = mi1_
13、0)/退出游戲 推薦精選 dlg_1 = new JDialog(jf,關(guān) 于); try FileInputStream io = new FileInputStream(resource/guanyu.txt);/得到路徑 byte a = new byteio.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
14、_1.pack(); dlg_1.setResizable(false); dlg_1.setSize(200, 120); dlg_1.setLocation(400, 240); dlg_1.show(); public void rand_block()/隨機產(chǎn)生磚塊 int num;num = (int)(Math.random() * 6) + 1;/產(chǎn)生06之間的隨機數(shù)blockNumber = num;switch(blockNumber) case 1: block1(); blockNumber = 1; break; case 2: block2(); blockNumbe
15、r = 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 =
16、1&downSign = false)/變換長條2種情況 if(game_sign_y0 = game_sign_y1&game_sign_y3 = 1&game_sign_x3 = 1) if(game_bodygame_sign_y0 + 1game_sign_x0 - 1 != 2&game_bodygame_sign_y2 - 1game_sign_x2 + 1 != 2&game_bodygame_sign_y3 - 2game_sign_x3 != 2) num_csh_game(); game_bodygame_sign_y0 + 1game_sign_x0 - 1 = 1; g
17、ame_bodygame_sign_y1game_sign_x1 = 1; game_bodygame_sign_y2 - 1game_sign_x2 + 1 = 1; game_bodygame_sign_y3 - 2game_sign_x3 = 1; infoTex.setText(游戲進(jìn)行中!);推薦精選 repaint(); if(game_sign_y1 = game_sign_y2&game_sign_y2 = game_sign_y3&game_sign_x0 = game_sign_x3&game_sign_y1 = 17) if(game_bodygame_sign_y0ga
18、me_sign_x0 - 2 != 2&game_bodygame_sign_y1 + 1game_sign_x1 + 1 != 2&game_bodygame_sign_y3 - 1game_sign_x3 - 1 != 2) num_csh_game(); game_bodygame_sign_y0game_sign_x0 - 2 = 1; game_bodygame_sign_y1 + 1game_sign_x1 + 1 = 1; game_bodygame_sign_y2game_sign_x2 = 1; game_bodygame_sign_y3 - 1game_sign_x3 -
19、1 = 1; infoTex.setText(游戲進(jìn)行中!); repaint(); if(game_sign_x1 = game_sign_x2&game_sign_x1 = game_sign_x3&game_sign_y0 = game_sign_y1&game_sign_x3 = 8) if(game_bodygame_sign_y0 + 2game_sign_x0 != 2&game_bodygame_sign_y1 + 1game_sign_x1 - 1 != 2&game_bodygame_sign_y3 - 1game_sign_x3 + 1 != 2) num_csh_gam
20、e(); game_bodygame_sign_y0 + 2game_sign_x0 = 1; game_bodygame_sign_y1 + 1game_sign_x1 - 1 = 1; game_bodygame_sign_y2game_sign_x2 = 1; game_bodygame_sign_y3 - 1game_sign_x3 + 1 = 1; infoTex.setText(游戲進(jìn)行中!); repaint(); if(game_sign_y0 = game_sign_y1&game_sign_y1 = game_sign_y2&game_sign_x0 = game_sign
21、_x3) if(game_bodygame_sign_y0 + 1game_sign_x0 + 1 != 2&game_bodygame_sign_y2 - 1game_sign_x2 - 1 != 2&game_bodygame_sign_y3game_sign_x3 + 2 != 2)推薦精選 num_csh_game(); game_bodygame_sign_y0 + 1game_sign_x0 + 1 = 1; game_bodygame_sign_y1game_sign_x1 = 1; game_bodygame_sign_y2 - 1game_sign_x2 - 1 = 1; g
22、ame_bodygame_sign_y3game_sign_x3 + 2 = 1; infoTex.setText(游戲進(jìn)行中!); repaint(); if(blockNumber = 4&downSign = false)/變換轉(zhuǎn)彎2有4種情況 if(game_sign_x0 = game_sign_x1&game_sign_x0 = game_sign_x3&game_sign_y1 = game_sign_y2&game_sign_x3 = 2) if(game_bodygame_sign_y0game_sign_x0 - 2 != 2&game_bodygame_sign_y2 -
23、 1game_sign_x2 - 1 != 2&game_bodygame_sign_y3 - 2game_sign_x3 != 2) num_csh_game(); game_bodygame_sign_y0game_sign_x0 - 2 = 1; game_bodygame_sign_y1game_sign_x1 = 1; game_bodygame_sign_y2 - 1game_sign_x2 - 1 = 1; game_bodygame_sign_y3 - 2game_sign_x3 = 1; infoTex.setText(游戲進(jìn)行中!); repaint(); if(game_
24、sign_y0 = game_sign_y1&game_sign_y0 = game_sign_y2&game_sign_x1 = game_sign_x3&game_sign_y0 = 2) if(game_bodygame_sign_y0 + 1game_sign_x0 - 1 != 2&推薦精選game_bodygame_sign_y1game_sign_x1 - 2 != 2&game_bodygame_sign_y3 - 1game_sign_x3 + 1 != 2) num_csh_game(); game_bodygame_sign_y0 + 1game_sign_x0 - 1
25、= 1; game_bodygame_sign_y1game_sign_x1 - 2 = 1; game_bodygame_sign_y2game_sign_x2 = 1; game_bodygame_sign_y3 - 1game_sign_x3 + 1 = 1; infoTex.setText(游戲進(jìn)行中!); repaint(); if(game_sign_y1 = game_sign_y2&game_sign_y2 = game_sign_y3&game_sign_x0 = game_sign_x1&game_sign_y0 = 2) if(game_bodygame_sign_y0g
26、ame_sign_x0 - 2 != 2&game_bodygame_sign_y2 - 1game_sign_x2 -1 != 2&game_bodygame_sign_y3 - 1game_sign_x3 + 1 != 2) num_csh_game(); game_bodygame_sign_y0game_sign_x0 - 2 = 1; game_bodygame_sign_y1game_sign_x1 = 1; game_bodygame_sign_y2 - 1game_sign_x2 - 1 = 1; game_bodygame_sign_y3 - 1game_sign_x3 +
27、1 = 1; infoTex.setText(游戲進(jìn)行中!); repaint(); if(game_sign_y0 = game_sign_y1&game_sign_y3 = 17) if(game_bodygame_sign_y0game_sign_x0 + 2 != 2&game_bodygame_sign_y1 + 1game_sign_x1 + 1 != 2&game_bodygame_sign_y推薦精選3 + 1game_sign_x3 - 1 != 2) num_csh_game(); game_bodygame_sign_y0game_sign_x0 + 2 = 1; gam
28、e_bodygame_sign_y1 + 1game_sign_x1 + 1 = 1; game_bodygame_sign_y2game_sign_x2 = 1; game_bodygame_sign_y3 + 1game_sign_x3 - 1 = 1; infoTex.setText(游戲進(jìn)行中!); repaint(); if(blockNumber = 7&downSign = false)/變換兩層磚塊2的2種情況 if(game_sign_x0 = game_sign_x1&game_sign_x0 = 16) if(game_bodygame_sign_y0game_sign_
29、x0 + 2 != 2&game_bodygame_sign_y1 - 1game_sign_x1 + 1 != 2&game_bodygame_sign_y3 - 1game_sign_x3 - 1 != 2) num_csh_game(); game_bodygame_sign_y0game_sign_x0 + 2 = 1; game_bodygame_sign_y1 - 1game_sign_x1 + 1 = 1; game_bodygame_sign_y2game_sign_x2 = 1; game_bodygame_sign_y3 - 1game_sign_x3 - 1 = 1; i
30、nfoTex.setText(游戲進(jìn)行中!); repaint(); if(game_sign_y0 = game_sign_y1&game_sign_y2 = 17) if(game_bodygame_sign_y0 + 1game_sign_x0 - 1 != 2&game_bodygame_sign_y1game_sign_x1 - 2 != 2&game_bodygame_sign_y2 + 1game_sign_x2 + 1 != 2) num_csh_game(); game_bodygame_sign_y0 + 1game_sign_x0 - 1 = 1; game_bodyga
31、me_sign_y1game_sign_x1 - 2 = 1; game_bodygame_sign_y2 + 1game_sign_x2 + 1 = 1; game_bodygame_sign_y3game_sign_x3 = 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_bodyij = 2) game_bodyij = 2; else game_bodyij = 0; publi
32、c void num_csh_restart()/重新開始時數(shù)組清零 for(int i = 0;i 19;i+) for(int j = 0;j 10;j+) game_bodyij = 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)/處理左鍵 推薦精選 th
33、is.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.
34、black);g.fill3DRect(0,0,300,450,true);for(int i = 0;i 19;i+) for(int j = 0;j 10;j+) if(game_bodyij = 1) g.setColor(Color.blue); g.fill3DRect(30*j,30*(i-4),30,30,true); if(game_bodyij = 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_xk = 0|game_bodygame_sign_ykgame_sign_xk - 1 = 2) sign = 1; if(sign = 0&downSign = false) num_csh_game(); for(int k = 0;k 4;k+) game_bodygame_sign_ykgame_sign_xk - 1 = 1; infoTex.setText(向左移動!); repaint();public void right()/向右移動int s
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年蘇科新版五年級語文下冊階段測試試卷含答案
- 2025年度餐飲企業(yè)員工培訓(xùn)與簽單激勵方案合同3篇
- 家庭文化在塑造健康生活方式中的影響
- 2025年蘇科新版六年級英語上冊月考試卷
- 2025年人教版(2024)六年級英語上冊階段測試試卷
- 2024版私募股權(quán)委托投資合同版
- 2024幼兒園消防設(shè)施升級改造與安裝服務(wù)協(xié)議3篇
- 昌平區(qū)六上數(shù)學(xué)試卷
- 2024版股權(quán)激勵協(xié)議書3篇
- 二零二五年度環(huán)保搬運工程合作協(xié)議3篇
- 2024年萍鄉(xiāng)衛(wèi)生職業(yè)學(xué)院單招職業(yè)適應(yīng)性測試題庫參考答案
- 中國農(nóng)業(yè)銀行信用借款合同
- ISO 56001-2024《創(chuàng)新管理體系-要求》專業(yè)解讀與應(yīng)用實踐指導(dǎo)材料之9:“5領(lǐng)導(dǎo)作用-5.3創(chuàng)新戰(zhàn)略”(雷澤佳編制-2025B0)
- 江蘇省連云港市2023-2024學(xué)年八年級上學(xué)期期末數(shù)學(xué)試題(原卷版)
- 初中英語聽力高頻詞
- 2024智能變電站新一代集控站設(shè)備監(jiān)控系統(tǒng)技術(shù)規(guī)范部分
- 2024年建筑業(yè)10項新技術(shù)
- 語文七年級下字帖打印版
- 二年級上冊音樂課件---選唱歌曲-我們和祖國最親親-西師大版(共8張PPT)
- 設(shè)備租賃服務(wù)方案
- 最新中石油帶壓作業(yè)技術(shù)規(guī)程
評論
0/150
提交評論