/**
*TankGame4.0
* 功能:
* 1:画出坦克
* 2:上下左右移动
* 3:可以发射子弹
* 4.敌人被我击中时消失(并爆炸)
* 5.我被敌人击中时消失(并爆炸)
* 6.敌人坦克会相互避让
* 7.设置关卡
* 7.1做一个开始的panel
* 8.可以中途暂停
* 8.1当用户点击暂停时将子弹和坦克的速度置为零,并维持方向不变
* 9.可以记录玩家成绩
* 9.1用文件流
* 9.2单写一个类记录玩家的数据
* 9.3先保存击毁敌人坦克数量
* 9.4存盘退出
* 10.设置音乐背景
*/
package com.TankGame4;
import java.awt.event.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.imageio.ImageIO;
import javax.swing.*;
public class TankGame4 extends JFrame implements ActionListener{
static int XL=1100;
static int YL=740;
final static int GXL=840;
final static int GYL=660;
//定义一个开始的面板
startPanel sp=null;
//定义我的面板
MyPanel mp=null;
JMenuBar jmb=null;//做出菜单
JMenu jm1,jm2,jm3;
JMenuItem jmi1,jmi2,jmi3,jmi4;
JButton jb1,jb2,jb3,jb4,jb5;
JToolBar jtb;
public static void main(String[] args) {
// TODO Auto-generated method stub
TankGame4 tank1=new TankGame4();
}
//构造函数
public TankGame4()
{
//创建组件
jmb=new JMenuBar();
jm1=new JMenu("游戏(G)");//游戏菜单
jm1.setMnemonic('G');//游戏菜单的助记符是G
jmi1=new JMenuItem("新游戏(N)");//菜单项“新游戏”
jmi1.addActionListener(this);
jmi1.setActionCommand("newGame");
jmi2=new JMenuItem("继续游戏");
jmi2.addActionListener(this);
jmi2.setActionCommand("continue");
jmi3=new JMenuItem("退出(E)");
jmi3.addActionListener(this);
jmi3.setActionCommand("exit");
jmi4=new JMenuItem("存盘退出(R)");
jmi4.addActionListener(this);
jmi4.setActionCommand("saveandexit");
jtb=new JToolBar();//创建工具条
jb1=new JButton(new ImageIcon("images/新游戏.png"));
jb2=new JButton(new ImageIcon("images/存盘.png"));
jb3=new JButton(new ImageIcon("images/退出.png"));
jb4=new JButton(new ImageIcon("images/帮助.png"));
jb5=new JButton(new ImageIcon("images/继续.png"));
//为jb设置提示信息
jb1.setToolTipText("新游戏");
jb1.addActionListener(this);//注册监听
jb1.setActionCommand("newGame");//设置监听命令
//
jb5.setToolTipText("继续游戏");
jb5.addActionListener(this);//注册监听
jb5.setActionCommand("continue");//设置监听命令
//
jb3.setToolTipText("退出");
jb3.addActionListener(this);
jb3.setActionCommand("exit");
// //jb[3].setToolTipText("剪切");
jb2.setToolTipText("退出存盘");
jb2.addActionListener(this);
jb2.setActionCommand("saveandexit");
jb4.setToolTipText("帮助");
//添加组件
jm1.add(jmi1);
jm1.add(jmi2);
jm1.add(jmi3);
jm1.add(jmi4);
jmb.add(jm1);
jtb.add(jb1);
jtb.add(jb5);
jtb.add(jb2);
jtb.add(jb3);
jtb.add(jb4);
sp=new startPanel();
this.add(sp);
Thread t=new Thread(sp);
t.start();
//this.add(jtb,BorderLayout.SOUTH);
this.setJMenuBar(jmb);
this.setSize(XL,YL);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand().equals("newGame"))//点击菜单,布置战场
{
mp=new MyPanel("newGame");
this.remove(sp);//将开始面板删除
this.add(mp);
Thread t=new Thread(mp);
t.start();//启动mp线程
this.addKeyListener(mp);
this.setVisible(true);//刷新显示新面板
}else if(e.getActionCommand().equals("exit")){
//用户点击了退出系统
//保存击毁敌人坦克的数量
Recorder.keepRecord();
System.exit(0);
}else if(e.getActionCommand().equals("continue")){
//Recorder.readRecord();
//System.out.println("12345");
mp=new MyPanel("continue");
this.remove(sp);//将开始面板删除
this.add(mp);
Thread t=new Thread(mp);
t.start();//启动mp线程
this.addKeyListener(mp);
this.setVisible(true);//刷新显示新面板
}else if(e.getActionCommand().equals("saveandexit"))
{
Recorder rd=new Recorder();
rd.setEts(mp.ets);
rd.saveAndExit();
}
}
}
class Node
{
int x=0;
int y=0;
int direct=0;
public Node(int x,int y,int direct)
{
this.x=x;
this.y=y;
this.direct=direct;
}
}
class Recorder
{
//记录每关有多少敌人
private static int enSize=10;
private static int myLife=3;
private static int distroyEnemyNum=0;
private static BufferedWriter bw=null;
private static FileWriter fw=null;
private static BufferedReader br=null;
private static FileReader fr=null;
private static Vector<EnemyTanks> ets=null;
static Vector<Node> nodes=new Vector<Node>();
public Vector<EnemyTanks> getEts() {
return ets;
}
public void setEts(Vector<EnemyTanks> ets) {
this.ets = ets;
}
public void saveAndExit()
{
// File f2=new File("d:/TankGame4_saveAll.txt");
// if(!f2.exists())
// {
// try {
// f2.createNewFile();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }else{
// System.out.println("该文件已存在!");
// }
try {
fw=new FileWriter("d:/TankGame4_saveAll.txt");
bw=new BufferedWriter(fw);
bw.write(distroyEnemyNum+"\r\n");
for(int i=0;i<ets.size();i++)
{
EnemyTanks et=ets.get(i);
if(et.isAlive)
{
String info=et.x+" "+et.y+" "+et.direct;
//写入到文件
bw.write(info+"\r\n");
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
bw.close();
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.exit(0);
}
public Vector<Node> readRecord()//读取文件中的数据
{
try {
fr=new FileReader("d:/TankGame4_saveAll.txt");
br=new BufferedReader(fr);
String n="";
n=br.readLine();
distroyEnemyNum=Integer.parseInt(n);
while((n=br.readLine())!=null)
{
String []threePart=n.split(" ");
Node node=new Node(Integer.parseInt(threePart[0]),Integer.parseInt(threePart[1]),Integer.parseInt(threePart[2]));
nodes.add(node);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
br.close();
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return nodes;
}
public static void keepRecord()//保存数据到文件
{
File f2=new File("d:/TankGame4.txt");
if(!f2.exists())
{
try {
f2.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
//System.out.println("该文件已存在!");
}
try {
fw=new FileWriter("d:/TankGame4.txt");
bw=new BufferedWriter(fw);
bw.write(distroyEnemyNum+"\r\n");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
bw.close();
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static int getDistroyEnemyNum() {
return distroyEnemyNum;
}
public static void setDistroyEnemyNum(int distroyEnemyNum) {
Recorder.distroyEnemyNum = distroyEnemyNum;
}
public static int getEnSize() {
return enSize;
}
public static void setEnSize(int enSize) {
Recorder.enSize = enSize;
}
public static int getMyLife() {
return myLife;
}
public static void setMyLife(int myLife) {
Recorder.myLife = myLife;
}
public static void reduceEnsize()
{
enSize--;
}
public static void increaseDistroy()
{
distroyEnemyNum++;
}
public static void reduceMyLife()
{
myLife--;
}
}
//设置开始面板,起到提示作用
class startPanel extends JPanel implements Runnable
{
int times=0;
public void paint(Graphics g)
{
super.paint(g);
g.fillRect(0, 0, TankGame4.X