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

上传人:b****7 文档编号:8749239 上传时间:2023-02-01 格式:DOCX 页数:26 大小:132.46KB
下载 相关 举报
基于JAVA的扫雷游戏课程设计.docx_第1页
第1页 / 共26页
基于JAVA的扫雷游戏课程设计.docx_第2页
第2页 / 共26页
基于JAVA的扫雷游戏课程设计.docx_第3页
第3页 / 共26页
基于JAVA的扫雷游戏课程设计.docx_第4页
第4页 / 共26页
基于JAVA的扫雷游戏课程设计.docx_第5页
第5页 / 共26页
点击查看更多>>
下载资源
资源描述

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

《基于JAVA的扫雷游戏课程设计.docx》由会员分享,可在线阅读,更多相关《基于JAVA的扫雷游戏课程设计.docx(26页珍藏版)》请在冰豆网上搜索。

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

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

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

基于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类:

importjava.awt.event.*;

importjavax.swing.*;

importjava.awt.*;

importjava.util.Random;

importjava.io.*;

 

publicclasssweeper{

Buttonboom[][]=newButton[14][14];

intvisualBoom[][]=newint[14][14];

intvisitTest[][]=newint[14][14];

intnumOfBoom=0;

LabeltimeLabel=newLabel();

timeRunnablerunnable=newtimeRunnable();

ThreadtimeThread=newThread(runnable);

longstartTime;

longusedTime;

JFramemainframe;

myPanelpanel;

ImageboomImage=newImageIcon("boom.jpg").getImage();

recordlistlist=newrecordlist();

JButtonstartButton;

JButtonaboutButton;

JButtonrecordButton;//类的属性

voidcreateWindow(){//创建基础框架

mainframe=newJFrame("扫雷");

panel=newmyPanel();//框架及面板

startButton=newJButton();

startButton.setText("重新开始");

startButton.setFont(newFont("楷书",Font.ITALIC,15));

startButton.setFocusPainted(false);

startButton.addActionListener(newstartListener());

aboutButton=newJButton();

aboutButton.setText("关于");

aboutButton.setFont(newFont("楷书",Font.ITALIC,15));

aboutButton.setFocusPainted(false);

aboutButton.addActionListener(newaboutListener());

recordButton=newJButton();

recordButton.setText("记录");

recordButton.setFont(newFont("楷书",Font.ITALIC,15));

recordButton.addActionListener(newrecordListener());

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);//框架布局

}

voidsetBoom()//生成虚拟雷盘的雷区

{

for(introw=0;row<14;row++)

for(intcol=0;col<14;col++)

{

boom[row][col]=newButton();

visualBoom[row][col]=0;

}//初始化雷区

for(inti=0;i<14;i++)

{

visualBoom[0][i]=-2;

visualBoom[i][0]=-2;

visualBoom[i][13]=-2;

visualBoom[13][i]=-2;

}//虚拟雷盘封边

intx,y;

Randomr=newRandom();

for(intcount=0;count<16;)

{

x=r.nextInt(12);

y=r.nextInt(12);

if(visualBoom[x+1][y+1]==0)

{

visualBoom[x+1][y+1]=-1;

count++;

}

}

}//生成地雷,边缘:

-2雷点:

-1正常点:

0

voidhandleBoom(){//炸弹信息转化

inttemp[][]=newint[14][14];

for(introw=0;row<14;row++)

for(intcol=0;col<14;col++)

{

temp[row][col]=visualBoom[row][col];

}

for(introw=1;row<13;row++)

for(intcol=1;col<13;col++)

{

temp[row][col]=countBoom(row,col);

}

numOfBoom=0;

visualBoom=temp;

}

intcountBoom(intx,inty){//周围炸弹计数器

intcount=0;

if(visualBoom[x][y]!

=-1)

{

if(visualBoom[x-1][y-1]==-1)

count++;

if(visualBoom[x][y-1]==-1)

count++;

if(visualBoom[x+1][y-1]==-1)

count++;

if(visualBoom[x+1][y]==-1)

count++;

if(visualBoom[x+1][y+1]==-1)

count++;

if(visualBoom[x][y+1]==-1)

count++;

if(visualBoom[x-1][y+1]==-1)

count++;

if(visualBoom[x-1][y]==-1)

count++;

}else

count=-1;

returncount;

}//雷:

-1雷数:

(int)

