java swing 扫雷
时间: 2023-12-27 15:01:17 浏览: 43
Java Swing 扫雷是一款基于 Java 编程语言和 Swing 用户界面库开发的经典扫雷游戏。玩家需要在一个由方块组成的游戏板上,根据数字推断出雷的位置,然后用小旗标记出来。游戏的目标是在不触雷的情况下,将所有的方块都揭开。
玩家可以通过点击方块来揭开它们,如果揭开的方块是雷,则游戏结束;如果揭开的方块是数字,则它表示周围八个方块中雷的数量;如果揭开的方块是空白,则会自动扩散揭开周围的方块。玩家可以利用数字提示来推断周围雷的位置,并用小旗标记。
Java Swing 扫雷具有简洁清晰的界面和丰富的游戏功能,包括不同难度的游戏模式、计时器、计分板等。玩家可以根据自己的喜好和能力选择不同的难度进行游戏挑战。游戏中还加入了一些音效和动画效果,增加了游戏的趣味性。
总的来说,Java Swing 扫雷是一款经典的益智游戏,具有简单易操作的特点,适合各个年龄段的玩家进行休闲娱乐。它不仅能训练玩家的逻辑思维能力,还能带来愉悦的游戏体验。
相关问题
给我一个使用了Java swing的扫雷游戏Java源码
好的,下面是一个使用Java Swing实现的扫雷游戏的源码示例:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;
public class MineSweeper implements ActionListener {
private JFrame mainFrame;
private JPanel mainPanel, buttonPanel;
private JButton[][] buttonArray;
private int[][] mineArray;
private boolean[][] clickedArray;
private int size, numMines;
private Random random;
public MineSweeper(int size, int numMines) {
this.size = size;
this.numMines = numMines;
this.random = new Random();
this.mineArray = new int[size][size];
this.clickedArray = new boolean[size][size];
mainFrame = new JFrame("Mine Sweeper");
mainPanel = new JPanel();
buttonPanel = new JPanel();
buttonArray = new JButton[size][size];
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
buttonArray[i][j] = new JButton();
buttonArray[i][j].setPreferredSize(new Dimension(25, 25));
buttonArray[i][j].addActionListener(this);
buttonPanel.add(buttonArray[i][j]);
}
}
mainPanel.add(buttonPanel);
mainFrame.add(mainPanel);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.pack();
mainFrame.setVisible(true);
generateMines(numMines);
}
private void generateMines(int numMines) {
int count = 0;
while (count < numMines) {
int x = random.nextInt(size);
int y = random.nextInt(size);
if (mineArray[x][y] == 0) {
mineArray[x][y] = 1;
count++;
}
}
}
public void actionPerformed(ActionEvent event) {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (event.getSource() == buttonArray[i][j]) {
if (mineArray[i][j] == 1) {
buttonArray[i][j].setText("M");
JOptionPane.showMessageDialog(mainFrame, "You lose!");
System.exit(0);
} else {
int count = countMines(i, j);
buttonArray[i][j].setText("" + count);
clickedArray[i][j] = true;
if (count == 0) {
expand(i, j);
}
if (checkWin()) {
JOptionPane.showMessageDialog(mainFrame, "You win!");
System.exit(0);
}
}
}
}
}
}
private int countMines(int x, int y) {
int count = 0;
for (int i = x - 1; i <= x + 1; i++) {
for (int j = y - 1; j <= y + 1; j++) {
if (i >= 0 && i < size && j >= 0 && j < size) {
if (mineArray[i][j] == 1) {
count++;
}
}
}
}
return count;
}
private void expand(int x, int y) {
for (int i = x - 1; i <= x + 1; i++) {
for (int j = y - 1; j <= y + 1; j++) {
if (i >= 0 && i < size && j >= 0 && j < size) {
if (!clickedArray[i][j]) {
int count = countMines(i, j);
buttonArray[i][j].setText("" + count);
clickedArray[i][j] = true;
if (count == 0) {
expand(i, j);
}
}
}
}
}
}
private boolean checkWin() {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (!clickedArray[i][j]) {
if (mineArray[i][j] != 1) {
return false;
}
}
}
}
return true;
}
public static void main(String[] args) {
new MineSweeper(10, 10);
}
}
```
这个扫雷游戏的源码使用了Java Swing库来实现图形界面,使用了JButton来表示每个格子,使用了JOptionPane来弹出游戏胜利或失败的提示框。游戏开始时,程序会生成一个size * size的二维数组,其中1表示雷,0表示非雷。玩家需要点击每个格子来揭示其内容,如果揭示到了雷则游戏失败,如果揭示到的格子周围有雷,则格子上会显示雷的数量。当玩家已经揭示了所有非雷格子时,游戏胜利。
javaswing小游戏
好的,我可以为您提供一些Java Swing小游戏的示例代码,您可以在此基础上进行修改和扩展。
1. 猜数字游戏
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GuessNumberGame extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JTextField textField;
private JLabel label;
private int answer;
private int tries;
public GuessNumberGame() {
super("Guess Number Game");
setLayout(new FlowLayout());
answer = (int) (Math.random() * 100) + 1;
tries = 0;
JLabel promptLabel = new JLabel("I'm thinking of a number between 1 and 100. Try to guess it.");
add(promptLabel);
textField = new JTextField(10);
textField.addActionListener(this);
add(textField);
JButton guessButton = new JButton("Guess");
guessButton.addActionListener(this);
add(guessButton);
label = new JLabel("");
add(label);
}
public void actionPerformed(ActionEvent e) {
try {
int guess = Integer.parseInt(textField.getText().trim());
if (guess < 1 || guess > 100)
throw new NumberFormatException("Number must be between 1 and 100");
tries++;
if (guess == answer) {
label.setText("Congratulations! You guessed it in " + tries + " tries.");
textField.setEditable(false);
} else if (guess < answer) {
label.setText(guess + " is too low. Try again.");
} else {
label.setText(guess + " is too high. Try again.");
}
} catch (NumberFormatException ex) {
label.setText("Invalid input: " + textField.getText());
}
textField.setText("");
}
public static void main(String[] args) {
GuessNumberGame game = new GuessNumberGame();
game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
game.setSize(400, 150);
game.setVisible(true);
}
}
```
2. 扫雷游戏
```java
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
public class MinesweeperGame extends JFrame implements ActionListener, MouseListener {
private static final long serialVersionUID = 1L;
private static final int SIZE = 10;
private static final int MINES = 10;
private JButton[][] buttons;
private boolean[][] mines;
private boolean[][] flags;
private int[][] counts;
public MinesweeperGame() {
super("Minesweeper Game");
setLayout(new GridLayout(SIZE, SIZE));
buttons = new JButton[SIZE][SIZE];
mines = new boolean[SIZE][SIZE];
flags = new boolean[SIZE][SIZE];
counts = new int[SIZE][SIZE];
Random random = new Random();
int count = 0;
while (count < MINES) {
int row = random.nextInt(SIZE);
int col = random.nextInt(SIZE);
if (!mines[row][col]) {
mines[row][col] = true;
count++;
}
}
for (int row = 0; row < SIZE; row++) {
for (int col = 0; col < SIZE; col++) {
buttons[row][col] = new JButton();
buttons[row][col].addActionListener(this);
buttons[row][col].addMouseListener(this);
add(buttons[row][col]);
}
}
setSize(400, 400);
setVisible(true);
}
private void gameOver(boolean win) {
for (int row = 0; row < SIZE; row++) {
for (int col = 0; col < SIZE; col++) {
if (mines[row][col]) {
buttons[row][col].setText("*");
} else if (flags[row][col]) {
buttons[row][col].setText("F");
}
buttons[row][col].setEnabled(false);
}
}
JOptionPane.showMessageDialog(this, win ? "You win!" : "You lose.");
System.exit(0);
}
private void reveal(int row, int col) {
if (row < 0 || row >= SIZE || col < 0 || col >= SIZE || counts[row][col] > 0 || flags[row][col])
return;
buttons[row][col].setEnabled(false);
if (mines[row][col]) {
gameOver(false);
} else {
int count = 0;
for (int r = row - 1; r <= row + 1; r++) {
for (int c = col - 1; c <= col + 1; c++) {
if (r >= 0 && r < SIZE && c >= 0 && c < SIZE && mines[r][c])
count++;
}
}
counts[row][col] = count;
buttons[row][col].setText(Integer.toString(count));
buttons[row][col].setForeground(Color.BLUE);
if (count == 0) {
for (int r = row - 1; r <= row + 1; r++) {
for (int c = col - 1; c <= col + 1; c++) {
reveal(r, c);
}
}
}
}
}
public void actionPerformed(ActionEvent e) {
JButton button = (JButton) e.getSource();
for (int row = 0; row < SIZE; row++) {
for (int col = 0; col < SIZE; col++) {
if (buttons[row][col] == button) {
reveal(row, col);
return;
}
}
}
}
public void mousePressed(MouseEvent e) {
JButton button = (JButton) e.getSource();
for (int row = 0; row < SIZE; row++) {
for (int col = 0; col < SIZE; col++) {
if (buttons[row][col] == button) {
if (e.getButton() == MouseEvent.BUTTON1) {
reveal(row, col);
} else if (e.getButton() == MouseEvent.BUTTON3) {
flags[row][col] = !flags[row][col];
if (flags[row][col]) {
buttons[row][col].setText("F");
buttons[row][col].setForeground(Color.RED);
} else {
buttons[row][col].setText("");
}
}
return;
}
}
}
}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
public static void main(String[] args) {
MinesweeperGame game = new MinesweeperGame();
game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
```
希望这些代码能够帮助您开始编写Java Swing小游戏。