Java Swing实现扫雷小游戏
程序员文章站
2023-12-20 11:00:16
swing设计扫雷心得,供大家参考,具体内容如下
最近学习swing学习之余做了一个小游戏:扫雷
1.前期设计
2.实现
其实完成这个游戏的核心就在于对数组的操...
swing设计扫雷心得,供大家参考,具体内容如下
最近学习swing学习之余做了一个小游戏:扫雷
1.前期设计
2.实现
其实完成这个游戏的核心就在于对数组的操纵,下面贴主要代码main.java:
package first; import java.awt.color; import java.awt.dimension; import java.awt.font; import java.awt.gridlayout; import java.awt.image; import java.awt.toolkit; import java.awt.datatransfer.clipboard; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.awt.event.mouseevent; import java.awt.event.mouselistener; import java.io.bufferedreader; import java.io.bufferedwriter; import java.io.ioexception; import java.io.inputstreamreader; import java.io.outputstreamwriter; import java.net.serversocket; import java.net.socket; import java.sql.drivermanager; import java.sql.sqlexception; import java.util.hashset; import java.util.set; import java.util.uuid; import javax.swing.imageicon; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jmenu; import javax.swing.jmenubar; import javax.swing.jmenuitem; import javax.swing.joptionpane; import javax.swing.timer; import com.mysql.jdbc.connection; import com.mysql.jdbc.statement; public class main extends jframe implements actionlistener, mouselistener { /** * 义建 */ private static final long serialversionuid = 1l; // 前期参数声明 jmenuitem jminew, jmisave, jmiopen, jmiexit, jmichuji, jmizhongji, jmigaoji, jmishowinfo, jmiziding; toolkit toolkit = toolkit.getdefaulttoolkit(); // 获取默认工具包。 clipboard clipboard = toolkit.getsystemclipboard();// 获取系统 calibrate // 的一个实例,作为本机平台提供的剪贴板工具的接口。 //两个图标 imageicon icon = new imageicon("g:\\eclipse-workspace\\classtest_thundergame\\mine.png"); imageicon icon1 = new imageicon("g:\\eclipse-workspace\\classtest_thundergame\\flag.png"); private static int num = 1;// 这个num是雷数,可以编写一个程序来改变 // private static final int snum = 9;// 这个snum是扫雷的格数,可以编写一个程序来改变 private jbutton[][] jb; private int[][] map; boolean[][] flags; boolean[][] flag; int couttime; // 声明connection对象 connection con; // 驱动程序名 string driver = "com.mysql.jdbc.driver"; // url:指向要访问的数据库名 string url = "jdbc:mysql://localhost:3306/testsql3"; // mysql配置的用户名 string user = "root"; // 密码 string password = "huang"; public main(int snum, int mines) {// 主要界面构造函数 settitle("扫雷"); // 初始雷数量 num = mines; jmenubar greenbar = new jmenubar();// 菜单容器 greenbar.setopaque(true); greenbar.setbackground(new color(250, 250, 250)); greenbar.setpreferredsize(new dimension(800, 28)); greenbar.setfont(new font(font.dialog_input, font.italic, 16)); // 菜单 jmenu filemenu1 = new jmenu("游戏"); jmenu filemenu2 = new jmenu("难度"); jmenu filemenu3 = new jmenu("帮助:"); greenbar.add(filemenu1); greenbar.add(filemenu2); greenbar.add(jmishowinfo = filemenu3); filemenu1.setfont(new font(font.dialog_input, font.italic, 16)); filemenu2.setfont(new font(font.dialog_input, font.italic, 16)); filemenu3.setfont(new font(font.dialog_input, font.italic, 16)); // 菜单项 filemenu1.add(jminew = new jmenuitem(" 新游戏 ")); filemenu1.add(jmisave = new jmenuitem(" 排行版 ")); filemenu1.add(jmiziding = new jmenuitem(" 自定义 ")); filemenu1.addseparator(); filemenu1.add(jmiexit = new jmenuitem(" 退出 ")); filemenu2.add(jmichuji = new jmenuitem(" 初级 ")); filemenu2.add(jmizhongji = new jmenuitem(" 中级 ")); filemenu2.add(jmigaoji = new jmenuitem(" 高级 ")); filemenu3.add(jmishowinfo = new jmenuitem(" 开发者信息 ")); jminew.addactionlistener(this); jmiexit.addactionlistener(this); jmisave.addactionlistener(this); jmishowinfo.addactionlistener(this); jmichuji.addactionlistener(this); jmizhongji.addactionlistener(this); jmigaoji.addactionlistener(this); jmiziding.addactionlistener(this); jmiziding.setfont(new font(font.dialog_input, font.italic, 16)); jmishowinfo.setfont(new font(font.dialog_input, font.italic, 16)); jminew.setfont(new font(font.dialog_input, font.italic, 16)); jmisave.setfont(new font(font.dialog_input, font.italic, 16)); jmiexit.setfont(new font(font.dialog_input, font.italic, 16)); jmichuji.setfont(new font(font.dialog_input, font.italic, 16)); jmizhongji.setfont(new font(font.dialog_input, font.italic, 16)); jmigaoji.setfont(new font(font.dialog_input, font.italic, 16)); setjmenubar(greenbar); image icon = toolkit.getdefaulttoolkit().getimage("g:\\eclipse-workspace\\classtest_thundergame\\mine.png"); seticonimage(icon); setlayout(new gridlayout(snum, snum)); jb = new jbutton[snum][snum]; map = new int[snum][snum]; // 将按钮映射到数组中 flags = new boolean[map.length][map[0].length];// 保存点开记录表 flag = new boolean[map.length][map[0].length];// 保存点开记录表 int count = 0; // 布雷 while (count < num) { int i = (int) (math.random() * map.length);// hang int j = (int) (math.random() * map[0].length);// lie if (map[i][j] != '*') { map[i][j] = '*'; count++; } } for (int i = 0; i < snum; i++) { for (int j = 0; j < snum; j++) { jb[i][j] = new jbutton(); jb[i][j].setname(i + "_" + j); jb[i][j].setbackground(new color(220, 220, 220)); jb[i][j].setfont(new font(font.dialog_input, font.italic, 10)); jb[i][j].addactionlistener(this); jb[i][j].addmouselistener(this);// 加入mouse监听 add(jb[i][j]); } } // 计时器 jlabel ststus = new jlabel(); jlabel times = new jlabel(); jlabel miao = new jlabel(); add(ststus); add(times); times.settext(" 0 "); miao.settext(" 秒"); settimer(times); couttime = 0; ststus.settext(" 时间:"); greenbar.add(ststus); greenbar.add(times, right_alignment); greenbar.add(miao, right_alignment); times.setfont(new font(font.dialog_input, font.italic, 16)); ststus.setfont(new font(font.dialog_input, font.italic, 16)); miao.setfont(new font(font.dialog_input, font.italic, 16)); setsize(700, 700); setlocationrelativeto(null); setvisible(true); // setdefaultcloseoperation(jframe.exit_on_close); setdefaultcloseoperation(dispose_on_close); // 加入这一行 } private void settimer(jlabel time) {// 时间监听 final jlabel vartime = time; timer timeaction = new timer(1000, new actionlistener() { public void actionperformed(actionevent e) { couttime++; vartime.settext("" + couttime); } }); timeaction.start(); } private void showtheclick(int x, int y) {// 点击事件实现 if (map[x][y] == '*') { jb[x][y].seticon(icon); showmines(); } else { int count1 = 0; for (int a = x - 1; a <= x + 1; a++) { for (int b = y - 1; b <= y + 1; b++) { if (!(a < 0 || b < 0 || b >= map[0].length || a >= map.length) && map[a][b] == '*') count1++; } } flags[x][y] = true; if (count1 == 0) { jb[x][y].setbackground(color.white); } else { jb[x][y].settext(count1 + ""); jb[x][y].setfont(new font(font.dialog_input, font.italic, 20)); jb[x][y].setbackground(color.white); } if (count1 == 0) { for (int i = x - 1; i <= x + 1; i++) { for (int j = y - 1; j <= y + 1; j++) { if (!(i < 0 || j < 0 || i >= map.length || j >= map[0].length)) { if (!(i == x && j == y) && flags[i][j] == false) { showtheclick(i, j);//循环遍历 } else { // 防止重复访问 } } } } } } } private void showmines() {// 显示所有雷 // todo auto-generated method stub for (int i = 0; i < map.length; i++) {// 显雷 for (int j = 0; j < map.length; j++) { if (map[i][j] == '*') { jb[i][j].seticon(icon); // } } } // 结束游戏 int b = joptionpane.showoptiondialog(null, "哎呀,炸了炸了,新游戏?", "确认框", joptionpane.yes_no_option, joptionpane.question_message, null, null, null); if (b == 1) { system.exit(0); } else { setvisible(false); new main(map.length,num); } } @override public void actionperformed(actionevent e) {// 事件监听处理 // todo auto-generated method stub if (e.getsource() == jminew) { setvisible(false); new main(map.length,num); } else if (e.getsource() == jmisave) { showrange(); } else if (e.getsource() == jmiexit) { system.exit(0); } else if (e.getsource() == jmiziding) { new selfmines(); } else if (e.getsource() == jmichuji) { setvisible(false); new main(5,3); } else if (e.getsource() == jmishowinfo) { new myinfo(); } else if (e.getsource() == jmizhongji) { setvisible(false); new main(10,10); } else if (e.getsource() == jmigaoji) { setvisible(false); new main(20,60); } else { object obj = e.getsource(); int x, y; string[] strm = ((jbutton) obj).getname().split("_"); x = integer.parseint(strm[0]); y = integer.parseint(strm[1]); showtheclick(x, y); checksuccess();// 检查是否游戏结束 } } private void showrange() {// 显示排行榜 new shiyan13(map.length); } private void checksuccess() {//判断游戏是否结束 // todo auto-generated method stub int count = map.length * map[0].length; for (int i = 0; i < map.length; i++) { for (int j = 0; j < map[0].length; j++) { if (flags[i][j] == true) count--; } } if (count == num) { string uuid = uuid.randomuuid().tostring().replaceall("-", "");//表唯一标示uuid // 链接数据库,存储时间数据 try { class.forname(driver); con = (connection) drivermanager.getconnection(url, user, password); string sql; if (!con.isclosed()) { // ta.settext(""); system.out.println("连接数据库成功"); // 创建对象 statement statement = (statement) con.createstatement(); // if (map.length == 10) { // //要执行的sql语句 sql = "insert into middlerange(userid,usertime) values(\"" + uuid + "\"," + couttime + ");"; statement.executeupdate(sql); con.close(); } else if (map.length == 5) { sql = "insert into rang(userid,usertime) values(\"" + uuid + "\"," + couttime + ");"; statement.executeupdate(sql); con.close(); } else if (map.length == 20) { sql = "insert into toprange(userid,usertime) values(\"" + uuid + "\"," + couttime + ");"; statement.executeupdate(sql); con.close(); }else{ } } } catch (classnotfoundexception e) { // 数据库驱动类异常处理 system.out.println("error"); e.printstacktrace(); } catch (sqlexception e) { // system.out.println(e); system.err.println("找不到数据"); // int i=joptionpane.showconfirmdialog(null, "你输入的sql语句有误", // "找不到",joptionpane.yes_no_option); } catch (exception e) { e.printstacktrace(); } finally { system.out.println("数据库获取数据成功!"); } int i = joptionpane.showoptiondialog(null, "恭喜你过关了,是否继续?", "确认框", joptionpane.yes_no_option, joptionpane.question_message, null, null, null); // ststus.settext("hello"+i); if (i == 1) { system.exit(0); } else { setvisible(false); new main(map.length,num); } } } @override public void mouseclicked(mouseevent e) { // todo auto-generated method stub int c = e.getbutton(); if (c == mouseevent.button3) { object obj1 = e.getsource(); int x, y; string[] strm = ((jbutton) obj1).getname().split("_"); x = integer.parseint(strm[0]); y = integer.parseint(strm[1]); if (flag[x][y] == false && flags[x][y] == false) {//插旗子 jb[x][y].seticon(icon1); flag[x][y] = true; } else { jb[x][y].seticon(null); flag[x][y] = false; } } } @override public void mousepressed(mouseevent e) { // todo auto-generated method stub } @override public void mousereleased(mouseevent e) { // todo auto-generated method stub } @override public void mouseentered(mouseevent e) { // todo auto-generated method stub } @override public void mouseexited(mouseevent e) { // todo auto-generated method stub } }
3.实现效果
4.主要功能实现
(1)基础扫雷功能(随机布雷,插旗)
(2)可以选择难度
(3)可以自定义扫雷的雷的数量以及格子数
(4)设置时间
(5)添加排行榜功能(根据时间存入数据库排序)
(6)外打包成exe文件(带jre)可以多平台运行. (使用exe4j打包jar包)
需要源码的可以留个邮箱!这是源码:扫雷游戏
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。