版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、.【連連看】簡(jiǎn)單小游戲初始源代碼使用說(shuō)明:進(jìn)入Java開(kāi)發(fā)工具eclipse 中,在左面建立一個(gè)任意名的java project, 然后在工程下建立一個(gè)名字為L(zhǎng)ianliankan 的 package,最后建立一個(gè)名字為lianliankan 的 class 類(lèi)。將以下程序代碼復(fù)制入class 內(nèi)容面板中,點(diǎn)擊運(yùn)行。package Lianliankan;import javax.swing.*;import java.awt.*;import java.awt.event.*;public class lianliankan implements ActionListenerJFrame m
2、ainFrame; / 主面板Container thisContainer;JPanel centerPanel,southPanel,northPanel; / 子面板JButton diamondsButton = new JButton65;/游戲按鈕數(shù)組JButton exitButton,resetButton,newlyButton; /退出,重列,重新開(kāi)始按鈕JLabel fractionLable=new JLabel(0); / 分?jǐn)?shù)標(biāo)簽JButton firstButton,secondButton; /分別記錄兩次被選中的按鈕int grid = new int87;/
3、儲(chǔ)存游戲按鈕位置static boolean pressInformation=false; /判斷是否有按鈕被選中int x0=0,y0=0,x=0,y=0,fristMsg=0,secondMsg=0,validateLV; / 游戲按鈕的位置坐標(biāo)int i,j,k,n;/ 消除方法控制public void init()mainFrame=new JFrame(JKJ連連看 );thisContainer = mainFrame.getContentPane();thisContainer.setLayout(new BorderLayout();centerPanel=new JPan
4、el();southPanel=new JPanel();northPanel=new JPanel();thisContainer.add(centerPanel,Center);thisContainer.add(southPanel,South);thisContainer.add(northPanel,North);centerPanel.setLayout(new GridLayout(6,5);for(int cols = 0;cols 6;cols+)for(int rows = 0;rows 5;rows+ )diamondsButtoncolsrows=new JButton
5、(String.valueOf(gridcols+1rows+1); diamondsButtoncolsrows.addActionListener(this); centerPanel.add(diamondsButtoncolsrows);exitButton=new JButton( 退出 );exitButton.addActionListener(this);resetButton=new JButton( 重列 );resetButton.addActionListener(this);newlyButton=new JButton( 再來(lái)一局 );.newlyButton.ad
6、dActionListener(this);southPanel.add(exitButton);southPanel.add(resetButton);southPanel.add(newlyButton);fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable.getText();northPanel.add(fractionLable);mainFrame.setBounds(280,100,500,450);mainFrame.setVisible(true);public void randomBuild
7、() int randoms,cols,rows;for(int twins=1;twins=15;twins+) randoms=(int)(Math.random()*25+1);for(int alike=1;alike=2;alike+) cols=(int)(Math.random()*6+1);rows=(int)(Math.random()*5+1);while(gridcolsrows!=0) cols=(int)(Math.random()*6+1);rows=(int)(Math.random()*5+1);this.gridcolsrows=randoms;public
8、void fraction()fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable.getText()+100);public void reload() int save = new int30;int n=0,cols,rows;int grid= new int87;for(int i=0;i=6;i+) for(int j=0;j=0) cols=(int)(Math.random()*6+1);rows=(int)(Math.random()*5+1);.while(gridcolsrows!=0) c
9、ols=(int)(Math.random()*6+1);rows=(int)(Math.random()*5+1);this.gridcolsrows=saven;n-;mainFrame.setVisible(false);pressInformation=false; / 這里一定要將按鈕點(diǎn)擊信息歸為初始 init();for(int i = 0;i 6;i+)for(int j = 0;j 5;j+ )if(gridi+1j+1=0)diamondsButtonij.setVisible(false);public void estimateEven(int placeX,int pl
10、aceY ,JButton bz) if(pressInformation=false) x=placeX;y=placeY;secondMsg=gridxy;secondButton=bz;pressInformation=true;else x0=x;y0=y;fristMsg=secondMsg;firstButton=secondButton;x=placeX;y=placeY;secondMsg=gridxy;secondButton=bz;if(fristMsg=secondMsg & secondButton!=firstButton)xiao();public void xia
11、o() / 相同的情況下能不能消去。仔細(xì)分析,不一條條注釋if(x0=x &(y0=y+1|y0=y-1) | (x0=x+1|x0=x-1)&(y0=y) / 判斷是否相鄰 remove();else.for (j=0;jj) / 如果第二個(gè)按鈕的 Y 坐標(biāo)大于空按鈕的 Y 坐標(biāo)說(shuō)明第一按鈕在第二按鈕左邊 for (i=y-1;i=j;i- ) / 判斷第二按鈕左側(cè)直到第一按鈕中間有沒(méi)有按鈕if (gridxi!=0) k=0;break;else k=1; /K=1 說(shuō)明通過(guò)了第一次驗(yàn)證if (k=1) linePassOne();if (yj) / 如果第二個(gè)按鈕的 Y 坐標(biāo)小于空按鈕的
12、 Y 坐標(biāo)說(shuō)明第一按鈕在第二按鈕右邊 for (i=y+1;i=j ;i+ ) / 判斷第二按鈕左側(cè)直到第一按鈕中間有沒(méi)有按鈕 if (gridxi!=0)k=0;break;else k=1; if (k=1)linePassOne();if (y=j ) linePassOne();if (k=2) if (x0=x) remove();if (x0x) for (n=x0;nx) for (n=x0;n=x+1 ;n- ) if (gridnj!=0) k=0;break;if(gridnj=0 & n=x+1) remove();for (i=0;ii) for (j=x-1;j=i
13、;j- ) if (gridjy!=0) k=0;break;else k=1; if (k=1) rowPassOne();if (xi) for (j=x+1;j=i;j+ ) if (gridjy!=0) k=0;break;else k=1; if (k=1) rowPassOne();if (x=i) rowPassOne();.if (k=2)if (y0=y) remove();if (y0y) for (n=y0;ny) for (n=y0;n=y+1 ;n-) if (gridin!=0) k=0;break;if(gridin=0 & n=y+1) remove();pub
14、lic void linePassOne()if (y0j) / 第一按鈕同行空按鈕在左邊f(xié)or (i=y0-1;i=j ;i- ) / 判斷第一按鈕同左側(cè)空按鈕之間有沒(méi)按鈕 if (gridx0i!=0) k=0;break;else k=2; /K=2 說(shuō)明通過(guò)了第二次驗(yàn)證if (y0j) / 第一按鈕同行空按鈕在與第二按鈕之間for (i=y0+1;ii) for (j=x0-1;j=i ;j- ) if (gridjy0!=0) k=0;break;else k=2; if (x0i) for (j=x0+1;j=i ;j+ ) if (gridjy0!=0) k=0;break;el
15、se k=2; public void remove()firstButton.setVisible(false);secondButton.setVisible(false);fraction();pressInformation=false;k=0;gridx0y0=0;gridxy=0;public void actionPerformed(ActionEvent e) if(e.getSource()=newlyButton)int grid = new int87;this.grid = grid;randomBuild();mainFrame.setVisible(false);pressInformation=false;init();.if(e.getSource()=exitButton)System.exit(0);if(e.getSource()=resetButton)reload();for(int cols
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 環(huán)氧乙烷(乙二醇)裝置操作工安全綜合測(cè)試考核試卷含答案
- 電子電氣產(chǎn)品能效檢驗(yàn)員持續(xù)改進(jìn)強(qiáng)化考核試卷含答案
- 礦井通風(fēng)工安全培訓(xùn)競(jìng)賽考核試卷含答案
- 凹版制版員安全生產(chǎn)基礎(chǔ)知識(shí)能力考核試卷含答案
- 燃?xì)廨斉鋱?chǎng)站運(yùn)行工崗前基礎(chǔ)實(shí)操考核試卷含答案
- 學(xué)生清明節(jié)回家掃墓的請(qǐng)假條
- 2025年聚烯烴類(lèi)線纜項(xiàng)目發(fā)展計(jì)劃
- 2025年聲增敏保偏光纖合作協(xié)議書(shū)
- 遼寧省葫蘆島市2025-2026學(xué)年高一上學(xué)期1月期末考試政治試卷
- 2026年數(shù)字藝術(shù)品收藏項(xiàng)目公司成立分析報(bào)告
- 2026年中國(guó)航空傳媒有限責(zé)任公司市場(chǎng)化人才招聘?jìng)淇碱}庫(kù)有答案詳解
- 2026年《全科》住院醫(yī)師規(guī)范化培訓(xùn)結(jié)業(yè)理論考試題庫(kù)及答案
- 2026北京大興初二上學(xué)期期末語(yǔ)文試卷和答案
- 專題23 廣東省深圳市高三一模語(yǔ)文試題(學(xué)生版)
- 廣元市利州區(qū)何家坪石材廠飾面用灰?guī)r礦礦山地質(zhì)環(huán)境保護(hù)與土地復(fù)墾方案
- 保健按摩師初級(jí)試題
- 上腔靜脈綜合征的護(hù)理
- 2021年度四川省專業(yè)技術(shù)人員繼續(xù)教育公需科目(答案整合)
- 醫(yī)療廢物處理方案
- 船舶靠離泊作業(yè)風(fēng)險(xiǎn)辨識(shí)表
- DB37T 2673-2019醫(yī)療機(jī)構(gòu)能源消耗定額標(biāo)準(zhǔn)
評(píng)論
0/150
提交評(píng)論