用java写一个拼图游戏
时间: 2023-05-21 17:06:35 浏览: 64
好的,以下是一个简单的基于 Java 的拼图游戏代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class PuzzleGame extends JFrame implements ActionListener {
private JButton[][] puzzleBoard;
private ImageIcon[][] puzzlePieces;
private JButton startButton, resetButton;
private JPanel puzzlePanel, controlPanel;
private final int rows = 3;
private final int cols = 3;
public PuzzleGame() {
setTitle("Puzzle Game");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 300);
puzzleBoard = new JButton[rows][cols];
puzzlePieces = new ImageIcon[rows][cols];
puzzlePanel = new JPanel(new GridLayout(rows, cols));
controlPanel = new JPanel();
startButton = new JButton("Start");
resetButton = new JButton("Reset");
startButton.addActionListener(this);
resetButton.addActionListener(this);
controlPanel.add(startButton);
controlPanel.add(resetButton);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
puzzleBoard[i][j] = new JButton();
puzzlePieces[i][j] = new ImageIcon(getClass().getResource("/puzzle/" + (i * cols + j + 1) + ".jpg"));
puzzleBoard[i][j].setIcon(puzzlePieces[i][j]);
puzzlePanel.add(puzzleBoard[i][j]);
}
}
add(puzzlePanel, BorderLayout.CENTER);
add(controlPanel, BorderLayout.SOUTH);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == startButton) {
int[] randNums = new int[rows * cols];
boolean[] used = new boolean[rows * cols];
for (int i = 0; i < randNums.length; i++) {
do {
randNums[i] = (int) (Math.random() * randNums.length);
} while (used[randNums[i]]);
used[randNums[i]] = true;
}
for (int i = 0; i < randNums.length; i++) {
puzzleBoard[i / cols][i % cols].setIcon(puzzlePieces[randNums[i] / cols][randNums[i] % cols]);
}
} else if (e.getSource() == resetButton) {
for (int i = 0; i < rows * cols; i++) {
puzzleBoard[i / cols][i % cols].setIcon(puzzlePieces[i / cols][i % cols]);
}
}
}
public static void main(String[] args) {
new PuzzleGame();
}
}
该代码会在窗口中生成一个 3x3 的拼图游戏板。点击 "Start" 按钮会生成一个随机的拼图,点击 "Reset" 按钮会重新生成一个已经排列好的拼图。可以替换图片资源中的图片,以获得不同的难度级别。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)