今天为大家分享潜艇大战的开发与制作。整个系统界面漂亮,有完整源码提供,希望大家可以喜欢。喜欢的帮忙点赞和关注。一起编程、一起进步
开发环境
开发语言为Java,开发环境Eclipse或者IDEA都可以。运行主程序,或者执行打开JAR文件即可以运行本程序。
系统框架
利用JDK自带的SWING框架开发,不需要安装第三方JAR包。纯窗体模式,直接运行Main文件即可以。同时带有详细得设计文档
主要功能
程序启动
1 运行Main.JAVA文件,启动游戏主程序
2 点击开始游戏按钮,正式开始游戏
操作方法
1 通过电脑键盘的上下、左右4个按钮来控制键盘的四个方向进行军舰移动
2 当水面上的潜艇移动的时候,选择合适的位置发射子弹
3 军舰发射的子弹击中潜艇的时候,获得相应的分数
4 每盘游戏有5条生命值和5关,即一定数量的子弹。当子弹发射完成的时候,游戏结束
5 系统保存系统的最高分数。并且显示在窗体右上角
实现效果
关键代码
package com.stj.views;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;
/*
* 游戏窗体
*/
public class MyFrame extends JFrame implements Observer
{
/**
*
*/
private static final long serialVersionUID = 1L;
private MyPanel panel; //游戏界面
private MainPanel mainpanel; //开始界面
private JMenuBar bar; //菜单栏
private JMenu menu1; //菜单1,2
private JMenu menu2;
protected boolean isStart;
protected boolean isExit;
private JMenuItem item1; //菜单项1,2,3,4,5
private JMenuItem item2;
private JMenuItem item3;
private JMenuItem item4;
private JMenuItem item5;
public MyFrame(Observable ob)
{
ob.addObserver(this);
Observable obs = ob;
this.panel = new MyPanel();
this.mainpanel = new MainPanel(obs);
this.isStart = mainpanel.getIsStart();
this.isExit = mainpanel.isExit();
}
public void showMyFrame()
{
Container c = this.getContentPane();
c.setLayout(new BorderLayout());
this.bar = new JMenuBar();
this.menu1 = new JMenu("菜单");
this.menu2 = new JMenu("帮助");
this.item1 = new JMenuItem("开始");
this.item2 = new JMenuItem("暂停");
this.item3 = new JMenuItem("退出");
this.item4 = new JMenuItem("关于我们");
this.item5 = new JMenuItem("游戏规则");
this.menu1.add(item1);
this.menu1.add(item2);
this.menu1.add(item3);
this.menu2.add(item4);
this.menu2.add(item5);
bar.add(menu1);
bar.add(menu2);
this.bar.setVisible(false);
this.setJMenuBar(this.bar);
item1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
MyFrame.this.panel.requestFocus();
MyFrame.this.panel.startGame();
}
}
);
item2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
MyFrame.this.panel.stopGame();
}
}
);
item3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
);
item4.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//MyFrame.this.panel.setFocusable(false);
MyFrame.this.panel.setSuspendFlag(true);
new InfoDialog(MyFrame.this,true,MyFrame.this.panel);
}
}
);
item5.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//MyFrame.this.panel.setFocusable(false);
MyFrame.this.panel.setSuspendFlag(true);
new HelpDialog(MyFrame.this,true,MyFrame.this.panel);
}
}
);
c.add(this.mainpanel,BorderLayout.CENTER);
this.setSize(645,511);
this.setVisible(true);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void changedPanel()
{
}
//游戏开始,切换到游戏主界面
public void update(Observable arg0, Object arg1)
{
MainPanel panel = (MainPanel) arg1;
//System.out.println("update" + panel.getIsStart());
if(panel.getIsStart())
{
//System.out.println("update" + panel.getIsStart());
this.getContentPane().remove(this.mainpanel);
//this.panel.setPreferredSize(new Dimension(645,511));
this.bar.setVisible(true);
this.getContentPane().add(this.panel, BorderLayout.CENTER);
this.repaint();
}
}
}