当前位置:首页 » 《随便一记》 » 正文

Java极致还原XP系统经典扫雷——全部代码_guyu的博客

5 人参与  2022年04月06日 11:43  分类 : 《随便一记》  评论

点击全文阅读


Java极致还原XP系统经典扫雷

前言

  最近疫情在家,没有工作上的996压迫着我,使我倍感无聊,不知道这满头秀发该如何消耗。
  闲逛着游戏社区,常常回想起和朋友一起通宵玩游戏的那种快感,现在的我们都各奔东西,跟自己的生活对线,怕是很难再体会到了。。。
  一款扫雷游戏使我眼前一亮,他用了关卡的模式,使我为了通关不断的尝试,不出意外,以我的脑力和运气,成功卡在了最后一关,无力感涌上心头,突然就有了写个外挂的想法。
  但这是不对的,我们要尊重游戏制作者,所以我气不过,那就自己做一个扫雷玩,真™机智啊。
  

实现功能

  鼠标左键单击,翻开方格
  鼠标中键点击,标记问号
  鼠标右键单击,标记旗子,旗子只能插和雷相同的数量
  翻开不是雷的话,将连锁翻开周围不是雷的方格
  翻开是雷的话,游戏结束,失败
  翻开雷周围的方块,将显示该方块周围雷的数量
  不能翻开标记了问号和旗子的方格
  游戏界面显示雷的数量、剩余时间和经典小黄脸(时间默认为无限)
  点击小黄脸可重新开始游戏
  

实战演示

  本来想从网上找点素材的,但…就是懒得找了,自己用PS随便画了几个图,感觉还行,勉强能看,需要的话,直接私聊我加Q就行。
在这里插入图片描述

失败

在这里插入图片描述

赢了

在这里插入图片描述

插旗

在这里插入图片描述

标记问号

在这里插入图片描述

  

下面是所有的代码

Test类

public class Test {
    public static void main(String[] args) {
        MyJFrame mj = new MyJFrame();
        mj.myJFrame();
    }
}

MyJFrame类

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

public class MyJFrame extends JFrame implements MouseListener {
    int window_width = 560, window_height = 640;//初始化游戏窗口大小
    static int mine_crosswise = 10, mine_vertical = 10;//初始化雷区为10*10
    static int mine_mun = 10;//初始化雷的数量
    static int sign_mun = 0;//初始化旗子的数量
    static int[][] mine = new int[mine_crosswise][mine_vertical];//保存每个雷的位置
    static int[][] mine1 = new int[mine_crosswise][mine_vertical];//给用户展示的数据
    int[][] sign = new int[mine_crosswise][mine_vertical];//保存每个旗子的位置
    int[][] uncertainty = new int[mine_crosswise][mine_vertical];//保存每个?的位置
    static BufferedImage image = null;//获取图片路径
    static int judge = 0;//判断游戏状态 0游戏中,1赢了,2输了
    static boolean mine_place = false;//绘制雷的开关,判断雷是否以放置,防止多次放置


    //---------------------------------------------------------------------------------------------------------------------
    //窗体
    public void myJFrame() {
        this.setTitle("扫雷"); //标题
        this.setSize(window_width, window_height); //窗口大小
        this.setResizable(false); //窗口是否可以改变大小=否
        this.setDefaultCloseOperation(MyJFrame.EXIT_ON_CLOSE);//窗口关闭方式为关闭窗口同时结束程序
        int width = Toolkit.getDefaultToolkit().getScreenSize().width;//获取屏幕宽度
        int height = Toolkit.getDefaultToolkit().getScreenSize().height;//获取屏幕高度
        this.setLocation((width - window_width) / 2, (height - window_height) / 2); //设置窗口默认位置以屏幕居中
        this.addMouseListener(this);
        this.setVisible(true); //窗口是否显示=是
    }


