![基于JAVA的掃雷游戲課程設(shè)計_第1頁](http://file3.renrendoc.com/fileroot_temp3/2022-2/7/2cb05368-8002-4242-ba1e-0b305ec390dc/2cb05368-8002-4242-ba1e-0b305ec390dc1.gif)
![基于JAVA的掃雷游戲課程設(shè)計_第2頁](http://file3.renrendoc.com/fileroot_temp3/2022-2/7/2cb05368-8002-4242-ba1e-0b305ec390dc/2cb05368-8002-4242-ba1e-0b305ec390dc2.gif)
![基于JAVA的掃雷游戲課程設(shè)計_第3頁](http://file3.renrendoc.com/fileroot_temp3/2022-2/7/2cb05368-8002-4242-ba1e-0b305ec390dc/2cb05368-8002-4242-ba1e-0b305ec390dc3.gif)
![基于JAVA的掃雷游戲課程設(shè)計_第4頁](http://file3.renrendoc.com/fileroot_temp3/2022-2/7/2cb05368-8002-4242-ba1e-0b305ec390dc/2cb05368-8002-4242-ba1e-0b305ec390dc4.gif)
![基于JAVA的掃雷游戲課程設(shè)計_第5頁](http://file3.renrendoc.com/fileroot_temp3/2022-2/7/2cb05368-8002-4242-ba1e-0b305ec390dc/2cb05368-8002-4242-ba1e-0b305ec390dc5.gif)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、基于JAVA的掃雷小游戲1、 引言本次課程設(shè)計目的在于設(shè)計開發(fā)一個類似windows自帶掃雷游戲的小游戲,實現(xiàn)基本的掃雷面板及掃雷的游戲功能、游戲數(shù)據(jù)存儲、游戲計時等功能。設(shè)計采用Windows下的eclipse開發(fā)工具由本人獨立完成。2、 系統(tǒng)設(shè)計本游戲采用快速原型模型的軟件開發(fā)方法設(shè)計,總共經(jīng)歷了八個版本的修改最終完成設(shè)計要求。在第一個版本中,實現(xiàn)如下功能:基于JFrame的掃雷框架的建立:使用JFrame建立起如圖的所示的程序框架,雷區(qū)為12*12,添加JPanel和JButton,采用setBounds的布局方式而非內(nèi)置的布局方法?;赗andom方法的虛擬雷盤的建立和動態(tài)修改:通過R
2、andom產(chǎn)生出一個14*14的數(shù)組,其中,二維數(shù)組邊緣對應(yīng)邊框標(biāo)記值為2,產(chǎn)生的雷點標(biāo)記為1,普通點標(biāo)記為0。再次建立一個12*12的數(shù)組對應(yīng)實際的游戲面板,初始值為0,遍歷14*14的數(shù)組中非邊緣的元素,將每個格子周圍的地雷數(shù)目賦值給對應(yīng)的12*12數(shù)組,地雷仍然用-1來表示,最后遍歷12*12的數(shù)組同時把數(shù)組中非0非-1的數(shù)繪制到JPanel上,值為-1的元素向面板對應(yīng)位置添加一個地雷的圖片(注:地雷圖片來自Windows7自帶掃雷游戲的截圖)?;贐utton的雷區(qū)覆蓋面板建立以及虛擬雷盤的ActionListener的連接:將生成好的底板覆蓋上12*12的Button并且為每個But
3、ton添加ActionListener,實現(xiàn)點擊后隱藏對應(yīng)的Button功能。結(jié)果如下圖: 重新開始及其按鍵功能的實現(xiàn):通過“重新開始”按鍵重新生成雷區(qū)以及重新覆蓋Button到所有格子。關(guān)于按鍵及其功能:通過“關(guān)于”按鍵彈出一個MessageDialog。在第二個版本中,實現(xiàn)如下功能:新增利用遞歸算法實現(xiàn)的一次點開一片區(qū)域功能:通過數(shù)據(jù)結(jié)構(gòu)中的走迷宮算法在按鍵監(jiān)聽中加入了連鎖點亮的算法,點亮該格,然后依次遍歷12*12表的周圍9格,發(fā)現(xiàn)為空格即遞歸調(diào)用遍歷算法,發(fā)現(xiàn)數(shù)字即點亮該格并return,初步實現(xiàn)了如圖所示的功能:新增虛擬訪問判定表的建立和刷新及修改:即通過查找已標(biāo)記的正確的雷并且計數(shù)
4、,如果達(dá)到了設(shè)定了雷的最大值即執(zhí)行游戲結(jié)束的方法。新增失敗提示框和自動刷新功能:即點亮了地雷的區(qū)域后,自動彈出對話框提示失敗并且執(zhí)行游戲結(jié)束的方法。對原boom表進(jìn)行了改動,解決了虛擬表和實際表的下標(biāo)錯位問題將原12*12的數(shù)組擴(kuò)充到14*14。在第三個版本中,實現(xiàn)如下功能:修復(fù)了一個導(dǎo)致重新開始后第一行雷點位置不變的BUG:重寫游戲結(jié)束的算法,改變循環(huán)的起始點,使其可以正確生成虛擬的雷點。新增了右鍵標(biāo)記、取消雷點的功能:為每個Button添加了MouseListener從而實現(xiàn)了當(dāng)點擊鼠標(biāo)右鍵時可以修改Button上文字,顯示為雷,并且當(dāng)該Button已經(jīng)顯示了雷的時候再次右鍵該Button
5、可以取消文字顯示。在第四個版本中,實現(xiàn)如下功能:調(diào)整了按鍵監(jiān)聽的點亮區(qū)域算法,當(dāng)且僅當(dāng)點擊處周圍沒有地雷時才會觸發(fā)openButton()算法,否則僅顯示當(dāng)前區(qū)域,提高了游戲性:重寫了Button的ActionListener,按條件區(qū)分是否執(zhí)行遞歸點亮算法,當(dāng)且僅當(dāng)單擊區(qū)域為空的時候才執(zhí)行點亮算法,否則僅點亮該區(qū)域。新增了基于System.currentTimeMillis()的計時器功能,計時器與重新開始游戲?qū)?yīng)同步更新:通過在游戲開始時獲取一個currentTimeMillis()以及實時監(jiān)控并刷新計時器窗口的值為當(dāng)前時間減去初始時間除以1000,為節(jié)約內(nèi)存,單獨為計時器開辟了一個線程,
6、每工作一次該線程休息0.5秒。在第五個版本中,實現(xiàn)如下功能:更改了獲勝和失敗后的提示信息:將本次游戲時間加入了游戲結(jié)束時的提示窗口。新增了“記錄”窗體的框架和面板:增加了一個新的JFrame,對應(yīng)“記錄”按鈕。在第六個版本中,實現(xiàn)如下功能:再次改進(jìn)了按鍵監(jiān)聽的點亮區(qū)域算法:進(jìn)行遞歸遍歷時將正相鄰和斜相鄰兩種情況分開,使斜相鄰的地雷值為0的格子不再會被自動點亮,提高了游戲性,至此版本為止,該算法已經(jīng)完全符合預(yù)期要求。游戲后臺新加入了recordlist類,用來存儲和處理光榮榜的數(shù)據(jù):該類擁有10條記錄以及插入新數(shù)據(jù)到對應(yīng)位置的功能。對記錄窗體的改動:通過取消設(shè)定recordFrame類的main
7、frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);以及設(shè)定recFrame.hide();方法解決了關(guān)閉窗口時導(dǎo)致的程序異常終止的錯誤。在第七個版本中,實現(xiàn)如下功能:記錄的讀取與存儲:通過ObjectOutputStream和ObjectInputStream成功實現(xiàn)了對光榮榜文件的存取功能。并且重新定義了上一版本的光榮榜信息控件,增加了獲勝時修改光榮榜并且自動保存文件的功能,同時新增nameInput窗口類到游戲結(jié)束時并且成績足以進(jìn)入光榮榜時調(diào)用的方法中,用于輸入獲取進(jìn)入光榮榜的玩家信息。在最終版本中,實現(xiàn)如下功能:記錄與游戲的同步措施
8、:通過更改FileOutputStream的實現(xiàn)位置到nameInputer中的actionListener中并且將recordlist和usedTime以參數(shù)形式通過構(gòu)造函數(shù)傳入nameInputer類中成功實現(xiàn)了光榮榜數(shù)據(jù)文件的存取。3、 系統(tǒng)實現(xiàn)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 visualBo
9、om = 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(
10、);recordlist list = new recordlist();JButton startButton;JButton aboutButton;JButton recordButton;/類的屬性void createWindow()/創(chuàng)建基礎(chǔ)框架mainframe = new JFrame("掃雷");panel = new myPanel();/框架及面板startButton = new JButton();startButton.setText("重新開始");startButton.setFont(new Font("楷書&
11、quot;,Font.ITALIC,15);startButton.setFocusPainted(false);startButton.addActionListener(new startListener();aboutButton = new JButton();aboutButton.setText("關(guān)于");aboutButton.setFont(new Font("楷書",Font.ITALIC,15);aboutButton.setFocusPainted(false);aboutButton.addActionListener(new
12、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(Colo
13、r.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(abou
14、tButton);panel.add(timeLabel);mainframe.setSize(450, 340);mainframe.setVisible(true);mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);mainframe.add(panel);/框架布局void setBoom()/生成虛擬雷盤的雷區(qū)for(int row = 0;row<14;row+)for(int col = 0;col<14;col+)boomrowcol = new Button();visualBoomrowcol = 0
15、;/初始化雷區(qū)for(int i = 0;i<14;i+)visualBoom0i = -2;visualBoomi0 = -2;visualBoomi13 = -2;visualBoom13i = -2;/虛擬雷盤封邊int x,y;Random r = new Random();for(int count = 0;count<16;)x = r.nextInt(12);y = r.nextInt(12);if(visualBoomx+1y+1 = 0)visualBoomx+1y+1 = -1;count+;/生成地雷,邊緣:-2 雷點:-1 正常點:0void handleB
16、oom()/炸彈信息轉(zhuǎn)化int temp = new int1414;for(int row = 0;row<14;row+)for(int col = 0;col<14;col+)temprowcol = visualBoomrowcol;for(int row = 1;row<13;row+)for(int col = 1;col<13;col+)temprowcol = countBoom(row,col);numOfBoom = 0;visualBoom = temp;int countBoom(int x,int y)/周圍炸彈計數(shù)器int count = 0
17、;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
18、;/雷:-1 雷數(shù):(int) void showButton()/加入雷區(qū)按鈕到面板上for(int row = 1;row<13;row+)for(int col = 1;col<13;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);pa
19、nel.add(boomrowcol);class myPanel extends JPanel/面板內(nèi)部類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)
20、;/繪制基本格g.setFont(new Font("楷書",Font.ITALIC,13);g.drawString("MineSweeper Ver 3.0", 305, 20);/繪制版本信息g.drawString("時間",310,240);for(int row = 1;row<13;row+)for(int col = 1;col<13;col+)if(visualBoomrowcol!=-1 && visualBoomrowcol!=0)g.drawString(Integer.toStri
21、ng(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/各種監(jiān)聽器int row,col;buttomListener(int x,int y)row = x;col = y;public void actionPerformed(ActionEvent e) if(visual
22、Boomrowcol=0)refreshVisitTest();openButton(row,col);else if(visualBoomrowcol != -1)boomrowcol.setVisible(false);elseboomrowcol.setVisible(false);gameOver(0);numOfBoom = 0;for(int row = 1;row<13;row+)for(int col = 1;col<13;col+)if(boomrowcol.getLabel() = "雷")numOfBoom+;if(numOfBoom =
23、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("雷"
24、);numOfBoom = 0;for(int row = 1;row<13;row+)for(int col = 1;col<13;col+)if(boomrowcol.getLabel() = "雷")numOfBoom+;if(numOfBoom = 16)gameOver(1);elseboomrowcol.setLabel("");Overridepublic void mouseEntered(MouseEvent e) / TODO Auto-generated method stubOverridepublic void mo
25、useExited(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()/重置訪問標(biāo)記表for(int row = 1;row<13;row+)for(int col = 1;col&
26、lt;13;col+)visitTestrowcol = 0;/訪問標(biāo)記置0for(int i = 0;i<14;i+)visualBoom0i = 1;visualBoomi0 = 1;visualBoomi13 = 1;visualBoom13i = 1;/邊緣訪問標(biāo)記置1class startListener implements ActionListenerpublic void actionPerformed(ActionEvent e) for(int row = 1;row<13;row+)for(int col = 1;col<13;col+)boomrowc
27、ol.setVisible(true);boomrowcol.setLabel("");visualBoomrowcol = 0;int x,y;Random r = new Random();for(int count = 0;count<16;)x = r.nextInt(12);y = r.nextInt(12);if(visualBoomx+1y+1 = 0)visualBoomx+1y+1 = -1;count+;handleBoom();startTime = System.currentTimeMillis();panel.repaint();Syste
28、m.out.println("");System.out.println("");System.out.println("");System.out.println("");for(int row = 1;row<13;row+)System.out.println(" ");for(int col = 1;col<13;col+)if(visualBoomcolrow!=-1)System.out.print(visualBoomcolrow+" ");else
29、System.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.showMessageD
30、ialog(mainframe.getContentPane(),"制作人: 濱江學(xué)院2011級軟件工程1班 王琢", "關(guān)于", JOptionPane.INFORMATION_MESSAGE);void openButton(int x,int y)/響應(yīng)鼠標(biāo)事件visitTestxy = 1;/訪問標(biāo)記置1boomxy.setVisible(false);if(visualBoomxy != -1)if(visualBoomx-1y-1 != -1)boomx-1y-1.setVisible(false);if(visualBoomxy-1 = 0
31、 && 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+1
32、y+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(vi
33、sualBoomx-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)
34、;catch(Exception ex)void gameOver(int isWin)/游戲結(jié)束if(isWin = 0)JOptionPane.showMessageDialog(mainframe.getContentPane()," 勝敗乃兵家常事,大俠請重新來過!n 本次游戲用時:" + usedTime + "秒", "YouLose!", JOptionPane.INFORMATION_MESSAGE);startButton.doClick();elseJOptionPane.showMessageDialog(mai
35、nframe.getContentPane()," 恭喜您!沒有什么地雷能逃過您的火眼金睛n 本次游戲用時:" + usedTime + "秒", "YouWin!", JOptionPane.INFORMATION_MESSAGE);tryObjectInputStream in = new ObjectInputStream(new FileInputStream("record.wz");list = (recordlist)in.readObject();in.close();if(usedTime<
36、=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;row<13;row+)System.out.pr
37、intln(" ");for(int col = 1;col<13;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.*;im
38、port javax.swing.*;public class recordFrame implements SerializableJFrame recFrame;recPanel recpanel;JButton close = new JButton("關(guān)閉");recordlist list;void createWindow()recFrame = new JFrame("光榮榜");recpanel = new recPanel();recpanel.setLayout(null);close.addActionListener(new cl
39、oseListener();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 p
40、aintComponent(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;pos<10;pos+)g.drawString(pos, 25, 2
41、0*(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()/構(gòu)造函數(shù)name = new String10;score = new long10;for(int i
42、= 0;i<10;i+)namei = "王琢"scorei = 999;long getLowestScore()/返回榜內(nèi)最長時間return score9;long getHighestScore()return score0;void insertValue(String n,long s)/插入新元素int i = 0;long temp;String ntemp;while(s>scorei)i+;dotemp = scorei;ntemp = namei;scorei = s;namei = n;s = temp;n = ntemp;i+;while(i<10);nameInputer類:import java.awt.event.ActionEvent;import java.aw
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 可降解聚烯烴專用料項目風(fēng)險識別與評估綜合報告
- 公租住房申請書
- 企業(yè)信息轉(zhuǎn)換與備份管理規(guī)定
- 生態(tài)移民申請書
- 江蘇省南京市聯(lián)合體2024-2025學(xué)年高三上學(xué)期12月月考物理試卷(解析版)
- 天然氣利用可行性研究報告(2024年-2024年)
- 中國風(fēng)景名勝區(qū)管理市場前景及投資研究報告
- 2025年浙江嘉興市海寧市交通投資集團(tuán)有限公司招聘筆試參考題庫附帶答案詳解
- 申請銀行承兌匯票申請書
- 醫(yī)師定期考核臨床類人文醫(yī)學(xué)知識考試題及答案
- 心臟起搏器植入指南
- 融于教學(xué)的形成性評價
- 中國古代突騎研究
- 專題07 二次函數(shù)與幾何圖形綜合問題(復(fù)習(xí)講義)(原卷版)-二輪要點歸納與典例解析
- 20以內(nèi)進(jìn)位加法100題(精心整理6套-可打印A4)
- 高中語文統(tǒng)編版(部編版)必修下冊第六單元 大單元公開課一等獎創(chuàng)新教學(xué)設(shè)計
- 技術(shù)標(biāo)(城鎮(zhèn)老舊小區(qū)改造工程)
- 山東省各地市地圖課件
- 2022年4月天津高考英語試題-(第一次)
- LH制造中心組織架構(gòu)圖職能
- 醫(yī)院重點崗位工作人員輪崗制度
評論
0/150
提交評論