java實(shí)現(xiàn)簡(jiǎn)易連連看小游戲
本文實(shí)例為大家分享了java實(shí)現(xiàn)簡(jiǎn)易連連看小游戲的具體代碼,供大家參考,具體內(nèi)容如下
新手上路,分享一下
直接上代碼
package linkgame; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.File; import java.util.HashSet; import java.util.Set; import java.util.TreeSet; public class LinkGame extends JFrame implements Runnable{ ? ? private JPanel panel_01,panel_02; ? ? private String first,finnal; ? ? private JLabel time; ? ? private boolean isClick=true; ? ? private int x1,y1,x2,y2; ? ? Icon temp; ? ? JButton firstbutton=new JButton(); ? ? JButton secondbutton=new JButton(); ? ? public LinkGame(){ ? ? ? ? setTitle("連連看"); ? ? ? ? setBounds(300,100,600,450); ? ? ? ? panel_01=new JPanel(new GridLayout(6,6)); ? ? ? ? panel_02=new JPanel(new BorderLayout()); ? ? ? ? close(); ? ? ? ? rightPanel(); ? ? ? ? leftPanel(); ? ? ? ? add(panel_01,BorderLayout.CENTER); ? ? ? ? add(panel_02,BorderLayout.WEST); ? ? ? ? setVisible(true); ? ? ? ? setResizable(false); ? ? ? ? setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); ? ? ? ? //run(); ? ? } //關(guān)閉應(yīng)用詢問 ? ? private void close(){ ? ? ? ? addWindowListener(new WindowAdapter() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void windowClosing(WindowEvent e) { ? ? ? ? ? ? ? ? int result = JOptionPane.showConfirmDialog(panel_01, "是否確認(rèn)退出?", "確認(rèn)", ? ? ? ? ? ? ? ? ? ? ? ? JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE); ? ? ? ? ? ? ? ? if(result == JOptionPane.OK_OPTION){ ? ? ? ? ? ? ? ? ? ? System.exit(0); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? }); ? ? } ? ? //左側(cè)面板 ? ? private void rightPanel(){ ? ? ? ? File img=new File("src/images");//打開圖片文件夾 ? ? ? ? String[] list=img.list();//獲取文件中的圖片名稱并存入集合 ? ? ? ? Set<String> s=new HashSet<>();//構(gòu)建無序集合 ? ? ? ? Set<String> s2=new TreeSet<>(); ? ? ? ? boolean bol=false; ? ? ? ? //把18個(gè)圖片存入set集合中以備生成界面 ? ? ? ? for(int l=0;l<18;l++){ ? ? ? ? ? ? s.add(list[l]); ? ? ? ? ? ? s2.add(list[l]); ? ? ? ? } ? ? ? ? for(int i=0,count=0;i<6;i++){ ? ? ? ? ? ? for(int j=0;j<6;j++){ ? ? ? ? ? ? ? ? String[] strs01=s.toArray(new String[0]); ? ? ? ? ? ? ? ? String[] strs02=s2.toArray(new String[0]); ? ? ? ? ? ? ? ? if(count>17){ ? ? ? ? ? ? ? ? ? ? count=0; ? ? ? ? ? ? ? ? ? ? bol=true; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? JButton button=new JButton();//新建按鈕 ? ? ? ? ? ? ? ? if(bol){ ? ? ? ? ? ? ? ? ? ? ImageIcon imgs=new ImageIcon("src/images/"+strs02[count]); ? ? ? ? ? ? ? ? ? ? button.setIcon(imgs); ? ? ? ? ? ? ? ? }else{ ? ? ? ? ? ? ? ? ? ? ImageIcon imgs=new ImageIcon("src/images/"+strs01[count]); ? ? ? ? ? ? ? ? ? ? button.setIcon(imgs); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? button.addActionListener(new click());//添加按鈕的監(jiān)聽事件 ? ? ? ? ? ? ? ? panel_01.add(button);//把按鈕添加到左面板 ? ? ? ? ? ? ? ? count++; ? ? ? ? ? ? } ? ? ? ? } ? ? } ? ? //有面板 ? ? private void leftPanel(){ ? ? ? ? ImageIcon icon=new ImageIcon("src/images/leftback.png");//連連看字 ? ? ? ? JLabel label=new JLabel(icon); ? ? ? ? time=new JLabel("剩余時(shí)間30秒");//倒計(jì)時(shí)計(jì)時(shí)初始化 ? ? ? ? time.setFont(new Font("楷體",Font.PLAIN,20));//設(shè)置字體樣式 ? ? ? ? panel_02.add(label,BorderLayout.NORTH); ? ? ? ? panel_02.add(time,BorderLayout.SOUTH); ? ? } ? ? //倒計(jì)時(shí)方法 ? ? public void run() { ? ? ? ? int ?count=30; ? ? ? ? while (count>=0) { ? ? ? ? ? ? try { ? ? ? ? ? ? ? ? time.setText("剩余時(shí)間"+count + "秒"); ? ? ? ? ? ? ? ? Thread.sleep(1000); ? //暫停1秒 ? ? ? ? ? ? ? ? count--; ? ? ? ? ? ? } catch (InterruptedException e) { ? ? ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? ? ? } ? ? ? ? ? ? if(count==0){ ? ? ? ? ? ? ? ? JOptionPane.showMessageDialog(panel_01,"時(shí)間用完,已結(jié)束"); ? ? ? ? ? ? ? ? System.exit(0); ? ? ? ? ? ? } ? ? ? ? } ? ? } ? ? //按鈕點(diǎn)擊內(nèi)部類 ? ? class click implements ActionListener{ ? ? ? ? ImageIcon img=new ImageIcon("src/images/Img319981730_null.jpg");//空白圖片 ? ? ? ? @Override ? ? ? ? public void actionPerformed(ActionEvent e) { ? ? ? ? ? ? //獲取點(diǎn)擊按鈕的行和列 ? ? ? ? ? ? if(isClick){ ? ? ? ? ? ? ? ? firstbutton=((JButton)e.getSource()); ? ? ? ? ? ? ? ? first=firstbutton.getIcon().toString();//獲取點(diǎn)擊圖片名稱 ? ? ? ? ? ? ? ? temp=firstbutton.getIcon(); ? ? ? ? ? ? ? ? x1=firstbutton.getLocation().x;//獲取點(diǎn)擊圖片橫縱坐標(biāo) ? ? ? ? ? ? ? ? y1=firstbutton.getLocation().y; ? ? ? ? ? ? ? ? firstbutton.setIcon(img);//點(diǎn)擊后圖片設(shè)置為空白圖片 ? ? ? ? ? ? ? ? isClick=false; ? ? ? ? ? ? }else { ? ? ? ? ? ? ? ? secondbutton=((JButton)e.getSource()); ? ? ? ? ? ? ? ? finnal=secondbutton.getIcon().toString();//獲取第二次點(diǎn)擊圖片名稱 ? ? ? ? ? ? ? ? x2=secondbutton.getLocation().x;//獲取點(diǎn)擊圖片橫縱坐標(biāo) ? ? ? ? ? ? ? ? y2=secondbutton.getLocation().y; ? ? ? ? ? ? ? ? isClick=true; ? ? ? ? ? ? ? ? win();//判斷是否消除或者勝利方法 ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? private void win(){ ? ? ? ? ? ? boolean bol=first.equals(finnal);//判斷兩次點(diǎn)擊的圖片名字是否相同 ? ? ? ? ? ? boolean bol2=!((x1==x2)&&(y1==y2));//判斷位置是否不一樣,防止點(diǎn)擊兩張相同的圖片 ? ? ? ? ? ? if(bol&&bol2){ ? ? ? ? ? ? ? ? firstbutton.setIcon(img); ? ? ? ? ? ? ? ? secondbutton.setIcon(img); ? ? ? ? ? ? ? ? String str02="src/images/Img319981730_null.jpg";//把第二張圖片改成空白圖片 ? ? ? ? ? ? ? ? Component[] bts=panel_01.getComponents();//獲取全部組件 ? ? ? ? ? ? ? ? int count=0; ? ? ? ? ? ? ? ? for(int i=0;i<bts.length;i++){ ? ? ? ? ? ? ? ? ? ? JButton btn=(JButton)bts[i]; ? ? ? ? ? ? ? ? ? ? String str01=btn.getIcon().toString(); ? ? ? ? ? ? ? ? ? ? if(str01.equals(str02)){//判斷組件名稱等于空白圖片名稱的個(gè)數(shù) ? ? ? ? ? ? ? ? ? ? ? ? count++; ? ? ? ? ? ? ? ? ? ? ? ? if(count==35){ ? ? ? ? ? ? ? ? ? ? ? ? ? ? //如果到達(dá)全部圖片則游戲勝利 ? ? ? ? ? ? ? ? ? ? ? ? ? ? JOptionPane.showMessageDialog(panel_01,"你贏了"); ? ? ? ? ? ? ? ? ? ? ? ? ? ? System.exit(0); ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? }else{ ? ? ? ? ? ? ? ? firstbutton.setIcon(temp);//設(shè)置第一張圖片還原 ? ? ? ? ? ? } ? ? ? ? } ? ? } }
然后創(chuàng)建主類運(yùn)行即可
package linkgame; public class Play { ? ? public static void main(String[] args) { ? ? ? ? LinkGame linkGame=new LinkGame(); ? ? } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java番外雜談之每天掃的二維碼你了解它內(nèi)含的信息嗎
二維碼已經(jīng)成為我們?nèi)粘I钪斜夭豢缮俚慕M成部分了,登錄需要掃一掃二維碼、買東西付錢需要掃一掃二維碼、開會(huì)簽到也需要掃一掃二維碼,那么如此使用的二維碼技術(shù),背后的原理是怎樣的呢?本文將結(jié)合二維碼的發(fā)展歷程以及典型應(yīng)用場(chǎng)景,分析二維碼背后的技術(shù)原理2022-02-02詳解Idea 2019.2 安裝lombok插件失效問題解決
這篇文章主要介紹了詳解Idea 2019.2 安裝lombok插件失效問題解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10利用Jackson實(shí)現(xiàn)數(shù)據(jù)脫敏的示例詳解
在我們的企業(yè)項(xiàng)目中,為了保護(hù)用戶隱私,數(shù)據(jù)脫敏成了必不可少的操作,那么我們?cè)趺磧?yōu)雅的利用Jackson實(shí)現(xiàn)數(shù)據(jù)脫敏呢,本文就來和大家詳細(xì)聊聊,希望對(duì)大家有所幫助2023-05-05解決IDEA中Maven項(xiàng)目中JSTL標(biāo)簽無效問題
這篇文章主要介紹了關(guān)于IDEA中Maven項(xiàng)目中JSTL標(biāo)簽無效問題的解決方法,需要的朋友可以參考下2018-09-09Spring AOP 實(shí)現(xiàn)自定義注解的示例
這篇文章主要介紹了Spring AOP 實(shí)現(xiàn)自定義注解的示例,幫助大家更好的理解和學(xué)習(xí)使用spring框架,感興趣的朋友可以了解下2021-03-03Java?中的?Lambda?List?轉(zhuǎn)?Map?的多種方法詳解
這篇文章主要介紹了Java中的Lambda?List轉(zhuǎn)Map幾種方式,傳統(tǒng)的方式又顯得太臃腫,于是就想到 Lambda 神器,今天我們就來看看都有哪幾種轉(zhuǎn)換方式(List -> Map),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2022-07-07SpringCloud實(shí)現(xiàn)服務(wù)調(diào)用feign與熔斷hystrix和網(wǎng)關(guān)gateway詳細(xì)分析
這篇文章主要介紹了SpringCloud實(shí)現(xiàn)服務(wù)調(diào)用feign與熔斷hystrix和網(wǎng)關(guān)gateway,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-04-04