如何用Java写一个拼图单机游戏
时间: 2023-05-31 08:06:36 浏览: 291
实现一个拼图游戏需要以下步骤:
1.创建一个JFrame窗口并设置窗口大小和标题。
2.创建一个JPanel面板来表示游戏区域。
3.在JPanel面板中加载拼图图片并将其分割成多个小图片块。
4.将小图片块随机分布在游戏区域中。
5.为每个小图片块添加鼠标监听器,使其能够被拖动。
6.实现小图片块拖动时的交换,即如果两个小图片块之间的距离小于一个阈值,则交换它们的位置。
7.添加一个计时器,用于记录游戏时间。
8.添加一个计分器,用于记录玩家的得分。
9.实现游戏结束的判断,即当所有小图片块排列正确时,游戏结束。
下面是一个基本的Java拼图游戏代码实现:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class PuzzleGame extends JFrame {
private JPanel gamePanel;
private JLabel timeLabel;
private JLabel scoreLabel;
private int score;
private Timer timer;
public PuzzleGame() {
setTitle("拼图游戏");
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gamePanel = new JPanel();
gamePanel.setLayout(new GridLayout(4, 4));
loadImages();
add(gamePanel, BorderLayout.CENTER);
JPanel infoPanel = new JPanel();
infoPanel.setLayout(new GridLayout(1, 2));
timeLabel = new JLabel("时间:0秒");
infoPanel.add(timeLabel);
scoreLabel = new JLabel("得分:0分");
infoPanel.add(scoreLabel);
add(infoPanel, BorderLayout.SOUTH);
timer = new Timer(1000, new TimerListener());
timer.start();
}
private void loadImages() {
ImageIcon icon = new ImageIcon("puzzle.jpg");
Image img = icon.getImage();
int w = img.getWidth(null) / 4;
int h = img.getHeight(null) / 4;
for (int i = 0; i < 16; i++) {
int x = (i % 4) * w;
int y = (i / 4) * h;
Image subImg = img.getSubimage(x, y, w, h);
PuzzlePiece piece = new PuzzlePiece(new ImageIcon(subImg));
piece.addMouseListener(new PieceListener());
gamePanel.add(piece);
}
}
private void checkWin() {
boolean win = true;
for (Component c : gamePanel.getComponents()) {
PuzzlePiece piece = (PuzzlePiece)c;
if (!piece.isInRightPosition()) {
win = false;
break;
}
}
if (win) {
timer.stop();
JOptionPane.showMessageDialog(this, "恭喜你完成拼图!");
}
}
private class PieceListener extends MouseAdapter {
private PuzzlePiece selectedPiece;
public void mousePressed(MouseEvent e) {
selectedPiece = (PuzzlePiece)e.getComponent();
selectedPiece.setSelected(true);
}
public void mouseReleased(MouseEvent e) {
PuzzlePiece releasedPiece = (PuzzlePiece)e.getComponent();
if (selectedPiece != null && selectedPiece != releasedPiece) {
int dx = selectedPiece.getPosition().x - releasedPiece.getPosition().x;
int dy = selectedPiece.getPosition().y - releasedPiece.getPosition().y;
if (Math.sqrt(dx*dx + dy*dy) < 50) {
Point temp = selectedPiece.getPosition();
selectedPiece.setPosition(releasedPiece.getPosition());
releasedPiece.setPosition(temp);
score += 10;
scoreLabel.setText("得分:" + score + "分");
checkWin();
}
}
selectedPiece.setSelected(false);
selectedPiece = null;
}
}
private class TimerListener implements ActionListener {
private int seconds;
public void actionPerformed(ActionEvent e) {
seconds++;
timeLabel.setText("时间:" + seconds + "秒");
}
}
public static void main(String[] args) {
PuzzleGame game = new PuzzleGame();
game.setVisible(true);
}
}
class PuzzlePiece extends JLabel {
private boolean selected;
private boolean rightPosition;
public PuzzlePiece(ImageIcon icon) {
super(icon);
selected = false;
rightPosition = false;
setBorder(BorderFactory.createLineBorder(Color.BLACK));
}
public boolean isInRightPosition() {
return rightPosition;
}
public void setInRightPosition(boolean b) {
rightPosition = b;
}
public Point getPosition() {
return getLocation();
}
public void setPosition(Point p) {
setLocation(p);
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean b) {
selected = b;
if (selected) {
setBorder(BorderFactory.createLineBorder(Color.RED, 3));
} else {
setBorder(BorderFactory.createLineBorder(Color.BLACK));
}
}
}
```
运行该代码,即可看到一个基本的拼图游戏界面,玩家可以通过拖动小图片块来完成拼图。游戏结束后,会弹出一个恭喜框,告诉玩家游戏已经完成。
阅读全文