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