ImageVerifierCode 换一换
格式:DOCX , 页数:26 ,大小:132.46KB ,
资源ID:8749239      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/8749239.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(基于JAVA的扫雷游戏课程设计.docx)为本站会员(b****7)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

基于JAVA的扫雷游戏课程设计.docx

1、基于JAVA的扫雷游戏课程设计基于JAVA的扫雷游戏课程设计基于JAVA的扫雷小游戏1、引言本次课程设计目的在于设计开发一个类似windows自带扫雷游戏的小游戏,实现基本的扫雷面板及扫雷的游戏功能、游戏数据存储、游戏计时等功能。设计采用Windows下的eclipse开发工具由本人独立完成。2、系统设计本游戏采用快速原型模型的软件开发方法设计,总共经历了八个版本的修改最终完成设计要求。在第一个版本中,实现如下功能:基于JFrame的扫雷框架的建立:使用JFrame建立起如图的所示的程序框架,雷区为12*12,添加JPanel和JButton,采用setBounds的布局方式而非内置的布局方法

2、。基于Random方法的虚拟雷盘的建立和动态修改:通过Random产生出一个14*14的数组,其中,二维数组边缘对应边框标记值为2,产生的雷点标记为1,普通点标记为0。再次建立一个12*12的数组对应实际的游戏面板,初始值为0,遍历14*14的数组中非边缘的元素,将每个格子周围的地雷数目赋值给对应的12*12数组,地雷仍然用-1来表示,最后遍历12*12的数组同时把数组中非0非-1的数绘制到JPanel上,值为-1的元素向面板对应位置添加一个地雷的图片(注:地雷图片来自Windows7自带扫雷游戏的截图)。基于Button的雷区覆盖面板建立以及虚拟雷盘的ActionListener的连接:将生

3、成好的底板覆盖上12*12的Button并且为每个Button添加ActionListener,实现点击后隐藏对应的Button功能。结果如下图: 重新开始及其按键功能的实现:通过“重新开始”按键重新生成雷区以及重新覆盖Button到所有格子。关于按键及其功能:通过“关于”按键弹出一个MessageDialog。在第二个版本中,实现如下功能:新增利用递归算法实现的一次点开一片区域功能:通过数据结构中的走迷宫算法在按键监听中加入了连锁点亮的算法,点亮该格,然后依次遍历12*12表的周围9格,发现为空格即递归调用遍历算法,发现数字即点亮该格并return,初步实现了如图所示的功能:新增虚拟访问判定

4、表的建立和刷新及修改:即通过查找已标记的正确的雷并且计数,如果达到了设定了雷的最大值即执行游戏结束的方法。新增失败提示框和自动刷新功能:即点亮了地雷的区域后,自动弹出对话框提示失败并且执行游戏结束的方法。对原boom表进行了改动,解决了虚拟表和实际表的下标错位问题将原12*12的数组扩充到14*14。在第三个版本中,实现如下功能:修复了一个导致重新开始后第一行雷点位置不变的BUG:重写游戏结束的算法,改变循环的起始点,使其可以正确生成虚拟的雷点。新增了右键标记、取消雷点的功能:为每个Button添加了MouseListener从而实现了当点击鼠标右键时可以修改Button上文字,显示为雷,并且

5、当该Button已经显示了雷的时候再次右键该Button可以取消文字显示。在第四个版本中,实现如下功能:调整了按键监听的点亮区域算法,当且仅当点击处周围没有地雷时才会触发openButton()算法,否则仅显示当前区域,提高了游戏性:重写了Button的ActionListener,按条件区分是否执行递归点亮算法,当且仅当单击区域为空的时候才执行点亮算法,否则仅点亮该区域。新增了基于System.currentTimeMillis()的计时器功能,计时器与重新开始游戏对应同步更新:通过在游戏开始时获取一个currentTimeMillis()以及实时监控并刷新计时器窗口的值为当前时间减去初始时

6、间除以1000,为节约内存,单独为计时器开辟了一个线程,每工作一次该线程休息0.5秒。在第五个版本中,实现如下功能:更改了获胜和失败后的提示信息:将本次游戏时间加入了游戏结束时的提示窗口。新增了“记录”窗体的框架和面板:增加了一个新的JFrame,对应“记录”按钮。在第六个版本中,实现如下功能:再次改进了按键监听的点亮区域算法:进行递归遍历时将正相邻和斜相邻两种情况分开,使斜相邻的地雷值为0的格子不再会被自动点亮,提高了游戏性,至此版本为止,该算法已经完全符合预期要求。游戏后台新加入了recordlist类,用来存储和处理光荣榜的数据:该类拥有10条记录以及插入新数据到对应位置的功能。对记录窗

7、体的改动:通过取消设定recordFrame类的mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);以及设定recFrame.hide();方法解决了关闭窗口时导致的程序异常终止的错误。在第七个版本中,实现如下功能:记录的读取与存储:通过ObjectOutputStream和ObjectInputStream成功实现了对光荣榜文件的存取功能。并且重新定义了上一版本的光荣榜信息控件,增加了获胜时修改光荣榜并且自动保存文件的功能,同时新增nameInput窗口类到游戏结束时并且成绩足以进入光荣榜时调用的方法中,用于输入获取进入光荣榜的玩

8、家信息。在最终版本中,实现如下功能:记录与游戏的同步措施:通过更改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 =

9、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 Ima

10、geIcon(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

11、 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(

12、); 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.c

13、urrentTimeMillis(); 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(

14、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

15、 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 正常点:0 void handleBoom() /

16、炸弹信息转化 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(visua

17、lBoomxy != -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+; else count = -1; retu

18、rn 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

19、,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

20、, 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+

21、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) refreshVis

22、itTest(); openButton(row,col); else if(visualBoomrowcol != -1) boomrowcol.setVisible(false); else boomrowcol.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 rightCli

23、ck implements MouseListener int row,col; rightClick(int x,int y) row = x; col = y; Override public void mouseClicked(MouseEvent e) / TODO Auto-generated method stub if(e.getButton() = MouseEvent.BUTTON3) if(boomrowcol.getLabel() != 雷) boomrowcol.setLabel(雷); numOfBoom = 0; for(int row = 1;row13;row+

24、) for(int col = 1;col13;col+) if(boomrowcol.getLabel() = 雷) numOfBoom+; if(numOfBoom = 16) gameOver(1); else boomrowcol.setLabel(); Override public void mouseEntered(MouseEvent e) / TODO Auto-generated method stub Override public void mouseExited(MouseEvent e) / TODO Auto-generated method stub Overr

25、ide public void mousePressed(MouseEvent e) / TODO Auto-generated method stub Override public void mouseReleased(MouseEvent e) / TODO Auto-generated method stub void refreshVisitTest() /重置访问标记表 for(int row = 1;row13;row+) for(int col = 1;col13;col+) visitTestrowcol = 0; /访问标记置0 for(int i = 0;i14;i+)

26、visualBoom0i = 1; visualBoomi0 = 1; visualBoomi13 = 1; visualBoom13i = 1; /边缘访问标记置1 class startListener implements ActionListener public 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();

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1