Java實(shí)現(xiàn)簡(jiǎn)易拼圖游戲的方法詳解
更新時(shí)間:2022年05月12日 09:21:41 作者:錯(cuò)過(guò)了時(shí)間
這篇文章主要介紹了如何利用Java語(yǔ)言實(shí)現(xiàn)簡(jiǎn)易拼圖游戲,幫助大家更好的理解和使用Java開(kāi)發(fā)游戲,感興趣的朋友可以跟隨小編一起學(xué)習(xí)一下
效果展示
介紹:游戲共有五張圖片可以選擇,分成了4 X 4 十六個(gè)方格,點(diǎn)擊開(kāi)始就可以開(kāi)始游戲。游戲運(yùn)行的截圖如下:
游戲結(jié)構(gòu)
實(shí)現(xiàn)代碼
代碼如下:MedleyGame.java類
package game.medleyPicture; import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.border.TitledBorder; public class MedleyGame extends JFrame { private JLabel modelLabel; private JPanel centerPanel; private JButton emptyButton; int num = 0; public static void main(String[] args) { try { MedleyGame frame = new MedleyGame(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } //建立窗口構(gòu)造方法 public MedleyGame() { super(); setResizable(false); setTitle("拼圖游戲"); setBounds(100, 100, 370, 525); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //創(chuàng)建面板對(duì)象,并增加邊框、布局 final JPanel topPanel = new JPanel(); topPanel.setBorder(new TitledBorder(null, "", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null)); topPanel.setLayout(new BorderLayout()); getContentPane().add(topPanel, BorderLayout.NORTH);//放于上方 //創(chuàng)建標(biāo)簽放原圖 modelLabel = new JLabel(); modelLabel.setIcon(new ImageIcon("image/"+ num+ "model.jpg")); topPanel.add(modelLabel, BorderLayout.WEST); //在右側(cè)加個(gè)面板,添加兩個(gè)按鈕 JPanel eastPanel = new JPanel(); topPanel.add(eastPanel,BorderLayout.CENTER); eastPanel.setLayout(new BorderLayout()); JButton nextButton = new JButton(); nextButton.setText("下一張"); nextButton.addActionListener(new NextButtonAction()); eastPanel.add(nextButton,BorderLayout.NORTH); //創(chuàng)建按鈕開(kāi)局添加監(jiān)聽(tīng) final JButton startButton = new JButton(); startButton.setText("開(kāi)局"); startButton.addActionListener(new StartButtonAction()); eastPanel.add(startButton, BorderLayout.CENTER); //初始化中心面板,設(shè)置邊框,添加按鈕 centerPanel = new JPanel(); centerPanel.setBorder(new TitledBorder(null, "", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null)); centerPanel.setLayout(new GridLayout(4, 0)); getContentPane().add(centerPanel, BorderLayout.CENTER); //初始化圖片 String[][] exactnessOrder = order(); //按排列添加按鈕,設(shè)置圖片 for (int row=0; row<4; row++) { for (int col=0; col<4; col++) { final JButton button = new JButton(); button.setName(row+""+col); button.setIcon(new ImageIcon(exactnessOrder[row][col])); if (exactnessOrder[row][col].equals("image/"+ num+"00.jpg")) emptyButton = button; button.addActionListener(new ImgButtonAction()); centerPanel.add(button); } } } //初始化圖片 private String[][] order() { String[][] exactnessOrder = new String[4][4]; for (int row=0; row<4; row++) { for (int col=0; col<4; col++) { exactnessOrder[row][col] = "image/"+ num+ row+ col+ ".jpg"; } } return exactnessOrder; } //隨機(jī)排列圖片 private String[][] reorder() { String[][] exactnessOrder = new String[4][4]; for (int row=0; row<4; row++) { for (int col=0; col<4; col++) { exactnessOrder[row][col] = "image/"+ num+ row+ col+ ".jpg"; } } String[][] stochasticOrder = new String[4][4]; for (int row=0; row<4; row++) { for (int col=0; col<4; col++) { while (stochasticOrder[row][col]==null) { int r = (int) (Math.random()*4); int c = (int) (Math.random()*4); if (exactnessOrder[r][c] != null) { stochasticOrder[row][col] = exactnessOrder[r][c]; exactnessOrder[r][c] = null; } } } } return stochasticOrder; } //游戲時(shí)排列圖片 class ImgButtonAction implements ActionListener { public void actionPerformed(ActionEvent e) { String emptyName= emptyButton.getName(); char emptyRow = emptyName.charAt(0); char emptyCol = emptyName.charAt(1); JButton clickButton = (JButton) e.getSource(); String clickName = clickButton.getName(); char clickRow = clickName.charAt(0); char clickCol = clickName.charAt(1); if (Math.abs(clickRow - emptyRow) + Math.abs(clickCol - emptyCol) == 1) { emptyButton.setIcon(clickButton.getIcon()); clickButton.setIcon(new ImageIcon("image/"+ num+ "00.jpg")); emptyButton = clickButton; } } } //換下一張圖片 class NextButtonAction implements ActionListener { public void actionPerformed(ActionEvent e) { if (num==5) { num=0; } else { ++num; } modelLabel.setIcon(new ImageIcon("image/"+num+"model.jpg")); String[][] exactnessOrder = order(); int i= 0; for (int row=0; row<4; row++) { for (int col=0; col<4; col++) { JButton button = (JButton) centerPanel.getComponent(i++); button.setIcon(new ImageIcon(exactnessOrder[row][col])); if(exactnessOrder[row][col].equals("image/"+ num+ "00.jpg")) emptyButton=button; } } } } //開(kāi)局排列圖片 class StartButtonAction implements ActionListener { public void actionPerformed(ActionEvent e) { String[][] stochasticOrder = reorder(); int i= 0; for (int row=0; row<4; row++) { for (int col=0; col<4; col++) { JButton button = (JButton) centerPanel.getComponent(i++); button.setIcon(new ImageIcon(stochasticOrder[row][col])); if(stochasticOrder[row][col].equals("image/"+ num+ "00.jpg")) emptyButton=button; } } } } }
以上就是Java實(shí)現(xiàn)簡(jiǎn)易拼圖游戲的方法詳解的詳細(xì)內(nèi)容,更多關(guān)于Java拼圖游戲的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
java實(shí)現(xiàn)飛機(jī)大戰(zhàn)案例詳解
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)飛機(jī)大戰(zhàn)案例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-03-03微服務(wù)間調(diào)用Retrofit在Spring?Cloud?Alibaba中的使用
這篇文章主要為大家介紹了微服務(wù)間調(diào)用Retrofit在Spring?Cloud?Alibaba中的使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06深入理解Java責(zé)任鏈模式實(shí)現(xiàn)靈活的請(qǐng)求處理流程
本文詳細(xì)介紹了Java中的責(zé)任鏈模式,幫助您理解其工作原理,以及如何在代碼中實(shí)現(xiàn)。該模式可以將請(qǐng)求沿著處理鏈路傳遞,實(shí)現(xiàn)靈活的請(qǐng)求處理流程。通過(guò)本文的學(xué)習(xí),您將獲得在Java應(yīng)用程序中使用責(zé)任鏈模式的知識(shí)和技能2023-04-04