扫雷游戏GUI

public class Cell {
    private boolean isBoom;
    private int boomCount;
    private boolean isFlag;
    private boolean isOpen;

    private MyButton myButton;
    private MouseAdapter mouseAdapter;

    public MouseAdapter getMouseAdapter() {
        return mouseAdapter;
    }

    public void setMouseAdapter(MouseAdapter mouseAdapter) {
        this.mouseAdapter = mouseAdapter;
    }

    public MyButton getMyButton() {
        return myButton;
    }

    public void setMyButton(MyButton myButton) {
        this.myButton = myButton;
    }

    public Cell(boolean isBoom, int boomCount, boolean isFlag, boolean isOpen) {
        this.isBoom = isBoom;
        this.boomCount = boomCount;
        this.isFlag = isFlag;
        this.isOpen = isOpen;
    }

    public boolean isOpen() {
        return isOpen;
    }

    public void setOpen(boolean open) {
        isOpen = open;
    }

    public Cell() {
    }

    public boolean isBoom() {
        return isBoom;
    }

    public void setBoom(boolean boom) {
        isBoom = boom;
    }

    public int getBoomCount() {
        return boomCount;
    }

    public void setBoomCount(int boomCount) {
        this.boomCount = boomCount;
    }

    public boolean isFlag() {
        return isFlag;
    }

    public void setFlag(boolean flag) {
        isFlag = flag;
    }
}
public class MyButton extends JButton {
    private int row;
    private int col;

    public MyButton(int row, int col) {
        this.row = row;
        this.col = col;
    }

    public MyButton() {
    }

    public int getRow() {
        return row;
    }

    public void setRow(int row) {
        this.row = row;
    }

    public int getCol() {
        return col;
    }

    public void setCol(int col) {
        this.col = col;
    }
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

public class ClearBoomFrame extends JFrame {

    Cell[][] cells = new Cell[25][25];
    MouseAdapter mouseAdapter;
    int totalCount = 25 * 25;

    int bombCount = 50;
    int count = 0;
    JLabel countTxt1;
    JLabel countTxt2;
    int minute = 0;
    int second = 0;
    Timer timer;

    public ClearBoomFrame() {
        initWindow();
        initCells();
        InitContent();
        this.setVisible(true);
    }

    private void printCells() {
        for (int i = 0; i < cells.length; i++) {
            for (int j = 0; j < cells[i].length; j++) {
                System.out.print(cells[i][j].isBoom() ? "*": cells[i][j].getBoomCount() );
            }
            System.out.println();
        }
        System.out.println("=============================");
    }
    private void initStart() {
        timer.cancel();
        count = 0;
        minute = 0;
        second = 0;
        //清空窗口
        ClearBoomFrame.this.getContentPane().removeAll();
        initCells();
        InitContent();
        repaint();
        setVisible(true);
    }

