




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、.面向?qū)ο笳n程設(shè)計(jì)潛艇大作戰(zhàn)小游戲 目錄1. 需求分析21.1需求分析:21.2功能設(shè)計(jì)22.概要設(shè)計(jì)22.1程序設(shè)計(jì)思路22.2程序運(yùn)行界面32.3 流程圖33.各模塊的功能及程序說(shuō)明43.1主界面代碼43.2潛艇對(duì)象的實(shí)現(xiàn)83.3潛艇爆炸的實(shí)現(xiàn)133.4游戲說(shuō)明143.5時(shí)間計(jì)時(shí)器163.5.1計(jì)時(shí)產(chǎn)生潛艇163.5.2計(jì)時(shí)產(chǎn)生魚雷183.6 讀取文件,記錄分?jǐn)?shù)194.實(shí)驗(yàn)心得261. 需求分析1.1需求分析:本程序的要求為(1) 使用Java建立一個(gè)小游戲(2) 程序中用到Java課程中的重點(diǎn)知識(shí)1.2功能設(shè)計(jì)本游戲的功能有以下幾個(gè)方面:(1) 首先繪制一個(gè)首頁(yè),首頁(yè)包括開始、退出按鈕,
2、采用事件監(jiān)聽。(2) 進(jìn)入游戲后使用,通過讀取文件,讀出以前的最好成績(jī),使用菜單按鈕開始游戲(3) 通過方向鍵來(lái)控制潛艇的移動(dòng)方向,空格鍵釋放魚雷(4) 擊中敵人后加分,同時(shí)被敵人擊中一次就扣除一次機(jī)會(huì),共3次機(jī)會(huì)2.概要設(shè)計(jì)2.1程序設(shè)計(jì)思路此游戲的關(guān)鍵點(diǎn)是潛艇是否被擊中的判斷,整個(gè)屏幕是個(gè)二維坐標(biāo)系,軍艦在一個(gè)水平位置移動(dòng),當(dāng)在某個(gè)位置發(fā)射魚雷,判斷魚雷的圖片與不斷移動(dòng)的潛艇圖片是否有重合的地方,如果有發(fā)生爆炸,如果沒有繼續(xù)移動(dòng)。2.2程序運(yùn)行界面2.3 流程圖本游戲的基本運(yùn)行流程是啟動(dòng)后把整個(gè)畫布作為一個(gè)線程,隨時(shí)準(zhǔn)備響應(yīng)用戶按鍵操作的K響應(yīng),100ms掃描一次潛艇和水雷等物體的運(yùn)行狀態(tài)
3、。在掃描潛艇和水雷運(yùn)動(dòng)時(shí),執(zhí)行各個(gè)物體畫面的移動(dòng)方法。程序并不為每隔新增潛艇和水雷開啟一個(gè)新線程,太多線程會(huì)造成程序的性能直線下降;而是將每一個(gè)物體類型直接加入到畫布中,每當(dāng)一個(gè)新物體產(chǎn)生,將直接在畫布上畫出;當(dāng)物體消除時(shí),將直接在畫布上被消除。這樣就形成一種注冊(cè)機(jī)制,所有游戲物體的產(chǎn)生和消除都需要畫布注冊(cè),畫布擁有不同類型物體的所有“名單”,所以,當(dāng)執(zhí)行移動(dòng)命令是只需要遍歷畫布中所有游戲物體,依次執(zhí)行每隔游戲物體中定義的移動(dòng)方法即可。 3.各模塊的功能及程序說(shuō)明3.1主界面代碼主界面設(shè)計(jì)是使用坐標(biāo)系,具體實(shí)現(xiàn)圖如下:public class MainPanel extends JP
4、anel private static final long serialVersionUID = 1L;private MyButton startButton;private MyButton exitButton;private JLabel helpLabel;private JLabel helpLabel1;private JLabel helpLabel2;private JLabel helpLabel3;private Image image;private JLabel centerlabel;private boolean isStart;private boolean
5、isExit;private Observable obs;public MainPanel(Observable ob) obs = ob;/初始化對(duì)象startButton = new MyButton("進(jìn)入游戲");exitButton = new MyButton("退出游戲");helpLabel = new JLabel();helpLabel1 = new JLabel();helpLabel2 = new JLabel();helpLabel3 = new JLabel();centerlabel = new JLabel(); thi
6、s.setLayout(new BorderLayout(); this.helpLabel.setPreferredSize(new Dimension(645,291); this.helpLabel1.setPreferredSize(new Dimension(180,80); this.helpLabel2.setPreferredSize(new Dimension(215,80); this.helpLabel3.setPreferredSize(new Dimension(645,80); this.centerlabel.setPreferredSize(new Dimens
7、ion(460,80); centerlabel.setLayout(new GridLayout(2,1); centerlabel.add(this.startButton); centerlabel.add(this.exitButton); this.centerlabel.setBackground(new Color(255,255,0); this.add(helpLabel,BorderLayout.NORTH); this.add(helpLabel1,BorderLayout.EAST); this.add(helpLabel2,BorderLayout.WEST); th
8、is.add(helpLabel3,BorderLayout.SOUTH); this.add(centerlabel,BorderLayout.CENTER); /類中聲明了游戲中需要的各種對(duì)象,并載入游戲中的圖片 image = Toolkit.getDefaultToolkit().getImage("imgs/主界面112.png"); / image = new ImageIcon(image).getImage(); /對(duì)開始按鈕的監(jiān)聽 this.startButton.addActionListener( new ActionListener() public
9、 void actionPerformed(ActionEvent e) boolean flag = true; MainPanel.this.setIsStart(flag); / System.out.println(MainPanel.this.getIsStart(); MainPanel.this.obs.notifyObservers(MainPanel.this); / System.out.println("isStart"); ); /對(duì)結(jié)束事件的監(jiān)聽 this.exitButton.addActionListener( new ActionListen
10、er() public void actionPerformed(ActionEvent e) MainPanel.this.setExit(true); );public void paint(Graphics g)super.paint(g);Graphics2D g2 = (Graphics2D) g; g2.drawImage(image,0,0, this.getWidth(), this.getHeight(),this);super.paintComponents(g);public boolean getIsStart()return this.isStart;public v
11、oid setIsStart(boolean isStart)this.isStart = isStart;public boolean isExit() return isExit;public void setExit(boolean isExit) this.isExit = isExit;3.2潛艇對(duì)象的實(shí)現(xiàn)public class Submarine implements Runnableprivate int X; /位置x,yprivate int Y;private int dx; /移動(dòng)距離private int m; /方向:0代表向左 1代表向右private WarSh
12、ip ship; private MyPanel panel;private int weight = 65; /默認(rèn)長(zhǎng)度和寬度,數(shù)據(jù)來(lái)自圖片大小private int height = 20;public boolean flag = false; /運(yùn)行標(biāo)記private Image image; /圖片對(duì)象/private static int num = 0;public Submarine(WarShip ship,MyPanel panel)this.ship = ship;this.panel = panel;this.dx = 1;/隨機(jī)產(chǎn)生潛艇圖片和運(yùn)動(dòng)方向this.m =
13、(int) (Math.random() * 2); if(this.m = 0) Random r = new Random();int num = r.nextInt(3);if(num = 0)image = Toolkit.getDefaultToolkit().getImage("imgs/潛艇1.png");/Toolkit.getDefaultToolkit().createImage( "C:1.JPG "),用異步的方式創(chuàng)建圖片。當(dāng)線程執(zhí)行到_img.getWidth(this)語(yǔ)句時(shí),創(chuàng)建圖片的線程還沒準(zhǔn)備好圖片所以會(huì)返回-1。ima
14、ge = new ImageIcon(image).getImage();else if(num = 1)image = Toolkit.getDefaultToolkit().getImage("imgs/潛艇2.png");image = new ImageIcon(image).getImage();else if(num = 2 )image = Toolkit.getDefaultToolkit().getImage("imgs/潛艇8.png");image = new ImageIcon(image).getImage(); if(this
15、.m = 1) Random r1 = new Random();int num = r1.nextInt(4); if(num = 0 )image = Toolkit.getDefaultToolkit().getImage("imgs/潛艇3.png");image = new ImageIcon(image).getImage();else if(num = 1 )image = Toolkit.getDefaultToolkit().getImage("imgs/潛艇4.png");image = new ImageIcon(image).ge
16、tImage();else if(num = 2 )image = Toolkit.getDefaultToolkit().getImage("imgs/潛艇6.png");image = new ImageIcon(image).getImage();else if(num = 3 )image = Toolkit.getDefaultToolkit().getImage("imgs/潛艇7.png");image = new ImageIcon(image).getImage(); this.weight = image.getWidth(panel
17、);this.height = image.getHeight(panel);if(m = 0)this.X = this.panel.getWidth() - this.weight;if(m = 1)this.X = 0;Random ry = new Random();int y1 = ry.nextInt(panel.getHeight() + 180;while(y1+this.getHeight() >= panel.getHeight()y1 = ry.nextInt(panel.getHeight() + 180;this.Y = y1;/計(jì)時(shí)器每隔一段時(shí)間產(chǎn)生魚雷對(duì)象T
18、imeManager2 tm2 = new TimeManager2(this,this.panel,this.ship,this.panel.getTorpedoArray();Thread t = new Thread(tm2);t.start();public void drawSubmarine(Graphics2D g) g.drawImage(image,this.X, this.Y, panel); public void moveLeft()/System.out.println("潛水艇運(yùn)動(dòng)");this.X -= dx;/System.out.print
19、ln(this.X);this.panel.repaint();if(this.X < 0)this.flag = true;public void moveright()this.X += dx;/this.panel.repaint();if(this.X > this.panel.getWidth()this.flag = true;public void run() /System.out.println("線程激活");while(!flag)/System.out.println("222");if(this.m = 0)this
20、.moveLeft();if(this.m = 1)this.moveright();if(this.panel.isStop()synchronized(MyPanel.subLock)tryMyPanel.subLock.wait();catch(Exception e)e.printStackTrace();this.flag = true;tryThread.sleep(10);catch(Exception e)e.printStackTrace();this.flag = true;public int getX() return X;public void setX(int x)
21、 X = x;public int getY() return Y;public void setY(int y) Y = y;public int getDx() return dx;public void setDx(int dx) this.dx = dx;public int getWeight() return weight;public void setWeight(int weight) this.weight = weight;public int getHeight() return height;public void setHeight(int height) this.
22、height = height;3.3潛艇爆炸的實(shí)現(xiàn)/* * 潛艇被擊中的爆炸效果,通過圖片顯示 */public class Hit implements Runnable private MyPanel panel; /主面板private Image image; /圖片private int liveTime = 500; /爆炸效果顯示的時(shí)間默認(rèn)為500毫秒private int beginX = 0; /位置 x yprivate int beginY = 0;private boolean isRunning = false; /游戲是否正在運(yùn)行標(biāo)志public Hit(int
23、x,int y,MyPanel panel)this.beginX = x;this.beginY = y;this.panel = panel;this.image = Toolkit.getDefaultToolkit().getImage("imgs/炸彈效果.png");this.image = new ImageIcon(this.image).getImage();public void drawHitting(Graphics2D g)g.drawImage(this.image, this.beginX,this.beginY,this.panel);pub
24、lic void run()while(!this.isRunning)try Thread.sleep(this.liveTime); catch (InterruptedException e) / TODO Auto-generated catch blocke.printStackTrace();this.isRunning = true;public boolean isRunning() return isRunning;public void setRunning(boolean isRunning) this.isRunning = isRunning;3.4游戲說(shuō)明/* *
25、*游戲規(guī)則的對(duì)話框,提示游戲規(guī)則信息 */public class HelpDialog extends JDialog implements MouseMotionListener,MouseListenerprivate static final long serialVersionUID = 1L;protected JFrame frame1; private MyPanel panel;private boolean flag = false;private boolean isDraw = false;private boolean isOutDraw = false;public
26、 HelpDialog(Frame frame,boolean modal,MyPanel panel)super(frame,modal);this.panel = panel;this.addMouseMotionListener(this);this.addMouseListener(this);this.setLocation(frame.getBounds().x + 180,frame.getBounds().y + 200);this.setSize(300, 200);this.setUndecorated(true); this.setVisiableRigeon(this.
27、getWidth(), this.getHeight();this.setVisible(true);public void paint(Graphics g)super.paint(g);Graphics2D g2 = (Graphics2D) g;/g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING , RenderingHints.VALUE_ANTIALIAS_ON);Point2D start = new Point2D.Float(this.getWidth()/2, 0);Point2D end = new Point2D.Fl
28、oat(this.getWidth()/2, this.getHeight();float dist = 0.05f,1.0f;Color colors = new Color(58,95,205), Color.CYAN;LinearGradientPaint p =new LinearGradientPaint (start,end, dist, colors);g2.setPaint(p);g2.fillRect(0,0, this.getWidth(), this.getHeight();String title = new String("游戲規(guī)則");g2.se
29、tFont(new Font("華文行楷",Font.BOLD,25);g2.setColor(Color.yellow);BasicGraphicsUtils.drawString(g2, title, 100, 90, 50);String context = new String("按 <- 或 A 鍵 控制軍艦向左");g2.setFont(new Font("華文行楷",Font.BOLD,15);g2.setColor(Color.yellow);BasicGraphicsUtils.drawString(g2, c
30、ontext, 100, 50, 100);String context1 = new String("按 -> 或 D 鍵 控制軍艦向右");g2.setFont(new Font("華文行楷",Font.BOLD,15);g2.setColor(Color.yellow);BasicGraphicsUtils.drawString(g2, context1, 100, 50, 120);String context2 = new String("按空格鍵扔炸彈");g2.setFont(new Font("華文行楷
31、",Font.BOLD,15);g2.setColor(Color.yellow);BasicGraphicsUtils.drawString(g2, context2, 100, 50, 140);/g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING , RenderingHints.VALUE_ANTIALIAS_ON);this.repaintShape(); 3.5時(shí)間計(jì)時(shí)器3.5.1計(jì)時(shí)產(chǎn)生潛艇public class TimeManager implements Runnable private WarShip ship
32、;private MyPanel panel;private int speed = 1000;public TimeManager(WarShip ship,MyPanel panel)this.ship = ship;this.panel = panel;public void run() Random r = new Random();while(this.panel.isRunning()if(this.panel.isStop() = true)/System.out.println("777");synchronized(MyPanel.subLock)tryM
33、yPanel.subLock.wait();catch(Exception e)e.printStackTrace();/this.flag = true;this.panel.endGame();Submarine sm = new Submarine(this.ship,this.panel);this.panel.getSubmarineArray().add(sm);Thread t = new Thread(sm);t.start();tryThread.sleep(this.speed + r.nextInt(this.speed * 3);catch(Exception e)e.
34、printStackTrace();public int getSpeed() return speed;public void setSpeed(int speed) this.speed = speed;3.5.2計(jì)時(shí)產(chǎn)生魚雷public class TimeManager2 implements Runnableprivate WarShip ship;private ArrayList<Torpedo> torpedoArray ;private MyPanel panel;private Submarine sm;public TimeManager2(Submarine
35、 sm,MyPanel panel,WarShip ship,ArrayList<Torpedo> torpedoArray)this.sm = sm; this.torpedoArray = torpedoArray;this.panel = panel;this.ship = ship;public void run() Random r = new Random();while(!this.sm.flag)/System.out.println("333");if(this.panel.isStop() = true)synchronized(MyPane
36、l.subLock)/System.out.println("stop");tryMyPanel.subLock.wait();catch(Exception e)e.printStackTrace();/this.flag = true;this.panel.endGame();Torpedo tp = new Torpedo(this.panel,this.ship,this.sm);/System.out.println("1111111111111");this.torpedoArray.add(tp);Thread t = new Thread
37、(tp);t.start();tryint time = r.nextInt(4000) + 2000;Thread.sleep(time);catch(Exception e)e.printStackTrace();3.6 讀取文件,記錄分?jǐn)?shù)public class InputDialog extends JDialog/* * */private static final long serialVersionUID = 1L;private JButton submit; /提交按鈕private JLabel text1;private JLabel text2;private JLab
38、el text3;private JTextField field;private MyPanel panel;private JPanel centerPanel;private JPanel northPanel;private JPanel southPanel;public InputDialog(Frame frame,boolean modal,MyPanel panel)super(frame,modal);this.panel = panel;this.submit = new JButton("提交");this.text1 = new JLabel(&q
39、uot;恭喜您進(jìn)入前十!");this.text1.setFont(new Font("楷體",Font.BOLD,15);this.text1.setHorizontalAlignment(JLabel.CENTER);this.text1.setVerticalAlignment(JLabel.BOTTOM);this.text1.setPreferredSize(new Dimension(300,90);this.text2 = new JLabel("請(qǐng)輸入您的姓名:");this.text2.setFont(new Font(&qu
40、ot;楷體",Font.BOLD,12);this.text2.setHorizontalAlignment(JLabel.CENTER);this.text2.setPreferredSize(new Dimension(130,20);this.text3 = new JLabel("總分:"+ this.panel.getScore();this.text3.setFont(new Font("楷體",Font.BOLD,12);this.text3.setHorizontalAlignment(JLabel.CENTER);this.t
41、ext3.setPreferredSize(new Dimension(300,30);this.field = new JTextField();this.field.setFont(new Font("楷體",Font.BOLD,15);this.field.setHorizontalAlignment(JLabel.CENTER);this.field.setPreferredSize(new Dimension(70,20);this.submit.setBackground(Color.orange);this.submit.setHorizontalAlignm
42、ent(JLabel.CENTER);this.submit.setPreferredSize(new Dimension(80,30);this.submit.setForeground(new Color(61,145,64);this.submit.setFont(new Font(" ",0,20);this.centerPanel = new JPanel(new FlowLayout(5);this.centerPanel.setPreferredSize(new Dimension(200,50);this.centerPanel.add(this.text2
43、);this.centerPanel.add(this.field);this.northPanel = new JPanel(new GridLayout(2,1);this.northPanel.setPreferredSize(new Dimension(300,90);this.northPanel.add(this.text1);this.northPanel.add(this.text3);JLabel southHelp1 = new JLabel();southHelp1.setPreferredSize(new Dimension(100, 50);JLabel southH
44、elp2 = new JLabel();southHelp2.setPreferredSize(new Dimension(100, 50);this.southPanel = new JPanel();this.southPanel.setPreferredSize(new Dimension(300,70);this.southPanel.add(southHelp1);this.southPanel.add(this.submit);this.southPanel.add(southHelp2); JLabel east = new JLabel(); east.setPreferred
45、Size(new Dimension(20, 30); JLabel west = new JLabel(); west.setPreferredSize(new Dimension(60, 20);Container c = this.getContentPane();c.add(this.northPanel,BorderLayout.NORTH);c.add(this.centerPanel,BorderLayout.CENTER);c.add(this.southPanel,BorderLayout.SOUTH);c.add(east,BorderLayout.EAST);c.add(
46、west,BorderLayout.WEST);this.setContentPane(c);this.setLocation(frame.getBounds().x + 180,frame.getBounds().y + 200);this.setUndecorated(true);AWTUtilities.setWindowOpacity(this, 0.7F);this.setSize(300, 200);this.submit.addActionListener(new ActionListener()public void actionPerformed(ActionEvent ar
47、g0)/得到用戶信息String name = InputDialog.this.field.getText().trim();String score = Integer.toString(InputDialog.this.panel.getScore();String pass = Integer.toString(InputDialog.this.panel.getPass();SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");/設(shè)置日期格式String date = df.format
48、(new Date();/ new Date()為獲取當(dāng)前系統(tǒng)時(shí)間String args = date.split(" ");String time = args0;String user = name + " " + score + " " + pass + " " + time;/將數(shù)據(jù)文件中的用戶信息取出,去掉最后一名,加入玩家信息,并按降序排序ArrayList<String> userList = new ArrayList<String>();BufferedReader br = null;BufferedWriter bw = null;tr
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年住宅認(rèn)購(gòu)合同樣本下載
- 2025年倉(cāng)儲(chǔ)續(xù)租合同示例文件
- 2025年合同管理與糾紛協(xié)調(diào)年
- 2025年貨物倉(cāng)儲(chǔ)保管合同樣本及費(fèi)用支付協(xié)議
- 2025年企業(yè)內(nèi)部承包責(zé)任合同樣本
- 2025年住宅產(chǎn)權(quán)變更合同協(xié)議書
- 2025年辦公場(chǎng)所建筑合同
- 建筑企業(yè)流動(dòng)資金借款合同書樣式8篇
- 2025年住宅小區(qū)園林景觀設(shè)計(jì)與施工合同
- 2025年建筑材料供應(yīng)及采購(gòu)合同示范文本
- 《我愛上班》朗誦稿
- 大唐杯5G大賽考試題庫(kù)原題真題版(含答案)
- 臨床重點(diǎn)專科申報(bào)書(麻醉、病理、檢驗(yàn))
- 2024屆高考英語(yǔ)復(fù)習(xí)語(yǔ)法填空課件
- JTGT F81-01-2004 公路工程基樁動(dòng)測(cè)技術(shù)規(guī)程
- 第14課當(dāng)代中國(guó)的外交課件-高中歷史選擇性必修一
- 出入境知識(shí)講座
- 設(shè)計(jì)服務(wù)項(xiàng)目應(yīng)急預(yù)案
- 義務(wù)教育科學(xué)課程標(biāo)準(zhǔn)(2022年版)解讀
- 質(zhì)量管理體系的文件與記錄控制
- 黑龍江農(nóng)業(yè)經(jīng)濟(jì)職業(yè)學(xué)院?jiǎn)握小队⒄Z(yǔ)》考試復(fù)習(xí)題庫(kù)(含答案)
評(píng)論
0/150
提交評(píng)論