写一个有图片的Java对对碰游戏
时间: 2023-12-06 20:45:42 浏览: 36
以下是一个简单的Java 对对碰游戏,使用Java Swing库来实现图形界面和事件处理。
首先,我们需要创建一个游戏面板,用于显示游戏界面和处理用户的操作。游戏面板需要继承自JPanel类,并实现MouseListener接口来处理鼠标事件。
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GamePanel extends JPanel implements MouseListener {
private int ROWS = 4;
private int COLS = 5;
private int NUM_IMAGES = 10;
private int TILE_SIZE = 80;
private int[][] board = new int[ROWS][COLS];
private boolean[][] revealed = new boolean[ROWS][COLS];
private int firstRow = -1;
private int firstCol = -1;
private int secondRow = -1;
private int secondCol = -1;
public GamePanel() {
setPreferredSize(new Dimension(COLS*TILE_SIZE, ROWS*TILE_SIZE));
addMouseListener(this);
initBoard();
}
private void initBoard() {
// initialize board with random images
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
board[i][j] = (int) (Math.random() * NUM_IMAGES);
}
}
}
private void drawTile(Graphics g, int row, int col) {
int x = col * TILE_SIZE;
int y = row * TILE_SIZE;
if (revealed[row][col]) {
// draw image
ImageIcon icon = new ImageIcon("images/" + board[row][col] + ".png");
g.drawImage(icon.getImage(), x, y, TILE_SIZE, TILE_SIZE, null);
} else {
// draw back of tile
g.setColor(Color.gray);
g.fillRect(x, y, TILE_SIZE, TILE_SIZE);
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
drawTile(g, i, j);
}
}
}
public void mouseClicked(MouseEvent e) {
int row = e.getY() / TILE_SIZE;
int col = e.getX() / TILE_SIZE;
if (row >= ROWS || col >= COLS) {
return;
}
if (revealed[row][col]) {
return;
}
if (firstRow == -1) {
// first tile clicked
firstRow = row;
firstCol = col;
revealed[firstRow][firstCol] = true;
repaint();
} else if (secondRow == -1) {
// second tile clicked
secondRow = row;
secondCol = col;
revealed[secondRow][secondCol] = true;
repaint();
if (board[firstRow][firstCol] == board[secondRow][secondCol]) {
// tiles match
firstRow = -1;
firstCol = -1;
secondRow = -1;
secondCol = -1;
} else {
// tiles don't match, hide them after a delay
Timer timer = new Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
revealed[firstRow][firstCol] = false;
revealed[secondRow][secondCol] = false;
firstRow = -1;
firstCol = -1;
secondRow = -1;
secondCol = -1;
repaint();
}
});
timer.setRepeats(false);
timer.start();
}
}
}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
}
```
在游戏面板中,我们使用一个二维数组来存储游戏面板上每个格子的图片编号。另外,我们还需要一个二维数组来记录每个格子是否已经被翻开。当用户点击一个未翻开的格子时,我们先判断是不是第一次点击,如果是,则将该格子设置为已翻开,并记录其行列号。如果是第二次点击,则将该格子也设置为已翻开,并记录其行列号。如果两次点击的格子图片相同,则匹配成功,否则需要在一段时间后将两个格子的图片翻回去。
接下来,我们需要创建一个主程序,用于创建游戏窗口和游戏面板。
```java
import javax.swing.*;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("Matching Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.add(new GamePanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
```
最后,我们还需要准备一些图片,用于显示在游戏面板上。在本例中,我们使用了10张图片,分别存储在images/0.png到images/9.png文件中。你可以替换这些图片为自己喜欢的图片。
这就是一个简单的Java对对碰游戏的实现。你可以根据自己的需要进行修改和扩展。