版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、 面向对象程序设计课程设计报告题 目: 潜艇大作战软件设计与实现 院 (系): 信息科学与工程学院 专业班级: 计算机科学与技术1201班 学生姓名: 程伟 学 号: 20121183011 指导教师: 吴奕 20 14 年 12 月 29 日至20 15 年 1 月 9 日华中科技大学武昌分校制 面向对象程序设计 课程设计任务书一、设计(调查报告/论文)题目 潜艇大作战软件设计与实现二、设计(调查报告/论文)主要内容内容:设计一个战舰猎捕潜艇的游戏,玩家以键盘输入来控制战舰的行动,猎杀潜艇获得分数,进入前十名有排名。 基本功能与要求:1首先绘制一个首页,首页包括开始,退出按钮,采用事件监听。
2、2进入游戏后使用,通过读取文件,读出以前的最好成绩,使用菜单按钮开始游戏。3通过方向键来控制潜艇的移动方向,空格键释放鱼雷。4 击中敌人后加分,同时被敌人击中一次就扣除一次机会,共三次机会三、原始资料1.面向对象程序设计课程设计指导书2. 耿祥义JAVA大学实用教程北京:清华大学出版社,2009四、要求的设计(调查/论文)成果1.课程设计报告2.课程设计软件五、进程安排布置任务,查找资料、需求分析1天总体设计1天详细设计1.5天编制源程序实现3.5天测试与修改1天撰写课设报告2天六、主要参考资料1 张广彬. Java课程设计案例精编(第二版).北京:清华大学出版社, 2011. 2 耿祥义.
3、Java课程设计(第二版).北京:清华大学出版社, 2008.3 耿祥义JAVA大学实用教程北京:清华大学出版社,20094 邹林达陈国君 Java2程序设计基础北京:清华大学出版社,20105 邹林达Java2程序设计基础实验指导北京:清华大学,2010指导教师(签名): 20 年 月 日目 录1需求与总体设计11.1需求分析11.1.1界面需求11.1.2性能需求11.2总体设计思路,功能图,类图11.2.1程序设计思路11.2.2功能图21.2.3类图32详细设计42.1详细设计思路与类的说明、细化42.1.1流程图42.1.2类的说明53编码实现63.1 JAVA编码实现64系统测试7
4、24.1开始游戏的界面724.2进入游戏并选择开始734.3暂停游戏的界面744.4前十名对话框与排名界面75总 结761需求与总体设计1.1需求分析1.1.1界面需求界面是一个窗口,游戏在此窗口上运行,窗口上有分数设置。1.1.2性能需求此游戏分敌方潜艇(若干)和我方战舰(生命值3条)两个角色,敌方潜艇在水下攻击我方战舰,我方战舰于水上猎杀敌方潜艇。1.2总体设计思路,功能图,类图1.2.1程序设计思路此游戏的关键点是潜艇是否被击中的判断,整个屏幕是个二维坐标系,军舰在一个水平位置移动,当在某个位置发射鱼雷,判断鱼雷的图片与不断移动的潜艇图片是否会有重合的地方,如果有发生爆炸,如果没有继续移
5、动。1.2.2功能图1.2.3类图2详细设计2.1详细设计思路与类的说明、细化2.1.1流程图本游戏的基本运行流程是启动后把整个画布昨为一个线程,随时准备响应用户按键操作K的响应,100ms扫描一次潜艇和水雷等物体的运行状态。在扫描潜艇和水雷运动时,执行各个物体画面的移动方法。程序并不为每隔新增潜艇和水雷开启一个新线程,太多线程会造成程序的性能直线下降;而是将每一个物体类型直接加入到画布中,每当一个新物体产生,将直接在画布上画出;当物体消除时,将直接在画布上被消除。这样就形成以种注册机制,所有游戏物体产生和消除都需要画布注册,画布拥有不同类型物体的所有“名单”,所有,当执行的命令只是需要遍历画
6、布中所有游戏物体,依次执行每隔游戏物体中定义的移动方法即可。2.1.2类的说明Blast:潜艇发射鱼雷击中战舰后爆炸的效果。Bum:炸弹FrameObservable:监听者模式来监听游戏画面切换。HelpDialog:游戏规则的对话框,提示游戏规则信息。Hit:潜艇被击中的爆炸效果。InfoDialog:关于我们的对话框,关于游戏的一些信息。InputDialog:玩家得分进图前十名的提示对话框。MainPanel:进图游戏的主界面面板。MyButton:绘制游戏背景MyDialog: 暂停游戏的提示对话框。MyFrame:游戏窗体。MyPanel:主程序ScoreDialog:玩家没有进入
7、前十名游戏就结束的提示对话框。Submarine:潜艇对象。SubmarineWarMain:重新开始游戏。TimeManager:计时器产生潜艇。TimeManager2:计时器产生鱼雷。Torpedo:潜艇发射的鱼雷对象。WallDialog:玩家进入前十名之后的成绩排名信息对话框。WarShip:战舰对象。3编码实现3.1 JAVA编码实现package com.stj.views;import java.awt.Color;import java.awt.Graphics2D;import java.util.Random;/* 潜艇发射的鱼雷击中战舰后爆炸的效果对象,原理:绘制多个从
8、小到大然后从大到小的圆显示爆炸效果 */public class Blast implements Runnableprivate int X; private int Y;private int r = 2,3,5,8,12,15,18,20,25,30,33,25,17,15,13,9,5; /圆的半径集合private int step = 0; /集合中圆的位置标记private boolean flag = false;public Blast(int x,int y)this.X = x;this.Y = y;public void drawBlast(Graphics2D g)R
9、andom random =new Random();int c = random.nextInt(2);if(c = 0)g.setColor( Color.RED);else if(c = 1)g.setColor(Color.ORANGE);if(this.step < r.length)/System.out.println("draw");/g.setColor(Color.gray);g.fillOval(this.X, this.Y, this.rthis.step, this.rthis.step);/step +;public void run()
10、while(!this.flag)step +;/System.out.println(step);try Thread.sleep(10); catch (InterruptedException e) / TODO Auto-generated catch blocke.printStackTrace();if(this.step = r.length)this.flag = true;public int getX() return X;public void setX(int x) X = x;public int getY() return Y;public void setY(in
11、t y) Y = y;public boolean isFlag() return flag;public void setFlag(boolean flag) this.flag = flag;package com.stj.views;import java.awt.*;import javax.swing.ImageIcon;public class Bumb implements Runnableprivate WarShip ship;private int beginX;private int beginY;private int width = 5;private int hei
12、ght = 15;public boolean flag = false;private MyPanel panel;private Image image;public Bumb(MyPanel p,WarShip ship)this.beginX= ship.getBeginX()+20;this.beginY = ship.getBeginY()+20;this.panel = p;this.ship = ship;image = Toolkit.getDefaultToolkit().getImage("imgs/炸弹.png");image = new Image
13、Icon(image).getImage();this.width = image.getWidth(panel);this.height = image.getHeight(panel);/System.out.println("bumb width:"+this.width);/System.out.println("bumb height:"+this.height);public void run() while(!flag)this.moveDown();if(this.panel.isStop() = true)synchronized(My
14、Panel.subLock)tryMyPanel.subLock.wait();catch(Exception e)e.printStackTrace();this.flag = true;tryThread.sleep(10);catch(Exception event)event.printStackTrace();this.flag = true;public void drawBumb(Graphics2D g)/g.setColor(Color.BLACK);/g.fillOval(this.beginX, this.beginY, this.radius, this.radius)
15、;g.drawImage(image, this.beginX, this.beginY, this.panel);public void moveDown()this.beginY += 1;if(this.beginY > (this.panel.getHeight() - this.height / 2)flag = true;/this.panel.repaint();public WarShip getShip() return ship;public void setShip(WarShip ship) this.ship = ship;public int getBegin
16、X() return beginX;public void setBeginX(int beginX) this.beginX = beginX;public int getBeginY() return beginY;public void setBeginY(int beginY) this.beginY = beginY;public int getWidth() return width;public void setWidth(int width) this.width = width;public int getHeight() return height;public void
17、setHeight(int height) this.height = height;import java.util.Observable;/*监听者模式来监听游戏画面切换,从游戏进入画面到游戏主画面,用MyFrame对象监听MainPanel */public class FrameObservable extends Observablepublic void notifyObservers(Object arg)/System.out.println("notify");this.setChanged();super.notifyObservers(arg); /
18、arg为被观察的对象import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.LinearGradientPaint;import java.awt.Shape;import java.awt.Frame;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.event.MouseMotionListener;impo
19、rt java.awt.geom.AffineTransform;import java.awt.geom.Area;import java.awt.geom.Ellipse2D;import java.awt.geom.Point2D;import java.awt.geom.RoundRectangle2D;import javax.swing.JDialog;import javax.swing.JFrame;import javax.swing.plaf.basic.BasicGraphicsUtils;import com.sun.awt.AWTUtilities;/*游戏规则的对话
20、框,提示游戏规则信息 */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 HelpDia
21、log(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.getWidth
22、(), 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.Float(this
23、.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("游戏规则");g2.setFont(ne
24、w 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, context,
25、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("华文行楷",F
26、ont.BOLD,15);g2.setColor(Color.yellow);BasicGraphicsUtils.drawString(g2, context2, 100, 50, 140);/g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING , RenderingHints.VALUE_ANTIALIAS_ON);this.repaintShape(); /设置对话框为圆角矩形public void setVisiableRigeon(int width,int height)Shape shape = new RoundRectang
27、le2D.Float(20,20,this.getWidth() - 40,this.getHeight()-40,15.0f,15.0f);Shape shape2 = new Ellipse2D.Double(this.getWidth() - 40, 10,30,30);Area area = new Area(shape);Area area2 = new Area(shape2);area.add(area2);AWTUtilities.setWindowShape(this,area);public void mouseClicked(MouseEvent arg0) public
28、 void mouseEntered(MouseEvent arg0) /System.out.println("enter");public void mouseExited(MouseEvent arg0) /System.out.println("exit");this.flag = false;this.isDraw = false;if(!this.isOutDraw)/this.repaint(this.getWidth() - 40,10,30,30);this.repaintShape();this.isOutDraw = true;pu
29、blic void mousePressed(MouseEvent arg0) int x = arg0.getX();int y = arg0.getY();Shape shape = new Ellipse2D.Double(this.getWidth() - 40, 10, 30, 30);/System.out.print(111111);if(shape.contains(x,y)this.panel.goOn();this.setVisible(false);public void mouseReleased(MouseEvent arg0) / TODO Auto-generat
30、ed method stubpublic void mouseDragged(MouseEvent arg0) / TODO Auto-generated method stubpublic void mouseMoved(MouseEvent arg0) int x = arg0.getX();int y = arg0.getY();/System.out.print(x + " ");/System.out.println(y);Shape shape = new Ellipse2D.Double(this.getWidth() - 40, 10, 30, 30);Sh
31、ape shape2 = new Ellipse2D.Double(this.getWidth() - 40, 10,30,30);Area area = new Area(shape);Area area2 = new Area(shape2);area.add(area2);if(shape.contains(x,y)this.flag = true;this.isOutDraw = false;if(!this.isDraw)/this.repaint(this.getWidth() - 40,10,30,30);this.repaintShape();this.isDraw = tru
32、e;else if(!shape.contains(x,y)this.flag = false;this.isDraw = false;if(!this.isOutDraw)/this.repaint(this.getWidth() - 40,10,30,30);this.repaintShape();this.isOutDraw = true;public void repaintShape()Graphics2D g2 = (Graphics2D) this.getGraphics();if(!flag)g2.setColor(Color.gray);else if(flag)g2.set
33、Color(Color.red);g2.fillOval(this.getWidth() - 40, 10, 30, 30);g2.setColor(Color.white);Shape shape = new RoundRectangle2D.Float( this.getWidth() - 40 + 3,10 + 15 - 3,24,6,1.0f,1.0f);AffineTransform af = AffineTransform.getRotateInstance(0.7,this.getWidth() - 40 + 3 + 12,10 + 15 -3 + 3);shape = af.c
34、reateTransformedShape(shape);g2.fill(shape);Shape shape2 = new RoundRectangle2D.Float( this.getWidth() - 40 + 3,10 + 15 - 3,24,6,1.0f,1.0f);AffineTransform af2 = AffineTransform.getRotateInstance(-0.7,this.getWidth() - 40 + 3 + 12,10 + 15 -3 + 3);shape2 = af2.createTransformedShape(shape2);g2.fill(s
35、hape2);package com.stj.views;import java.awt.Graphics2D;import java.awt.Image;import java.awt.Toolkit;import javax.swing.ImageIcon;/*潜艇被击中的爆炸效果,通过图片显示 */public class Hit implements Runnable private MyPanel panel; /主面板private Image image; /图片private int liveTime = 500; /爆炸效果显示的时间默认为500毫秒private int b
36、eginX = 0; /位置 x yprivate int beginY = 0;private boolean isRunning = false; /游戏是否正在运行标志public Hit(int 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(
37、);public void drawHitting(Graphics2D g)g.drawImage(this.image, this.beginX,this.beginY,this.panel);public void run()while(!this.isRunning)try Thread.sleep(this.liveTime); catch (InterruptedException e) / TODO Auto-generated catch blocke.printStackTrace();this.isRunning = true;public boolean isRunnin
38、g() return isRunning;public void setRunning(boolean isRunning) this.isRunning = isRunning;package com.stj.views;import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.LinearGradientPaint;import java.awt.Shape;import java.awt.Frame;import java.a
39、wt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.event.MouseMotionListener;import java.awt.geom.AffineTransform;import java.awt.geom.Area;import java.awt.geom.Ellipse2D;import java.awt.geom.Point2D;import java.awt.geom.RoundRectangle2D;import javax.swing.JDialog;import javax.s
40、wing.JFrame;import javax.swing.plaf.basic.BasicGraphicsUtils;import com.sun.awt.AWTUtilities;/* 关于我们的对话框,关于游戏的一些信息 */public class InfoDialog extends JDialog implements MouseMotionListener,MouseListenerprivate static final long serialVersionUID = 1L;protected JFrame frame1;private MyPanel panel;priva
41、te boolean flag = false;private boolean isDraw = false;private boolean isOutDraw = false;/private MouseInfo mouse;public InfoDialog(Frame frame,boolean modal,MyPanel panel)super(frame,modal);this.panel = panel;this.addMouseMotionListener(this);this.addMouseListener(this);this.setLocation(frame.getBo
42、unds().x + 180,frame.getBounds().y + 200);this.setSize(300, 200);this.setUndecorated(true); this.setVisiableRigeon(this.getWidth(), this.getHeight();this.setVisible(true);public void paint(Graphics g)super.paint(g);Graphics2D g2 = (Graphics2D) g;/g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING ,
43、 RenderingHints.VALUE_ANTIALIAS_ON);Point2D start = new Point2D.Float(this.getWidth()/2, 0);Point2D end = new Point2D.Float(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, col
44、ors);g2.setPaint(p);g2.fillRect(0,0, this.getWidth(), this.getHeight();String title = new String("关于我们");g2.setFont(new Font("楷体",Font.BOLD,25);g2.setColor(Color.yellow);BasicGraphicsUtils.drawString(g2, title, 100, 90, 50);String context = new String("本游戏为Java awt和swing组件&q
45、uot;);g2.setFont(new Font("楷体",Font.BOLD,15);g2.setColor(Color.yellow);BasicGraphicsUtils.drawString(g2, context, 100, 35, 80);String context1 = new String("模拟的休闲小游戏潜艇大战");g2.setFont(new Font("楷体",Font.BOLD,15);g2.setColor(Color.yellow);BasicGraphicsUtils.drawString(g2,
46、 context1, 100, 35, 100);String context2 = new String("由于本人时间有限,没有对代码");g2.setFont(new Font("楷体",Font.BOLD,15);g2.setColor(Color.yellow);BasicGraphicsUtils.drawString(g2, context2, 100, 35, 120);String context3 = new String("进行优化和界面UI处理,所以有");g2.setFont(new Font("楷
47、体",Font.BOLD,15);g2.setColor(Color.yellow);BasicGraphicsUtils.drawString(g2, context3, 100, 35, 140);String context4 = new String("很多的不足之处,仅做学习之用。");g2.setFont(new Font("楷体",Font.BOLD,15);g2.setColor(Color.yellow);BasicGraphicsUtils.drawString(g2, context4, 100, 35, 160);/g2
48、.setRenderingHint(RenderingHints.KEY_ANTIALIASING , RenderingHints.VALUE_ANTIALIAS_ON);this.repaintShape(); public void setVisiableRigeon(int width,int height)Shape shape = new RoundRectangle2D.Float(20,20,this.getWidth() - 40,this.getHeight()-40,15.0f,15.0f);Shape shape2 = new Ellipse2D.Double(this
49、.getWidth() - 40, 10,30,30);Area area = new Area(shape);Area area2 = new Area(shape2);area.add(area2);AWTUtilities.setWindowShape(this,area);public void mouseClicked(MouseEvent arg0) public void mouseEntered(MouseEvent arg0)/System.out.println("enter");public void mouseExited(MouseEvent ar
50、g0) /System.out.println("exit");this.flag = false;this.isDraw = false;if(!this.isOutDraw)/this.repaint(this.getWidth() - 40,10,30,30);this.repaintShape();this.isOutDraw = true;public void mousePressed(MouseEvent arg0) int x = arg0.getX();int y = arg0.getY();Shape shape = new Ellipse2D.Double(this.getWidth() - 40, 10, 30, 30);/System.out.print(111111);if(shape.contains(x,y)this.panel.goOn();this.setVisible(false);public void mouseReleased(MouseEvent arg0) / TODO Auto-generated method stubpublic void mouseDragged(MouseEvent arg0) / TODO Auto-generated method stubpublic void mou
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 磷酸生产工班组建设考核试卷含答案
- 海藻饲料肥料制作工岗前风险评估与管理考核试卷含答案
- 发电车乘务员QC管理水平考核试卷含答案
- 19.1.3 中位数和众数 课件 2025-2026学年华东师大版八年级数学下册
- 某麻纺厂安全防护操作规范
- 沈阳市高校全运体育馆赛后利用价值的多维剖析与策略探索
- 沂南供热PPP项目资产证券化:模式、挑战与发展路径
- 汽车消费服务领域竞争力剖析与预付费卡创新应用研究
- 商品房面积差异处理补充协议
- 城市园林绿化修剪技师考试试卷及答案
- 肢体麻木护理查房要点
- 英语可数与不可数名词专项练习
- 服务心理学(第四版)课件 项目四 任务二 激发消费动机
- 江苏省姜堰市蒋垛中学高二信息技术教学设计+试题
- 城市地铁线路EPC施工组织设计范文
- 北京市海淀区第五十七中学2024-2025学年八年级下学期期中英语试卷(含答案)
- 加油站员工安全培训教育档案台帐
- 蚊虫叮咬教学课件
- DB13T 2055-2014 学校安全管理规范
- T/CAPE 10001-2017设备管理体系要求
- 萨满文化课件
评论
0/150
提交评论