Java實(shí)現(xiàn)簡易拼圖游戲的方法詳解
更新時間:2022年05月12日 09:21:41 作者:錯過了時間
這篇文章主要介紹了如何利用Java語言實(shí)現(xiàn)簡易拼圖游戲,幫助大家更好的理解和使用Java開發(fā)游戲,感興趣的朋友可以跟隨小編一起學(xué)習(xí)一下
效果展示
介紹:游戲共有五張圖片可以選擇,分成了4 X 4 十六個方格,點(diǎn)擊開始就可以開始游戲。游戲運(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)建面板對象,并增加邊框、布局
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è)加個面板,添加兩個按鈕
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)建按鈕開局添加監(jiān)聽
final JButton startButton = new JButton();
startButton.setText("開局");
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;
}
//游戲時排列圖片
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;
}
}
}
}
//開局排列圖片
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)簡易拼圖游戲的方法詳解的詳細(xì)內(nèi)容,更多關(guān)于Java拼圖游戲的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
idea項(xiàng)目的左側(cè)目錄沒了如何設(shè)置
這篇文章主要介紹了idea項(xiàng)目的左側(cè)目錄沒了如何設(shè)置的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02
SpringBoot實(shí)現(xiàn)多數(shù)據(jù)源配置的示例詳解
這篇文章主要為大家詳細(xì)介紹了SpringBoot實(shí)現(xiàn)多數(shù)據(jù)源配置的相關(guān)知識,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-12-12
關(guān)于spring.factories的常用配置項(xiàng)說明
這篇文章主要介紹了關(guān)于spring.factories的常用配置項(xiàng)說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07
SpringSecurity 默認(rèn)表單登錄頁展示流程源碼
本篇主要講解 SpringSecurity提供的默認(rèn)表單登錄頁 它是如何展示流程,本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友參考下吧2020-01-01

