




已阅读5页,还剩10页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
基于JAVA的扫雷小游戏1、 引言本次课程设计目的在于设计开发一个类似windows自带扫雷游戏的小游戏,实现基本的扫雷面板及扫雷的游戏功能、游戏数据存储、游戏计时等功能。设计采用Windows下的eclipse开发工具由本人独立完成。2、 系统设计本游戏采用快速原型模型的软件开发方法设计,总共经历了八个版本的修改最终完成设计要求。在第一个版本中,实现如下功能:基于JFrame的扫雷框架的建立:使用JFrame建立起如图的所示的程序框架,雷区为12*12,添加JPanel和JButton,采用setBounds的布局方式而非内置的布局方法。基于Random方法的虚拟雷盘的建立和动态修改:通过Random产生出一个14*14的数组,其中,二维数组边缘对应边框标记值为2,产生的雷点标记为1,普通点标记为0。再次建立一个12*12的数组对应实际的游戏面板,初始值为0,遍历14*14的数组中非边缘的元素,将每个格子周围的地雷数目赋值给对应的12*12数组,地雷仍然用-1来表示,最后遍历12*12的数组同时把数组中非0非-1的数绘制到JPanel上,值为-1的元素向面板对应位置添加一个地雷的图片(注:地雷图片来自Windows7自带扫雷游戏的截图)。基于Button的雷区覆盖面板建立以及虚拟雷盘的ActionListener的连接:将生成好的底板覆盖上12*12的Button并且为每个Button添加ActionListener,实现点击后隐藏对应的Button功能。结果如下图: 重新开始及其按键功能的实现:通过“重新开始”按键重新生成雷区以及重新覆盖Button到所有格子。关于按键及其功能:通过“关于”按键弹出一个MessageDialog。在第二个版本中,实现如下功能:新增利用递归算法实现的一次点开一片区域功能:通过数据结构中的走迷宫算法在按键监听中加入了连锁点亮的算法,点亮该格,然后依次遍历12*12表的周围9格,发现为空格即递归调用遍历算法,发现数字即点亮该格并return,初步实现了如图所示的功能:新增虚拟访问判定表的建立和刷新及修改:即通过查找已标记的正确的雷并且计数,如果达到了设定了雷的最大值即执行游戏结束的方法。新增失败提示框和自动刷新功能:即点亮了地雷的区域后,自动弹出对话框提示失败并且执行游戏结束的方法。对原boom表进行了改动,解决了虚拟表和实际表的下标错位问题将原12*12的数组扩充到14*14。在第三个版本中,实现如下功能:修复了一个导致重新开始后第一行雷点位置不变的BUG:重写游戏结束的算法,改变循环的起始点,使其可以正确生成虚拟的雷点。新增了右键标记、取消雷点的功能:为每个Button添加了MouseListener从而实现了当点击鼠标右键时可以修改Button上文字,显示为雷,并且当该Button已经显示了雷的时候再次右键该Button可以取消文字显示。在第四个版本中,实现如下功能:调整了按键监听的点亮区域算法,当且仅当点击处周围没有地雷时才会触发openButton()算法,否则仅显示当前区域,提高了游戏性:重写了Button的ActionListener,按条件区分是否执行递归点亮算法,当且仅当单击区域为空的时候才执行点亮算法,否则仅点亮该区域。新增了基于System.currentTimeMillis()的计时器功能,计时器与重新开始游戏对应同步更新:通过在游戏开始时获取一个currentTimeMillis()以及实时监控并刷新计时器窗口的值为当前时间减去初始时间除以1000,为节约内存,单独为计时器开辟了一个线程,每工作一次该线程休息0.5秒。在第五个版本中,实现如下功能:更改了获胜和失败后的提示信息:将本次游戏时间加入了游戏结束时的提示窗口。新增了“记录”窗体的框架和面板:增加了一个新的JFrame,对应“记录”按钮。在第六个版本中,实现如下功能:再次改进了按键监听的点亮区域算法:进行递归遍历时将正相邻和斜相邻两种情况分开,使斜相邻的地雷值为0的格子不再会被自动点亮,提高了游戏性,至此版本为止,该算法已经完全符合预期要求。游戏后台新加入了recordlist类,用来存储和处理光荣榜的数据:该类拥有10条记录以及插入新数据到对应位置的功能。对记录窗体的改动:通过取消设定recordFrame类的mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);以及设定recFrame.hide();方法解决了关闭窗口时导致的程序异常终止的错误。在第七个版本中,实现如下功能:记录的读取与存储:通过ObjectOutputStream和ObjectInputStream成功实现了对光荣榜文件的存取功能。并且重新定义了上一版本的光荣榜信息控件,增加了获胜时修改光荣榜并且自动保存文件的功能,同时新增nameInput窗口类到游戏结束时并且成绩足以进入光荣榜时调用的方法中,用于输入获取进入光荣榜的玩家信息。在最终版本中,实现如下功能:记录与游戏的同步措施:通过更改FileOutputStream的实现位置到nameInputer中的actionListener中并且将recordlist和usedTime以参数形式通过构造函数传入nameInputer类中成功实现了光荣榜数据文件的存取。3、 系统实现Sweeper类:import java.awt.event.*;import javax.swing.*;import java.awt.*;import java.util.Random;import java.io.*;public class sweeperButton boom = new Button1414;int visualBoom = new int1414;int visitTest = new int1414;int numOfBoom = 0;Label timeLabel = new Label();timeRunnable runnable = new timeRunnable();Thread timeThread = new Thread(runnable);long startTime;long usedTime;JFrame mainframe;myPanel panel;Image boomImage = new ImageIcon(boom.jpg).getImage();recordlist list = new recordlist();JButton startButton;JButton aboutButton;JButton recordButton;/类的属性void createWindow()/创建基础框架mainframe = new JFrame(扫雷);panel = new myPanel();/框架及面板startButton = new JButton();startButton.setText(重新开始);startButton.setFont(new Font(楷书,Font.ITALIC,15);startButton.setFocusPainted(false);startButton.addActionListener(new startListener();aboutButton = new JButton();aboutButton.setText(关于);aboutButton.setFont(new Font(楷书,Font.ITALIC,15);aboutButton.setFocusPainted(false);aboutButton.addActionListener(new aboutListener();recordButton = new JButton();recordButton.setText(记录);recordButton.setFont(new Font(楷书,Font.ITALIC,15);recordButton.addActionListener(new recordListener();recordButton.setFocusPainted(false);/按钮timeLabel.setBounds(350, 220, 30, 30);timeLabel.setBackground(Color.white);startTime = System.currentTimeMillis();timeThread.start();panel.setLayout(null);panel.setBackground(Color.BLACK);startButton.setBounds(320, 40, 100, 30);panel.add(startButton);recordButton.setBounds(320,100,100,30);panel.add(recordButton);aboutButton.setBounds(320,160,100,30);panel.add(aboutButton);panel.add(timeLabel);mainframe.setSize(450, 340);mainframe.setVisible(true);mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);mainframe.add(panel);/框架布局void setBoom()/生成虚拟雷盘的雷区for(int row = 0;row14;row+)for(int col = 0;col14;col+)boomrowcol = new Button();visualBoomrowcol = 0;/初始化雷区for(int i = 0;i14;i+)visualBoom0i = -2;visualBoomi0 = -2;visualBoomi13 = -2;visualBoom13i = -2;/虚拟雷盘封边int x,y;Random r = new Random();for(int count = 0;count16;)x = r.nextInt(12);y = r.nextInt(12);if(visualBoomx+1y+1 = 0)visualBoomx+1y+1 = -1;count+;/生成地雷,边缘:-2 雷点:-1 正常点:0void handleBoom()/炸弹信息转化int temp = new int1414;for(int row = 0;row14;row+)for(int col = 0;col14;col+)temprowcol = visualBoomrowcol;for(int row = 1;row13;row+)for(int col = 1;col13;col+)temprowcol = countBoom(row,col);numOfBoom = 0;visualBoom = temp;int countBoom(int x,int y)/周围炸弹计数器int count = 0;if(visualBoomxy != -1)if(visualBoomx-1y-1 = -1)count+;if(visualBoomxy-1 = -1)count+;if(visualBoomx+1y-1 = -1)count+;if(visualBoomx+1y = -1)count+;if(visualBoomx+1y+1 = -1)count+;if(visualBoomxy+1 = -1) count+;if(visualBoomx-1y+1 = -1)count+; if(visualBoomx-1y = -1)count+;elsecount = -1;return count;/雷:-1 雷数:(int) void showButton()/加入雷区按钮到面板上for(int row = 1;row13;row+)for(int col = 1;col13;col+)boomrowcol.setBounds(row-1)*25, (col-1)*25, 25, 25);boomrowcol.setFocusable(false);boomrowcol.addActionListener(new buttomListener(row,col);boomrowcol.addMouseListener(new rightClick(row,col);panel.add(boomrowcol);class myPanel extends JPanel/面板内部类public void paintComponent(Graphics g)g.setColor(Color.gray);g.fillRect(0, 0, 300, 300);g.setColor(Color.black);for(int line = 0;line=300;line+=25)g.drawLine(line, 0, line, 300);for(int row = 0;row=300;row+=25)g.drawLine(0, row, 300, row);/绘制基本格g.setFont(new Font(楷书,Font.ITALIC,13);g.drawString(MineSweeper Ver 3.0, 305, 20);/绘制版本信息g.drawString(时间,310,240);for(int row = 1;row13;row+)for(int col = 1;col13;col+)if(visualBoomrowcol!=-1 & visualBoomrowcol!=0)g.drawString(Integer.toString(visualBoomrowcol), (row-1)*25+8, (col-1)*25+20);else if(visualBoomrowcol=-1) g.drawImage(boomImage,(row-1)*25,(col-1)*25,25,25,this);/面板绘图class buttomListener implements ActionListener/各种监听器int row,col;buttomListener(int x,int y)row = x;col = y;public void actionPerformed(ActionEvent e) if(visualBoomrowcol=0)refreshVisitTest();openButton(row,col);else if(visualBoomrowcol != -1)boomrowcol.setVisible(false);elseboomrowcol.setVisible(false);gameOver(0);numOfBoom = 0;for(int row = 1;row13;row+)for(int col = 1;col13;col+)if(boomrowcol.getLabel() = 雷)numOfBoom+;if(numOfBoom = 16)gameOver(1);class rightClick implements MouseListenerint row,col;rightClick(int x,int y)row = x;col = y;Overridepublic void mouseClicked(MouseEvent e) / TODO Auto-generated method stubif(e.getButton() = MouseEvent.BUTTON3)if(boomrowcol.getLabel() != 雷)boomrowcol.setLabel(雷);numOfBoom = 0;for(int row = 1;row13;row+)for(int col = 1;col13;col+)if(boomrowcol.getLabel() = 雷)numOfBoom+;if(numOfBoom = 16)gameOver(1);elseboomrowcol.setLabel();Overridepublic void mouseEntered(MouseEvent e) / TODO Auto-generated method stubOverridepublic void mouseExited(MouseEvent e) / TODO Auto-generated method stubOverridepublic void mousePressed(MouseEvent e) / TODO Auto-generated method stubOverridepublic void mouseReleased(MouseEvent e) / TODO Auto-generated method stubvoid refreshVisitTest()/重置访问标记表for(int row = 1;row13;row+)for(int col = 1;col13;col+)visitTestrowcol = 0;/访问标记置0for(int i = 0;i14;i+)visualBoom0i = 1;visualBoomi0 = 1;visualBoomi13 = 1;visualBoom13i = 1;/边缘访问标记置1class startListener implements ActionListenerpublic void actionPerformed(ActionEvent e) for(int row = 1;row13;row+)for(int col = 1;col13;col+)boomrowcol.setVisible(true);boomrowcol.setLabel();visualBoomrowcol = 0;int x,y;Random r = new Random();for(int count = 0;count16;)x = r.nextInt(12);y = r.nextInt(12);if(visualBoomx+1y+1 = 0)visualBoomx+1y+1 = -1;count+;handleBoom();startTime = System.currentTimeMillis();panel.repaint();System.out.println();System.out.println();System.out.println();System.out.println();for(int row = 1;row13;row+)System.out.println( );for(int col = 1;col13;col+)if(visualBoomcolrow!=-1)System.out.print(visualBoomcolrow+ );elseSystem.out.print(* );class recordListener implements ActionListenerOverridepublic void actionPerformed(ActionEvent arg0) recordFrame rec = new recordFrame();rec.createWindow();class aboutListener implements ActionListenerpublic void actionPerformed(ActionEvent e) JOptionPane.showMessageDialog(mainframe.getContentPane(),制作人: 滨江学院2011级软件工程1班 王琢, 关于, JOptionPane.INFORMATION_MESSAGE);void openButton(int x,int y)/响应鼠标事件visitTestxy = 1;/访问标记置1boomxy.setVisible(false);if(visualBoomxy != -1)if(visualBoomx-1y-1 != -1)boomx-1y-1.setVisible(false);if(visualBoomxy-1 = 0 & visitTestxy-1 = 0)openButton(x,y-1);else if(visualBoomxy-1 != -1)boomxy-1.setVisible(false);if(visualBoomx+1y-1 != -1)boomx+1y-1.setVisible(false);if(visualBoomx+1y = 0 & visitTestx+1y = 0)openButton(x+1,y);else if(visualBoomx+1y != -1)boomx+1y.setVisible(false);if(visualBoomx+1y+1 != -1)boomx+1y+1.setVisible(false);if(visualBoomxy+1 = 0 & visitTestxy+1 = 0)openButton(x,y+1);else if(visualBoomxy+1 != -1)boomxy+1.setVisible(false);if(visualBoomx-1y+1 != -1)boomx-1y+1.setVisible(false); if(visualBoomx-1y = 0 & visitTestx-1y = 0) openButton(x-1,y);else if(visualBoomx-1y != -1)boomx-1y.setVisible(false);elsegameOver(0);class timeRunnable implements Runnable/计时器专用线程Overridepublic void run() while(true)timeLabel.setText(Long.toString(System.currentTimeMillis()-startTime)/1000);usedTime = (System.currentTimeMillis()-startTime)/1000 + 1;tryThread.sleep(500);catch(Exception ex)void gameOver(int isWin)/游戏结束if(isWin = 0)JOptionPane.showMessageDialog(mainframe.getContentPane(), 胜败乃兵家常事,大侠请重新来过!n 本次游戏用时: + usedTime + 秒, YouLose!, JOptionPane.INFORMATION_MESSAGE);startButton.doClick();elseJOptionPane.showMessageDialog(mainframe.getContentPane(), 恭喜您!没有什么地雷能逃过您的火眼金睛n 本次游戏用时: + usedTime + 秒, YouWin!, JOptionPane.INFORMATION_MESSAGE);tryObjectInputStream in = new ObjectInputStream(new FileInputStream(record.wz);list = (recordlist)in.readObject();in.close();if(usedTime=list.getLowestScore();nameInputer nameinputer = new nameInputer(list,usedTime);catch(Exception e)startButton.doClick();public static void main(String args)sweeper main = new sweeper();main.setBoom();main.handleBoom();main.createWindow();main.showButton();for(int row = 1;row13;row+)System.out.println( );for(int col = 1;col13;col+)if(main.visualBoomcolrow!=-1)System.out.print(main.visualBoomcolrow+ );elseSystem.out.print(* );recordFrame类:import java.awt.Graphics;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.*;import javax.swing.*;public class recordFrame implements SerializableJFrame recFrame;recPanel recpanel;JButton close = new JButton(关闭);recordlist list;void createWindow()recFrame = new JFrame(光荣榜);recpanel = new recPanel();recpanel.setLayout(null);close.addActionListener(new closeListener();close.setBounds(50, 230, 80, 20);recpanel.add(close);recFrame.setSize(200, 300);recFrame.setVisible(true);recFrame.add(recpanel);class closeListener implements ActionListenerOverridepublic void actionPerformed(ActionEvent arg0) recFrame.hide();class recPanel extends JPanelpublic void paintComponent(Graphics g)g.drawString(姓名, 25, 20);g.drawString(耗时, 125, 20);tryObjectInputStream in = new ObjectInputStream(new FileInputStream(record.wz);list = (recordlist)in.readObject();in.close();for(int pos = 0;pos10;pos+)g.drawString(pos, 25, 20*(pos+2);g.drawString(Long.toString(list.scorepos), 125, 20*(pos+2);catch(Exception e)e.printStackTrace();recordList类:import java.io.*;public class recordlist implements Serializable/光荣榜存储类public String name;public long score;public recordlist()/构造函数name = new String10;score = new long10;for(int i = 0;iscorei)i+;dotemp = scorei;ntemp = namei;scorei = s;namei = n;s = temp;n = ntemp;i+;while(i10);nameInputer类:import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.FileOu
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 培训部主管的知识要求课件
- 培训赋能知识书单课件
- 培训课程的收获与价值
- 口算课件教学课件
- 2025咨询汇编:高校实习生就业保障与职业规划辅导服务合同
- 2025医疗机构合作协议中患者隐私保护与信息监测标准合同
- 2025年度科技创新创业服务平台运营指导及辅导合同
- 2025年城市综合体零售物业租赁及经营管理权交接专项合同
- 2025年绿色生态农业项目施工人员临时用工合同
- 2025年图书馆资料管理及校园后勤保障一体化服务合同
- (2025年标准)离职手协议书
- 2025年团场人员考试题库
- 生猪屠宰兽医卫生检验人员理论考试题库及答案
- 心脏起搏器植入指南
- 垂体功能减退症
- 大学生安全教育(高职版)实习实训与择业就业安全
- 2022新能源集控中心项目调试记录表
- 国家工作人员登记备案表
- 中考数学总复习经验交流课件
- 干部任免审批表(全国干部人事档案专项审核专用)
- 2023年生态环境综合行政执法考试参考题库(400题)
评论
0/150
提交评论