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*10static 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小游戏还有:
五子棋全代码
贪吃蛇全代码
我一直坚持“分享知识”的原则,免费透明的将代码公开展示,如果帮到了你,请顺手点个赞吧(#.#)。