voidshowButton()//加入雷区按钮到面板上

{

for(introw=1;row<13;row++)

for(intcol=1;col<13;col++)

{

boom[row][col].setBounds((row-1)*25,(col-1)*25,25,25);

boom[row][col].setFocusable(false);

boom[row][col].addActionListener(newbuttomListener(row,col));

boom[row][col].addMouseListener(newrightClick(row,col));

panel.add(boom[row][col]);

}

}

classmyPanelextendsJPanel{//面板内部类

publicvoidpaintComponent(Graphicsg)

{

g.setColor(Color.gray);

g.fillRect(0,0,300,300);

g.setColor(Color.black);

for(intline=0;line<=300;line+=25)

g.drawLine(line,0,line,300);

for(introw=0;row<=300;row+=25)

g.drawLine(0,row,300,row);//绘制基本格

g.setFont(newFont("楷书",Font.ITALIC,13));

g.drawString("MineSweeperVer3.0",305,20);//绘制版本信息

g.drawString("时间",310,240);

for(introw=1;row<13;row++)

for(intcol=1;col<13;col++)

{

if(visualBoom[row][col]!

=-1&&visualBoom[row][col]!

=0)

g.drawString(Integer.toString(visualBoom[row][col]),(row-1)*25+8,(col-1)*25+20);

elseif(visualBoom[row][col]==-1)

{

g.drawImage(boomImage,(row-1)*25,(col-1)*25,25,25,this);

}

}

}

}//面板绘图

classbuttomListenerimplementsActionListener{//各种监听器

introw,col;

buttomListener(intx,inty)

{

row=x;

col=y;

}

publicvoidactionPerformed(ActionEvente){

if(visualBoom[row][col]==0)

{

refreshVisitTest();

openButton(row,col);

}elseif(visualBoom[row][col]!

=-1)

{

boom[row][col].setVisible(false);

}else

{

boom[row][col].setVisible(false);

gameOver(0);

}

numOfBoom=0;

for(introw=1;row<13;row++)

for(intcol=1;col<13;col++)

if(boom[row][col].getLabel()=="雷")

numOfBoom++;

if(numOfBoom==16)

gameOver

(1);

}

}

classrightClickimplementsMouseListener{

introw,col;

rightClick(intx,inty)

{

row=x;

col=y;

}

@Override

publicvoidmouseClicked(MouseEvente){

//TODOAuto-generatedmethodstub

if(e.getButton()==MouseEvent.BUTTON3)

{

if(boom[row][col].getLabel()!

="雷")

{

boom[row][col].setLabel("雷");

numOfBoom=0;

for(introw=1;row<13;row++)

for(intcol=1;col<13;col++)

if(boom[row][col].getLabel()=="雷")

numOfBoom++;

if(numOfBoom==16)

gameOver

(1);

}

else

boom[row][col].setLabel("");

}

}

@Override

publicvoidmouseEntered(MouseEvente){

//TODOAuto-generatedmethodstub

}

@Override

publicvoidmouseExited(MouseEvente){

//TODOAuto-generatedmethodstub

}

@Override

publicvoidmousePressed(MouseEvente){

//TODOAuto-generatedmethodstub

}

@Override

publicvoidmouseReleased(MouseEvente){

//TODOAuto-generatedmethodstub

}

}

voidrefreshVisitTest(){//重置访问标记表

for(introw=1;row<13;row++)

for(intcol=1;col<13;col++)

{

visitTest[row][col]=0;

}//访问标记置0

for(inti=0;i<14;i++)

{

visualBoom[0][i]=1;

visualBoom[i][0]=1;

visualBoom[i][13]=1;

visualBoom[13][i]=1;

}//边缘访问标记置1

}

classstartListenerimplementsActionListener{

publicvoidactionPerformed(ActionEvente){

for(introw=1;row<13;row++)

for(intcol=1;col<13;col++)

{

boom[row][col].setVisible(true);

boom[row][col].setLabel("");

visualBoom[row][col]=0;

}

intx,y;

Randomr=newRandom();

for(intcount=0;count<16;)

{

x=r.nextInt(12);

y=r.nextInt(12);

if(visualBoom[x+1][y+1]==0)

{

visualBoom[x+1][y+1]=-1;

count++;

}

}

handleBoom();

startTime=System.currentTimeMillis();

panel.repaint();

System.out.println("");

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 初中教育

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

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