版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、面向对象课程设计潜艇大作战小游戏 目录1. 需求分析21.1需求分析:21.2功能设计22.概要设计22.1程序设计思路22.2程序运行界面32.3 流程图33.各模块的功能及程序说明43.1主界面代码43.2潜艇对象的实现83.3潜艇爆炸的实现133.4游戏说明143.5时间计时器163.5.1计时产生潜艇163.5.2计时产生鱼雷183.6 读取文件,记录分数194.实验心得261. 需求分析1.1需求分析:本程序的要求为(1) 使用Java建立一个小游戏(2) 程序中用到Java课程中的重点知识1.2功能设计本游戏的功能有以下几个方面:(1) 首先绘制一个首页,首页包括开始、退出按钮,采
2、用事件监听。(2) 进入游戏后使用,通过读取文件,读出以前的最好成绩,使用菜单按钮开始游戏(3) 通过方向键来控制潜艇的移动方向,空格键释放鱼雷(4) 击中敌人后加分,同时被敌人击中一次就扣除一次机会,共3次机会2.概要设计2.1程序设计思路此游戏的关键点是潜艇是否被击中的判断,整个屏幕是个二维坐标系,军舰在一个水平位置移动,当在某个位置发射鱼雷,判断鱼雷的图片与不断移动的潜艇图片是否有重合的地方,如果有发生爆炸,如果没有继续移动。2.2程序运行界面2.3 流程图本游戏的基本运行流程是启动后把整个画布作为一个线程,随时准备响应用户按键操作的K响应,100ms扫描一次潜艇和水雷等物体的运行状态。
3、在扫描潜艇和水雷运动时,执行各个物体画面的移动方法。程序并不为每隔新增潜艇和水雷开启一个新线程,太多线程会造成程序的性能直线下降;而是将每一个物体类型直接加入到画布中,每当一个新物体产生,将直接在画布上画出;当物体消除时,将直接在画布上被消除。这样就形成一种注册机制,所有游戏物体的产生和消除都需要画布注册,画布拥有不同类型物体的所有“名单”,所以,当执行移动命令是只需要遍历画布中所有游戏物体,依次执行每隔游戏物体中定义的移动方法即可。3.各模块的功能及程序说明3.1主界面代码主界面设计是使用坐标系,具体实现图如下:public class MainPanel extends JPanel pr
4、ivate 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 isExit;
5、private Observable obs;public MainPanel(Observable ob) obs = ob;/初始化对象startButton = new MyButton(进入游戏);exitButton = new MyButton(退出游戏);helpLabel = new JLabel();helpLabel1 = new JLabel();helpLabel2 = new JLabel();helpLabel3 = new JLabel();centerlabel = new JLabel(); this.setLayout(new BorderLayout();
6、 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 Dimension(460,80); centerlabel.setLay
7、out(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); this.add(helpLabel3,BorderLayout.
8、SOUTH); this.add(centerlabel,BorderLayout.CENTER); /类中声明了游戏中需要的各种对象,并载入游戏中的图片 image = Toolkit.getDefaultToolkit().getImage(imgs/主界面112.png); / image = new ImageIcon(image).getImage(); /对开始按钮的监听 this.startButton.addActionListener( new ActionListener() public void actionPerformed(ActionEvent e) boolea
9、n flag = true; MainPanel.this.setIsStart(flag); / System.out.println(MainPanel.this.getIsStart(); MainPanel.this.obs.notifyObservers(MainPanel.this); / System.out.println(isStart); ); /对结束事件的监听 this.exitButton.addActionListener( new ActionListener() public void actionPerformed(ActionEvent e) MainPan
10、el.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 void setIsStart(boolean isStart)this.isStart = isStart;p
11、ublic boolean isExit() return isExit;public void setExit(boolean isExit) this.isExit = isExit;3.2潜艇对象的实现public class Submarine implements Runnableprivate int X; /位置x,yprivate int Y;private int dx; /移动距离private int m; /方向:0代表向左 1代表向右private WarShip ship; private MyPanel panel;private int weight = 65;
12、 /默认长度和宽度,数据来自图片大小private int height = 20;public boolean flag = false; /运行标记private Image image; /图片对象/private static int num = 0;public Submarine(WarShip ship,MyPanel panel)this.ship = ship;this.panel = panel;this.dx = 1;/随机产生潜艇图片和运动方向this.m = (int) (Math.random() * 2); if(this.m = 0) Random r = ne
13、w Random();int num = r.nextInt(3);if(num = 0)image = Toolkit.getDefaultToolkit().getImage(imgs/潜艇1.png);/Toolkit.getDefaultToolkit().createImage( C:1.JPG ),用异步的方式创建图片。当线程执行到_img.getWidth(this)语句时,创建图片的线程还没准备好图片所以会返回-1。image = new ImageIcon(image).getImage();else if(num = 1)image = Toolkit.getDefault
14、Toolkit().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.m = 1) Random r1 = new Random();int num = r1.nextInt(4); if(num = 0 )image = Toolkit.getDefaultToolkit
15、().getImage(imgs/潜艇3.png);image = new ImageIcon(image).getImage();else if(num = 1 )image = Toolkit.getDefaultToolkit().getImage(imgs/潜艇4.png);image = new ImageIcon(image).getImage();else if(num = 2 )image = Toolkit.getDefaultToolkit().getImage(imgs/潜艇6.png);image = new ImageIcon(image).getImage();el
16、se if(num = 3 )image = Toolkit.getDefaultToolkit().getImage(imgs/潜艇7.png);image = new ImageIcon(image).getImage(); this.weight = image.getWidth(panel);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.n
17、extInt(panel.getHeight() + 180;while(y1+this.getHeight() = panel.getHeight()y1 = ry.nextInt(panel.getHeight() + 180;this.Y = y1;/计时器每隔一段时间产生鱼雷对象TimeManager2 tm2 = new TimeManager2(this,this.panel,this.ship,this.panel.getTorpedoArray();Thread t = new Thread(tm2);t.start();public void drawSubmarine(Gr
18、aphics2D g) g.drawImage(image,this.X, this.Y, panel); public void moveLeft()/System.out.println(潜水艇运动);this.X -= dx;/System.out.println(this.X);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(thi
19、s.m = 0)this.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
20、 setX(int x) 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 h
21、eight) this.height = height;3.3潜艇爆炸的实现/* * 潜艇被击中的爆炸效果,通过图片显示 */public class Hit implements Runnable private MyPanel panel; /主面板private Image image; /图片private int liveTime = 500; /爆炸效果显示的时间默认为500毫秒private int beginX = 0; /位置 x yprivate int beginY = 0;private boolean isRunning = false; /游戏是否正在运行标志pub
22、lic 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();public void drawHitting(Graphics2D g)g.drawImage(this.image, this.beginX,this.beginY,this.panel);pub
23、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游戏说明/* *
24、*游戏规则的对话框,提示游戏规则信息 */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
25、 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.
26、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
27、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(游戏规则);g2.setFont(new Fo
28、nt(华文行楷,Font.BOLD,25);g2.setColor(Color.yellow);BasicGraphicsUtils.drawString(g2, title, 100, 90, 50);String context = 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(按空
29、格键扔炸弹);g2.setFont(new Font(华文行楷,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时间计时器3.5.1计时产生潜艇public class TimeManager implements Runn
30、able private WarShip ship;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
31、.subLock)tryMyPanel.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(E
32、xception e)e.printStackTrace();public int getSpeed() return speed;public void setSpeed(int speed) this.speed = speed;3.5.2计时产生鱼雷public class TimeManager2 implements Runnableprivate WarShip ship;private ArrayList torpedoArray ;private MyPanel panel;private Submarine sm;public TimeManager2(Submarine s
33、m,MyPanel panel,WarShip ship,ArrayList 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(MyPanel.subLock)/System.out.println
34、(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(tp);t.start();tryint time = r.nextInt(4000) + 2000;T
35、hread.sleep(time);catch(Exception e)e.printStackTrace();3.6 读取文件,记录分数public class InputDialog extends JDialog/* * */private static final long serialVersionUID = 1L;private JButton submit; /提交按钮private JLabel text1;private JLabel text2;private JLabel text3;private JTextField field;private MyPanel pan
36、el;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(恭喜您进入前十!);this.text1.setFont(new Font(楷体,Font.BOLD,15);this.text1.s
37、etHorizontalAlignment(JLabel.CENTER);this.text1.setVerticalAlignment(JLabel.BOTTOM);this.text1.setPreferredSize(new Dimension(300,90);this.text2 = new JLabel(请输入您的姓名:);this.text2.setFont(new Font(楷体,Font.BOLD,12);this.text2.setHorizontalAlignment(JLabel.CENTER);this.text2.setPreferredSize(new Dimens
38、ion(130,20);this.text3 = new JLabel(总分:+ this.panel.getScore();this.text3.setFont(new Font(楷体,Font.BOLD,12);this.text3.setHorizontalAlignment(JLabel.CENTER);this.text3.setPreferredSize(new Dimension(300,30);this.field = new JTextField();this.field.setFont(new Font(楷体,Font.BOLD,15);this.field.setHori
39、zontalAlignment(JLabel.CENTER);this.field.setPreferredSize(new Dimension(70,20);this.submit.setBackground(Color.orange);this.submit.setHorizontalAlignment(JLabel.CENTER);this.submit.setPreferredSize(new Dimension(80,30);this.submit.setForeground(new Color(61,145,64);this.submit.setFont(new Font( ,0,
40、20);this.centerPanel = new JPanel(new FlowLayout(5);this.centerPanel.setPreferredSize(new Dimension(200,50);this.centerPanel.add(this.text2);this.centerPanel.add(this.field);this.northPanel = new JPanel(new GridLayout(2,1);this.northPanel.setPreferredSize(new Dimension(300,90);this.northPanel.add(th
41、is.text1);this.northPanel.add(this.text3);JLabel southHelp1 = new JLabel();southHelp1.setPreferredSize(new Dimension(100, 50);JLabel southHelp2 = new JLabel();southHelp2.setPreferredSize(new Dimension(100, 50);this.southPanel = new JPanel();this.southPanel.setPreferredSize(new Dimension(300,70);this
42、.southPanel.add(southHelp1);this.southPanel.add(this.submit);this.southPanel.add(southHelp2); JLabel east = new JLabel(); east.setPreferredSize(new Dimension(20, 30); JLabel west = new JLabel(); west.setPreferredSize(new Dimension(60, 20);Container c = this.getContentPane();c.add(this.northPanel,Bor
43、derLayout.NORTH);c.add(this.centerPanel,BorderLayout.CENTER);c.add(this.southPanel,BorderLayout.SOUTH);c.add(east,BorderLayout.EAST);c.add(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 arg0)/得到用户信息String name = InputDialog.this.field.getText().trim();String score = Integer.toString(InputDialog.this.panel.getScore();String pass = In
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
评论
0/150
提交评论