    private void initMenu() {
        JFrame frame = new JFrame("系统菜单示例");
        frame.setSize(50, 150);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JMenuBar menuBar = new JMenuBar();// 创建菜单条
        //设置菜单条大小
        menuBar.setPreferredSize(new Dimension(50, 20));
        JMenu systemMenu = new JMenu("系统");// 创建“系统”菜单

        // 创建“重启”菜单项
        JMenuItem restartMenuItem = new JMenuItem("重启");
        restartMenuItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int response = JOptionPane.showConfirmDialog(frame,
                        "确定要重启应用程序吗?", "重启",
                        JOptionPane.YES_NO_OPTION);
                if (response == JOptionPane.YES_OPTION) {
                    // 这里可以添加重新启动的逻辑
                    removeEvent();
                    initStart();
                    JOptionPane.showMessageDialog(frame, "应用程序正在重启...");
                    // 在实际应用中,你可以通过重启逻辑重新加载当前应用
                }
            }
        });

        // 创建“退出”菜单项
        JMenuItem exitMenuItem = new JMenuItem("退出");
        exitMenuItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int response = JOptionPane.showConfirmDialog(frame,
                        "确定要退出应用程序吗?", "退出",
                        JOptionPane.YES_NO_OPTION);
                if (response == JOptionPane.YES_OPTION) {
                    System.exit(0); // 关闭当前应用
                }
            }
        });


        JMenuItem difficultMenuItem = new JMenuItem("加难");
        difficultMenuItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (bombCount <= 300) {
                    bombCount += 50;
                    removeEvent();
                    initStart();
                    System.out.println("当前难度为:" + bombCount);
                }else {
                    JOptionPane.showMessageDialog(frame, "已经到达最大难度,无法再增加难度!");
                }
            }
        });


        // 将菜单项添加到“系统”菜单中
        systemMenu.add(restartMenuItem);
        systemMenu.add(exitMenuItem);
        systemMenu.add(difficultMenuItem);
        // 将“系统”菜单添加到菜单条中
        menuBar.add(systemMenu);
        // 设置菜单条到主窗口
        setJMenuBar(menuBar);
    }


    //添加计时器
    private void initTimer() {
        countTxt2= new JLabel(",已用时"+minute + ":" + second);
        countTxt2.setBounds(400, 7, 100, 15);
        //设置时钟文字的颜色为紫色
        Font font = new Font("仿宋", Font.BOLD, 13);
        countTxt2.setFont(font);
        //给font设置颜色
        countTxt2.setForeground(Color.RED);
        this.add(countTxt2);
        //设置定时器, 每一秒执行一次
        timer = new Timer();
        timer.schedule(new TimerTask(){
            public void run() {
                second++;
                if (second == 60) {
                    minute++;
                    second = 0;

                }
                if (minute == 99 && second == 59) {
                    timer.cancel();
                }
               countTxt2.setText(",已用时"+minute + ":" + second);
            }
        }, 1000, 1000);

    }

    private void removeEvent() {
        // 移除事件监听器,防止重复监听
        for (int i = 0; i < cells.length; i++) {
            for (int j = 0; j < cells[i].length; j++) {
                MyButton button = cells[i][j].getMyButton();
                button.removeMouseListener(cells[i][j].getMouseAdapter());
            }
        }
        ;
    }


    private void InitContent() {
        initMenu();
        initTimer();
        printCells();

        countTxt1 = new JLabel("已经打开" + count + "个格子");
        countTxt1.setBounds(300, 5, 100, 15);
        countTxt1.setForeground(Color.RED); // 设置文字颜色为红色
        countTxt1.setFont(new Font("楷体", Font.BOLD, 12)); // 设置字体
        this.add(countTxt1);
        // 显示窗口

        for (int i = 0; i < 25; i++) {
            for (int j = 0; j < 25; j++) {
                //设置20行20列的按钮
                MyButton button = new MyButton(i, j);
                cells[i][j].setMyButton(button);
                button.setBounds(j * 21 + 12, i * 21 + 26, 20, 20);
                //设置内边距
                button.setMargin(new Insets(0, 0, 0, 0));
                button.setBackground(Color.decode("#e0cff4"));
                this.add(button);

                mouseAdapter = new MouseAdapter() {
                    @Override
                    public void mousePressed(MouseEvent e) {
                        MyButton myButton = (MyButton) e.getSource();
                        int row = myButton.getRow();
                        int col = myButton.getCol();
                        if (e.getButton() == MouseEvent.BUTTON1) {
                            if (cells[row][col].isFlag() ||cells[row][col].isOpen()) {
                                return;
                            }
                            if (cells[row][col].isBoom()) {
                                myButton.setText("🤡");
                                cells[row][col].setOpen(true);
                                timer.cancel();
                                removeEvent();
                                JOptionPane.showMessageDialog(null, "Game Over", "提示", JOptionPane.ERROR_MESSAGE);

                            } else {
                                if (cells[row][col].getBoomCount() != 0) {
                                    count++;
                                    cells[row][col].setOpen(true);
                                    myButton.setText(cells[row][col].getBoomCount() + "");
                                    myButton.setBackground(Color.decode("#e8ca13"));
                                } else {
                                    openAroundCells(row, col);
                                }
                            }

                        }else if(e.getButton() == MouseEvent.BUTTON3){
                            if (!cells[row][col].isOpen()) {
                                cells[row][col].setFlag(!cells[row][col].isFlag());
                                if (cells[row][col].isFlag()) {
                                    myButton.setText("🚩");
                                } else {
                                    myButton.setText("");
                                }
                            }
                        }
                        countTxt1.setText("您已经走了" + count + "步");
                        if (totalCount == bombCount + count) {
                            removeEvent();
                            JOptionPane.showMessageDialog(null, "恭喜你,游戏胜利!", "提示", JOptionPane.INFORMATION_MESSAGE);
                        }

                    }
                };
                cells[i][j].setMouseAdapter(mouseAdapter);
                button.addMouseListener(mouseAdapter);

            }
        }
    }

    private void initCells() {
        for (int i = 0; i < 25; i++) {
            for (int j = 0; j < 25; j++) {
                cells[i][j] = new Cell();
            }
        }
        int count = 0;
        Random random = new Random();
        int x, y;
        while (count < bombCount) {
            x = random.nextInt(25);
            y = random.nextInt(cells[x].length);
            if (cells[x][y].isBoom()) {
                continue;
            } else {
                cells[x][y].setBoom(true);
                count++;
            }
        }
        for (int i = 0; i < 25; i++) {
            for (int j = 0; j < 25; j++) {

                for (int k = Math.max(i - 1, 0); k <= Math.min(i + 1, cells.length - 1); k++) {
                    for (int l = Math.max(j - 1, 0); l <= Math.min(j + 1, cells[i].length - 1); l++) {
                        if (!(k == i && l == j)) {
                            if (cells[k][l].isBoom())
                                cells[i][j].setBoomCount(cells[i][j].getBoomCount() + 1);
                        }
                    }
                }
            }
        }
    }

    private void openAroundCells(int row, int col) {
        count++;
        cells[row][col].setOpen(true);
        cells[row][col].getMyButton().setText(cells[row][col].getBoomCount() + "");

        int rowUp = row - 1;
        int columnUp = col;
        if (rowUp >= 0 && !cells[rowUp][columnUp].isOpen()) {
            if (cells[rowUp][columnUp].getBoomCount() == 0)
                openAroundCells(rowUp, columnUp);
            else {
                count++;
                cells[rowUp][columnUp].setOpen(true);
                cells[rowUp][columnUp].getMyButton().setText(cells[rowUp][columnUp].getBoomCount() + "");
                cells[rowUp][columnUp].getMyButton().setBackground(Color.decode("#e8ca13"));
            }
        }

        int rowDown = row + 1;
        int columnDown = col;
        if (rowDown <= cells.length - 1 && !cells[rowDown][columnDown].isOpen()) {
            if (cells[rowDown][columnDown].getBoomCount() == 0)
                openAroundCells(rowDown, columnDown);
            else {
                count++;
                cells[rowDown][columnDown].setOpen(true);
                cells[rowDown][columnDown].getMyButton().setText(cells[rowDown][columnDown].getBoomCount() + "");
                cells[rowDown][columnDown].getMyButton().setBackground(Color.decode("#e8ca13"));
            }
        }

        int rowLeft = row;
        int columnLeft = col - 1;
        if (columnLeft >= 0 && !cells[rowLeft][columnLeft].isOpen()) {
            if (cells[rowLeft][columnLeft].getBoomCount() == 0)
                openAroundCells(rowLeft, columnLeft);
            else {
                count++;
                cells[rowLeft][columnLeft].setOpen(true);
                cells[rowLeft][columnLeft].getMyButton().setText(cells[rowLeft][columnLeft].getBoomCount() + "");
                cells[rowLeft][columnLeft].getMyButton().setBackground(Color.decode("#e8ca13"));
            }
        }


        int rowRight = row;
        int columnRight = col + 1;
        if (columnRight <= cells[rowRight].length - 1 && !cells[rowRight][columnRight].isOpen()) {
            if (cells[rowRight][columnRight].getBoomCount() == 0)
                openAroundCells(rowRight, columnRight);
            else {
                count++;
                cells[rowRight][columnRight].setOpen(true);
                cells[rowRight][columnRight].getMyButton().setText(cells[rowRight][columnRight].getBoomCount() + "");
                cells[rowRight][columnRight].getMyButton().setBackground(Color.decode("#e8ca13"));
            }
        }

    }

    private void initWindow() {
        //创建窗口
        this.setTitle("扫雷");
        this.setLayout(null);
        this.setSize(565,  630);
        this.setLocationRelativeTo(null);
        this.setResizable(false);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

}
public class Test {
    public static void main(String[] args) {
        new ClearBoomFrame();
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值