    //---------------------------------------------------------------------------------------------------------------------
    //覆写paint方法,绘制界面
    public void paint(Graphics g) {

        //双缓冲技术防止屏幕闪烁
        BufferedImage bi = new BufferedImage(window_width, window_height, BufferedImage.TYPE_INT_ARGB);
        Graphics g2 = bi.createGraphics();

        //剩余雷的数量
        g2.setColor(Color.red);//设置画笔颜色
        g2.setFont(new Font("微软雅黑", 10, 30));//设置字体
        g2.drawString("雷数:" + mine_mun, 50, 80);//绘制字符

        //剩余时间
        g2.setColor(Color.red);//设置画笔颜色
        g2.setFont(new Font("微软雅黑", 10, 30));//设置字体
        g2.drawString("时间:无限", window_width - 200, 80);//绘制字符

        //调用放置小黄脸的方法
        again();
        //笑脸位置居中
        g2.drawImage(image, (window_width - 50) / 2, 45, this);


        /*
         * 绘制雷区
         * 根据初始化的雷区数值,循环生成雷区
         * 0、9::没有掀开,1-8:显示周围雷数、-1掀开
         */
        for (int i = 0; i < mine_crosswise; i++) {
            for (int j = 0; j < mine_vertical; j++) {
                if (mine1[i][j] == -1) {
                    try {
                        image = ImageIO.read(new File("D:\\#Java\\个人项目\\MineSweeper\\img\\1.png"));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    g2.drawImage(image, 30 + i * 50, 110 + j * 50, this);
                    continue;
                }
                if (mine1[i][j] == 1) {
                    try {
                        image = ImageIO.read(new File("D:\\#Java\\个人项目\\MineSweeper\\img\\01.png"));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    g2.drawImage(image, 30 + i * 50, 110 + j * 50, this);
                    continue;
                }
                if (mine1[i][j] == 2) {
                    try {
                        image = ImageIO.read(new File("D:\\#Java\\个人项目\\MineSweeper\\img\\02.png"));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    g2.drawImage(image, 30 + i * 50, 110 + j * 50, this);
                    continue;
                }
                if (mine1[i][j] == 3) {
                    try {
                        image = ImageIO.read(new File("D:\\#Java\\个人项目\\MineSweeper\\img\\03.png"));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    g2.drawImage(image, 30 + i * 50, 110 + j * 50, this);
                    continue;
                }
                if (mine1[i][j] == 4) {
                    try {
                        image = ImageIO.read(new File("D:\\#Java\\个人项目\\MineSweeper\\img\\04.png"));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    g2.drawImage(image, 30 + i * 50, 110 + j * 50, this);
                    continue;
                }
                if (mine1[i][j] == 5) {
                    try {
                        image = ImageIO.read(new File("D:\\#Java\\个人项目\\MineSweeper\\img\\05.png"));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    g2.drawImage(image, 30 + i * 50, 110 + j * 50, this);
                    continue;
                }
                if (mine1[i][j] == 6) {
                    try {
                        image = ImageIO.read(new File("D:\\#Java\\个人项目\\MineSweeper\\img\\06.png"));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    g2.drawImage(image, 30 + i * 50, 110 + j * 50, this);
                    continue;
                }
                if (mine1[i][j] == 7) {
                    try {
                        image = ImageIO.read(new File("D:\\#Java\\个人项目\\MineSweeper\\img\\07.png"));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    g2.drawImage(image, 30 + i * 50, 110 + j * 50, this);
                    continue;
                }
                if (mine1[i][j] == 8) {
                    try {
                        image = ImageIO.read(new File("D:\\#Java\\个人项目\\MineSweeper\\img\\08.png"));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    g2.drawImage(image, 30 + i * 50, 110 + j * 50, this);
                    continue;
                }
                if (sign[i][j] == 1) {
                    try {
                        image = ImageIO.read(new File("D:\\#Java\\个人项目\\MineSweeper\\img\\sign.png"));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    g2.drawImage(image, 30 + i * 50, 110 + j * 50, this);
                    continue;
                }
                if (uncertainty[i][j] == 1) {
                    try {
                        image = ImageIO.read(new File("D:\\#Java\\个人项目\\MineSweeper\\img\\uncertainty.png"));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    g2.drawImage(image, 30 + i * 50, 110 + j * 50, this);
                    continue;
                }
                try {
                    image = ImageIO.read(new File("D:\\#Java\\个人项目\\MineSweeper\\img\\0.png"));
                } catch (IOException e) {
                    e.printStackTrace();
                }

                g2.drawImage(image, 30 + i * 50, 110 + j * 50, this);
            }
        }


        /*
         * 绘制游戏结束后显示的雷
         * 根据初始化的雷区数值,循环生成雷区
         * 游戏结束后可显示所有雷位置
         */
        if (judge == 1 || judge == 2) {
            for (int i = 0; i < mine_crosswise; i++) {
                for (int j = 0; j < mine_vertical; j++) {
                    if (mine[i][j] == 9) {
                        try {
                            image = ImageIO.read(new File("D:\\#Java\\个人项目\\MineSweeper\\img\\mine.png"));
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        g2.drawImage(image, 30 + i * 50, 110 + j * 50, this);
                    }
                }

            }
        }

        //调用放置地雷的方法
        place();

        //将以上所述全部绘制在画布上
        g.drawImage(bi, 0, 0, this);
    }

    /**
     * 通过递归算法实现连锁
     * 判断四个边、四个角、和其余的中间部分,如果为零则将其改为-1,代表已翻开
     * 否则将数字显示出来
     */
    public static void scan(int x, int y) {
        //上
        if (x - 1 >= 0 && x + 1 <= mine_crosswise - 1 && y - 1 < 0) {
            if (mine[x - 1][y] == 0) {
                mine1[x - 1][y] = mine[x - 1][y] = -1;
                scan(x - 1, y);
            } else {
                mine1[x - 1][y] = mine[x - 1][y];
            }
            if (mine[x - 1][y + 1] == 0) {
                mine1[x - 1][y + 1] = mine[x - 1][y + 1] = -1;
                scan(x - 1, y + 1);

            } else {
                mine1[x - 1][y + 1] = mine[x - 1][y + 1];

            }

            if (mine[x][y + 1] == 0) {
                mine1[x][y + 1] = mine[x][y + 1] = -1;
                scan(x, y + 1);

            } else {
                mine1[x][y + 1] = mine[x][y + 1];

            }

            if (mine[x + 1][y + 1] == 0) {
                mine1[x + 1][y + 1] = mine[x + 1][y + 1] = -1;
                scan(x + 1, y + 1);

            } else {
                mine1[x + 1][y + 1] = mine[x + 1][y + 1];
            }

            if (mine[x + 1][y] == 0) {
                mine1[x + 1][y] = mine[x + 1][y] = -1;
                scan(x + 1, y);

            } else {
                mine1[x + 1][y] = mine[x + 1][y];
            }
        }
        //下
        if (x - 1 >= 0 && x + 1 <= mine_crosswise - 1 && y + 1 > mine_vertical - 1) {

            if (mine[x - 1][y] == 0) {
                mine1[x - 1][y] = mine[x - 1][y] = -1;
                scan(x - 1, y);

            } else {
                mine1[x - 1][y] = mine[x - 1][y];
            }

            if (mine[x - 1][y - 1] == 0) {
                mine1[x - 1][y - 1] = mine[x - 1][y - 1] = -1;
                scan(x - 1, y - 1);
            } else {
                mine1[x - 1][y - 1] = mine[x - 1][y - 1];
            }

            if (mine[x][y - 1] == 0) {
                mine1[x][y - 1] = mine[x][y - 1] = -1;
                scan(x, y - 1);
            } else {
                mine1[x][y - 1] = mine[x][y - 1];
            }

            if (mine[x + 1][y - 1] == 0) {
                mine1[x + 1][y - 1] = mine[x + 1][y - 1] = -1;
                scan(x + 1, y - 1);
            } else {
                mine1[x + 1][y - 1] = mine[x + 1][y - 1];
            }

            if (mine[x + 1][y] == 0) {
                mine1[x + 1][y] = mine[x + 1][y] = -1;
                scan(x + 1, y);
            } else {
                mine1[x + 1][y] = mine[x + 1][y];
            }
        }
        //左
        if (y - 1 >= 0 && y + 1 <= mine_crosswise - 1 && x - 1 < 0) {

            if (mine[x][y - 1] == 0) {
                mine1[x][y - 1] = mine[x][y - 1] = -1;
                scan(x, y - 1);
            } else {
                mine1[x][y - 1] = mine[x][y - 1];
            }

            if (mine[x + 1][y - 1] == 0) {
                mine1[x + 1][y - 1] = mine[x + 1][y - 1] = -1;
                scan(x + 1, y - 1);
            } else {
                mine1[x + 1][y - 1] = mine[x + 1][y - 1];
            }

            if (mine[x + 1][y] == 0) {
                mine1[x + 1][y] = mine[x + 1][y] = -1;
                scan(x + 1, y);
            } else {
                mine1[x + 1][y] = mine[x + 1][y];
            }

            if (mine[x + 1][y + 1] == 0) {
                mine1[x + 1][y + 1] = mine[x + 1][y + 1] = -1;
                scan(x + 1, y + 1);
            } else {
                mine1[x + 1][y + 1] = mine[x + 1][y + 1];
            }

            if (mine[x][y + 1] == 0) {
                mine1[x][y + 1] = mine[x][y + 1] = -1;
                scan(x, y + 1);
            } else {
                mine1[x][y + 1] = mine[x][y + 1];
            }
        }
        //右
        if (y - 1 >= 0 && y + 1 <= mine_vertical - 1 && x + 1 > mine_crosswise - 1) {
            if (mine[x][y - 1] == 0) {
                mine1[x][y - 1] = mine[x][y - 1] = -1;
                scan(x, y - 1);
            } else {
                mine1[x][y - 1] = mine[x][y - 1];
            }

            if (mine[x - 1][y - 1] == 0) {
                mine1[x - 1][y - 1] = mine[x - 1][y - 1] = -1;
                scan(x - 1, y - 1);
            } else {
                mine1[x - 1][y - 1] = mine[x - 1][y - 1];
            }

            if (mine[x - 1][y] == 0) {
                mine1[x - 1][y] = mine[x - 1][y] = -1;
                scan(x - 1, y);
            } else {
                mine1[x - 1][y] = mine[x - 1][y];
            }

            if (mine[x - 1][y + 1] == 0) {
                mine1[x - 1][y + 1] = mine[x - 1][y + 1] = -1;
                scan(x - 1, y + 1);
            } else {
                mine1[x - 1][y + 1] = mine[x - 1][y + 1];
            }

            if (mine[x][y + 1] == 0) {
                mine1[x][y + 1] = mine[x][y + 1] = -1;
                scan(x, y + 1);
            } else {
                mine1[x][y + 1] = mine[x][y + 1];
            }
        }
        //左上角
        if (x - 1 < 0 && y - 1 < 0) {

            if (mine[x + 1][y] == 0) {
                mine1[x + 1][y] = mine[x + 1][y] = -1;
                scan(x + 1, y);
            } else {
                mine1[x + 1][y] = mine[x + 1][y];
            }

            if (mine[x][y + 1] == 0) {
                mine1[x][y + 1] = mine[x][y + 1] = -1;
                scan(x, y + 1);
            } else {
                mine1[x][y + 1] = mine[x][y + 1];
            }

            if (mine[x + 1][y + 1] == 0) {
                mine1[x + 1][y + 1] = mine[x + 1][y + 1] = -1;
                scan(x + 1, y + 1);
            } else {
                mine1[x + 1][y + 1] = mine[x + 1][y + 1];
            }
        }
        //右上角
        if (x + 1 >= mine_crosswise && y - 1 < 0) {

            if (mine[x - 1][y] == 0) {
                mine1[x - 1][y] = mine[x - 1][y] = -1;
                scan(x - 1, y);
            } else {
                mine1[x - 1][y] = mine[x - 1][y];
            }

            if (mine[x][y + 1] == 0) {
                mine1[x][y + 1] = mine[x][y + 1] = -1;
                scan(x, y + 1);
            } else {
                mine1[x][y + 1] = mine[x][y + 1];
            }

            if (mine[x - 1][y + 1] == 0) {
                mine1[x - 1][y + 1] = mine[x - 1][y + 1] = -1;
                scan(x - 1, y + 1);
            } else {
                mine1[x - 1][y + 1] = mine[x - 1][y + 1];
            }
        }
        //左下角
        if (x - 1 < 0 && y + 1 >= mine_vertical) {

            if (mine[x + 1][y] == 0) {
                mine1[x + 1][y] = mine[x + 1][y] = -1;
                scan(x + 1, y);
            } else {
                mine1[x + 1][y] = mine[x + 1][y];
            }

            if (mine[x][y - 1] == 0) {
                mine1[x][y - 1] = mine[x][y - 1] = -1;
                scan(x, y - 1);
            } else {
                mine1[x][y - 1] = mine[x][y - 1];
            }

            if (mine[x + 1][y - 1] == 0) {
                mine1[x + 1][y - 1] = mine[x + 1][y - 1] = -1;
                scan(x + 1, y - 1);
            } else {
                mine1[x + 1][y - 1] = mine[x + 1][y - 1];
            }
        }
        //右下角
        if (x + 1 >= mine_crosswise && y + 1 >= mine_vertical) {

            if (mine[x - 1][y] == 0) {
                mine1[x - 1][y] = mine[x - 1][y] = -1;
                scan(x - 1, y);
            } else {
                mine1[x - 1][y] = mine[x - 1][y];
            }

            if (mine[x][y - 1] == 0) {
                mine1[x][y - 1] = mine[x][y - 1] = -1;
                scan(x, y - 1);
            } else {
                mine1[x][y - 1] = mine[x][y - 1];
            }

            if (mine[x - 1][y - 1] == 0) {
                mine1[x - 1][y - 1] = mine[x - 1][y - 1] = -1;
                scan(x - 1, y - 1);
            } else {
                mine1[x - 1][y - 1] = mine[x - 1][y - 1];
            }
        }

        //中间部分
        if (x - 1 >= 0 && y - 1 >= 0 && x + 1 < mine_crosswise && y + 1 < mine_vertical) {

            if (mine[x - 1][y - 1] == 0) {
                mine1[x - 1][y - 1] = mine[x - 1][y - 1] = -1;
                scan(x - 1, y - 1);
            } else {
                mine1[x - 1][y - 1] = mine[x - 1][y - 1];
            }

            if (mine[x - 1][y] == 0) {
                mine1[x - 1][y] = mine[x - 1][y] = -1;
                scan(x - 1, y);
            } else {
                mine1[x - 1][y] = mine[x - 1][y];
            }

            if (mine[x - 1][y + 1] == 0) {
                mine1[x][y - 1] = mine[x][y - 1] = -1;
                scan(x - 1, y + 1);
            } else {
                mine1[x][y - 1] = mine[x][y - 1];
            }

            if (mine[x][y - 1] == 0) {
                mine1[x][y - 1] = mine[x][y - 1] = -1;
                scan(x, y - 1);
            } else {
                mine1[x][y - 1] = mine[x][y - 1];
            }

            if (mine[x][y + 1] == 0) {
                mine1[x][y + 1] = mine[x][y + 1] = -1;
                scan(x, y + 1);
            } else {
                mine1[x][y + 1] = mine[x][y + 1];
            }

            if (mine[x + 1][y - 1] == 0) {
                mine1[x + 1][y - 1] = mine[x + 1][y - 1] = -1;
                scan(x + 1, y - 1);
            } else {
                mine1[x + 1][y - 1] = mine[x + 1][y - 1];
            }

            if (mine[x + 1][y] == 0) {
                mine1[x + 1][y] = mine[x + 1][y] = -1;
                scan(x + 1, y);
            } else {
                mine1[x + 1][y] = mine[x + 1][y];
            }

            if (mine[x + 1][y + 1] == 0) {
                mine1[x + 1][y + 1] = mine[x + 1][y + 1] = -1;
                scan(x + 1, y + 1);
            } else {
                mine1[x + 1][y + 1] = mine[x + 1][y + 1];
            }
        }

    }


    /**
     * 小黄脸——重新开始按钮
     * 判断游戏当前状态,改变小黄脸的表情
     */
    public static void again() {
        if (judge == 0) {
            try {
                image = ImageIO.read(new File("D:\\#Java\\个人项目\\MineSweeper\\img\\playing.png"));
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else if (judge == 1) {
            try {
                image = ImageIO.read(new File("D:\\#Java\\个人项目\\MineSweeper\\img\\win.png"));
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else if (judge == 2) {
            try {
                image = ImageIO.read(new File("D:\\#Java\\个人项目\\MineSweeper\\img\\lose.png"));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 放置地雷
     * 利用当前时间为随机数种子
     * 随机二位数组的位置,赋值为1,代表此处有雷
     * 如果随机到已经有雷的区域,则跳出循环,重新随机
     * 放置雷后将周围数值加1
     */
    public static void place() {
        if (mine_place == false) {
            Random r = new Random();
            int x, y;
            for (int i = 0; i < mine_mun; i++) {
                x = r.nextInt(mine_mun);
                y = r.nextInt(mine_mun);
                if (mine[x][y] == 9) {
                    i--;
                    continue;
                }
                mine1[x][y] = mine[x][y] = 9;

                //上
                if (x - 1 >= 0 && x + 1 <= mine_crosswise - 1 && y - 1 < 0) {
                    if (mine[x - 1][y] != 9) {
                        mine[x - 1][y]++;
                    }
                    if (mine[x - 1][y + 1] != 9) {
                        mine[x - 1][y + 1]++;
                    }
                    if (mine[x][y + 1] != 9) {
                        mine[x][y + 1]++;
                    }

                    if (mine[x + 1][y + 1] != 9) {
                        mine[x + 1][y + 1]++;
                    }
                    if (mine[x + 1][y] != 9) {
                        mine[x + 1][y]++;
                    }
                }
                //下
                if (x - 1 >= 0 && x + 1 <= mine_crosswise - 1 && y + 1 > mine_vertical - 1) {
                    if (mine[x - 1][y] != 9) {
                        mine[x - 1][y]++;
                    }
                    if (mine[x - 1][y - 1] != 9) {
                        mine[x - 1][y - 1]++;
                    }
                    if (mine[x][y - 1] != 9) {
                        mine[x][y - 1]++;
                    }
                    if (mine[x + 1][y - 1] != 9) {
                        mine[x + 1][y - 1]++;
                    }
                    if (mine[x + 1][y] != 9) {
                        mine[x + 1][y]++;
                    }

                }
                //左
                if (y - 1 >= 0 && y + 1 <= mine_crosswise - 1 && x - 1 < 0) {
                    if (mine[x][y - 1] != 9) {
                        mine[x][y - 1]++;
                    }
                    if (mine[x + 1][y - 1] != 9) {
                        mine[x + 1][y - 1]++;
                    }
                    if (mine[x + 1][y] != 9) {
                        mine[x + 1][y]++;
                    }
                    if (mine[x + 1][y + 1] != 9) {
                        mine[x + 1][y + 1]++;
                    }
                    if (mine[x][y + 1] != 9) {
                        mine[x][y + 1]++;
                    }
                }
                //右
                if (y - 1 >= 0 && y + 1 <= mine_vertical - 1 && x + 1 > mine_crosswise - 1) {
                    if (mine[x][y - 1] != 9) {
                        mine[x][y - 1]++;
                    }
                    if (mine[x - 1][y - 1] != 9) {
                        mine[x - 1][y - 1]++;
                    }
                    if (mine[x - 1][y] != 9) {
                        mine[x - 1][y]++;
                    }
                    if (mine[x - 1][y + 1] != 9) {
                        mine[x - 1][y + 1]++;
                    }
                    if (mine[x][y + 1] != 9) {
                        mine[x][y + 1]++;
                    }
                }
                //左上角
                if (x - 1 < 0 && y - 1 < 0) {
                    if (mine[x + 1][y] != 9) {
                        mine[x + 1][y]++;
                    }
                    if (mine[x][y + 1] != 9) {
                        mine[x][y + 1]++;
                    }
                    if (mine[x + 1][y + 1] != 9) {
                        mine[x + 1][y + 1]++;
                    }
                }
                //右上角
                if (x + 1 >= mine_crosswise && y - 1 < 0) {
                    if (mine[x - 1][y] != 9) {
                        mine[x - 1][y]++;
                    }
                    if (mine[x][y + 1] != 9) {
                        mine[x][y + 1]++;
                    }
                    if (mine[x - 1][y + 1] != 9) {
                        mine[x - 1][y + 1]++;
                    }

                }
                //左下角
                if (x - 1 < 0 && y + 1 >= mine_vertical) {
                    if (mine[x + 1][y] != 9) {
                        mine[x + 1][y]++;
                    }
                    if (mine[x][y - 1] != 9) {
                        mine[x][y - 1]++;
                    }
                    if (mine[x + 1][y - 1] != 9) {
                        mine[x + 1][y - 1]++;
                    }
                }
                //右下角
                if (x + 1 >= mine_crosswise && y + 1 >= mine_vertical) {
                    if (mine[x - 1][y] != 9) {
                        mine[x - 1][y]++;
                    }
                    if (mine[x][y - 1] != 9) {
                        mine[x][y - 1]++;
                    }
                    if (mine[x - 1][y - 1] != 9) {
                        mine[x - 1][y - 1]++;
                    }
                }
                //中间部分
                if (x - 1 >= 0 && y - 1 >= 0 && x + 1 < mine_crosswise && y + 1 < mine_vertical) {
                    if (mine[x - 1][y - 1] != 9) {
                        mine[x - 1][y - 1]++;
                    }
                    if (mine[x - 1][y] != 9) {
                        mine[x - 1][y]++;
                    }
                    if (mine[x - 1][y + 1] != 9) {
                        mine[x - 1][y + 1]++;
                    }
                    if (mine[x][y - 1] != 9) {
                        mine[x][y - 1]++;
                    }
                    if (mine[x][y + 1] != 9) {
                        mine[x][y + 1]++;
                    }
                    if (mine[x + 1][y - 1] != 9) {
                        mine[x + 1][y - 1]++;
                    }
                    if (mine[x + 1][y] != 9) {
                        mine[x + 1][y]++;
                    }
                    if (mine[x + 1][y + 1] != 9) {
                        mine[x + 1][y + 1]++;
                    }
                }

            }
            mine_place = true;
        }
    }


    //---------------------------------------------------------------------------------------------------------------------
    @Override//鼠标点击
    public void mouseClicked(MouseEvent e) {
        //获取鼠标点击位置
        int x = e.getX();
        int y = e.getY();


        //判断是否开始了游戏
        if (judge == 0) {
            //判断点击位置是否在雷区内
            if (x > 30 && x < 30 + mine_crosswise * 50 && y > 110 && y < 110 + mine_vertical * 50) {
                x = (x - 30) / 50;
                y = (y - 110) / 50;
                //判断鼠标点击方式BUTTON1:左键、BUTTON2:中键、BUTTON3:右键
                if (e.getButton() == MouseEvent.BUTTON1) {
                    //左键代表翻开
                    //判断点击位置是否合法
                    if (sign[x][y] == 0 && uncertainty[x][y] == 0) {
                        //判断点击区域有没有雷
                        if (mine[x][y] != 9) {
                            //调用递归
                            scan(x, y);
                            mine1[x][y] = mine[x][y];
                            this.repaint();

                        } else if (mine[x][y] == 9) {
                            judge = 2;
                            this.repaint();
                            JOptionPane.showMessageDialog(this, "你被雷炸了个粉碎!");
                        }

                        int mun = 0;//计算还有多少个方块没有翻开
                        for (int i = 0; i < mine_crosswise; i++) {
                            for (int j = 0; j < mine_vertical; j++) {
                                if (mine1[i][j] == 0) {
                                    ++mun;
                                }
                            }
                        }
                        //如果全翻开了,那就赢了
                        if (mun == 0) {
                            judge = 1;
                            this.repaint();
                            JOptionPane.showMessageDialog(this, "你完整的活了下来!");
                        }
                    }
                } else if (e.getButton() == MouseEvent.BUTTON2) {
                    //中键代表问号
                    if (uncertainty[x][y] == 0 && sign[x][y] == 0) {
                        uncertainty[x][y] = 1;
                    } else if (uncertainty[x][y] == 1) {
                        uncertainty[x][y] = 0;
                    }
                    this.repaint();
                } else if (e.getButton() == MouseEvent.BUTTON3) {
                    //右键代表插旗
                    if (sign[x][y] == 0 && sign_mun < mine_mun && uncertainty[x][y] == 0) {
                        sign[x][y] = 1;
                        sign_mun++;
                    } else if (sign[x][y] == 1) {
                        sign[x][y] = 0;
                        sign_mun--;
                    }
                    this.repaint();
                }
            }


            //重新开始按钮——小黄脸
            if (x >= (window_width - 50) / 2 && y >= 45 && x <= (window_width - 50) / 2 + 50 && y <= 45 + 50) {
                //重置所有数据
                mine_mun = 10;//初始化雷的数量
                //初始化二维数组
                for (int i = 0; i < mine_crosswise; i++) {
                    for (int j = 0; j < mine_vertical; j++) {
                        mine[i][j] = 0;
                        mine1[i][j] = 0;
                        sign[i][j] = 0;
                        uncertainty[i][j] = 0;
                    }
                }
                mine_place = false;//绘制雷的开关
                //生成新的数据
                again();//小黄脸的新状态
                place();//雷的新位置
                judge = 0;//恢复游戏状态
                this.repaint();
            }
        }
    }


    @Override//鼠标按下
    public void mousePressed(MouseEvent e) {

    }

    @Override//鼠标抬起
    public void mouseReleased(MouseEvent e) {

    }

    @Override//鼠标进入
    public void mouseEntered(MouseEvent e) {

    }

    @Override//鼠标离开
    public void mouseExited(MouseEvent e) {


    }

}

结束语

  实现的功能比较基础,有时间的话我想再升级一下,比如加个调难度啥的,我也整个关卡,哈哈。
  BUG不可避免,我自己玩了几遍,暂时还没发现什么严重的问题,如果有问题,欢迎到评论区指正。
  往期Java小游戏还有:
  五子棋全代码
  贪吃蛇全代码
  最后还是老话:坚持分享知识的原则,保证代码公开透明,如果帮到了你,请顺手点个赞吧(#^.^#)


点击全文阅读


本文链接:http://zhangshiyu.com/post/37439.html

项目  雷区  翻开  
<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

关于我们 | 我要投稿 | 免责申明

Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1