package test6;
import java.io.*;
import java.util.*;
class Recorder
{
private static int enNum=20;
private static int myLife=3;
private static int allShot=0;
static FileWriter fw=null;
static BufferedWriter bw=null;
static FileReader fr=null;
static BufferedReader br=null;
private static Vector<Enemy> ets=new Vector<Enemy>();
//保存敌人的坐标,方向 数量
public static void keepRecAndEnemy()
{
try {
fw=new FileWriter("d:\\javatank.txt");
bw=new BufferedWriter(fw);
bw.write(allShot+"\r\n");
for(int i=0;i<ets.size();i++)
{
Enemy et=ets.get(i);
if(et.isLive)
{
String record=et.x+" "+et.y+" "+et.direct;
bw.write(record+"\r\n");
}
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
bw.close();
fw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
//从文件中读取记录
public static void getRecording()
{
try {
fr=new FileReader("d:\\javatank.txt");
br=new BufferedReader(fr);
String n=br.readLine();
allShot=Integer.parseInt(n);
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}finally{
try {
br.close();
fr.close();
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
//玩家击毁敌人坦克数量保存到文件中
public static void keepRecording()
{
try {
fw=new FileWriter("d:\\javatank.txt");
bw=new BufferedWriter(fw);
bw.write(allShot+"\r\n");
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}finally{
try {
bw.close();
fw.close();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
public static int getEnNum() {
return enNum;
}
public static void setEnNum(int enNum) {
Recorder.enNum = enNum;
}
public static int getMyLife() {
return myLife;
}
public static void setMyLife(int myLife) {
Recorder.myLife = myLife;
}
public static void reduceEnemy()
{
enNum--;
}
public static int getAllShot() {
return allShot;
}
public static void setAllShot(int allShot) {
Recorder.allShot = allShot;
}
public static void addallShot()
{
allShot++;
}
public static Vector<Enemy> getEts() {
return ets;
}
public static void setEts(Vector<Enemy> ets1) {
System.out.print("whita ");
ets = ets1;
}
}
//炸弹类
class Bomb
{
int x,y;
int life=6;
boolean isLive=true;
public Bomb(int x,int y)
{
this.x=x;
this.y=y;
}
public void lifeDown()
{
if(life>0)
{
life--;
}else
{
this.isLive=false;
}
}
}
//定义坦克的类
class Tank
{
int x=0;
int y=0;
int direct=0;
int color;
int speed=1;
boolean isLive=true;
public int getColor() {
return color;
}
public void setColor(int color) {
this.color = color;
}
public int getDirect() {
return direct;
}
public void setDirect(int direct) {
this.direct = direct;
}
public void moveUp()
{
y=y-speed;
}
public void moveRight()
{
x=x+speed;
}
public void moveDown()
{
y=y+speed;
}
public void moveLeft()
{
x=x-speed;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public Tank(int x,int y){
this.x=x;
this.y=y;
}
}
//定义我的坦克,继承坦克类
class Mytank extends Tank
{
Vector<Shot> ss=new Vector<Shot>();
Shot s=null;
public Mytank(int x,int y)
{
super(x, y);
this.x=x;
this.y=y;
}
public void shotEnemy()
{
switch(this.direct)
{
case 0:
s=new Shot(x+10,y,0);
ss.add(s);
break;
case 1:
s=new Shot(x+30,y+10,1);
ss.add(s);
break;
case 2:
s=new Shot(x+10,y+30,2);
ss.add(s);
break;
case 3:
s=new Shot(x,y+10,3);
ss.add(s);
break;
}
Thread t=new Thread(s);
t.start();
}
}
//敌人的坦克
class Enemy extends Tank implements Runnable
{
int times=0;
Vector<Shot> ss=new Vector<Shot>();
//判断是否重叠
Vector<Enemy> ets=new Vector<Enemy>();
public void setEts(Vector<Enemy> ets)
{
this.ets=ets;
}
//判断重叠的函数
public boolean isTouchOther()
{
switch(this.direct)
{
case 0://我的(敌人)坦克方向
for(int i=0;i<ets.size();i++)
{
Enemy et=ets.get(i);
if(this!=et)
{
if(et.direct==0||et.direct==2)//敌人的坦克的方向
{
if(this.x>et.x&&this.x<et.x+20&&this.y>et.y&&this.y<et.y+30)
{
return true;
}
if(this.x+20>et.x&&this.x+20<et.x+20&&this.y>et.y&&this.y<et.y+30)
{
return true;
}
}
if(et.direct==1||et.direct==3)
{
if(this.x>et.x&&this.x<et.x+30&&this.y>et.y&&this.y<et.y+20)
{
return true;
}
if(this.x+20>et.x&&this.x+20<et.x+30&&this.y>et.y&&this.y<et.y+20)
{
return true;
}
}
}
}
break;
case 1:
for(int i=0;i<ets.size();i++)
{
Enemy et=ets.get(i);
if(this!=et)
{
if(et.direct==0||et.direct==2)//敌人的坦克的方向
{
if(this.x+30>et.x&&this.x+30<et.x+20&&this.y>et.y&&this.y<et.y+30)
{
return true;
}
if(this.x+30>et.x&&this.x+30<et.x+20&&this.y+20>et.y&&this.y+20<et.y+30)
{
return true;
}
}
if(et.direct==1||et.direct==3)
{
if(this.x+30>et.x&&this.x+30<et.x+20&&this.y>et.y&&this.y<et.y+20)
{
return true;
}
if(this.x+30>et.x&&this.x+30<et.x+30&&this.y+20>et.y&&this.y+20<et.y+20)
{
return true;
}
}
}
}
break;
case 2:
for(int i=0;i<ets.size();i++)
{
Enemy et=ets.get(i);
if(this!=et)
{
if(et.direct==0||et.direct==2)//敌人的坦克的方向
{
if(this.x>et.x&&this.x<et.x+20&&this.y+30>et.y&&this.y+30<et.y+30)
{
return true;
}
if(this.x+20>et.x&&this.x+20<et.x+20&&this.y+30>et.y&&this.y+30<et.y+30)
{
return true;
}
}
if(et.direct==1||et.direct==3)
{
if(this.x>et.x&&this.x<et.x+30&&this.y+30>et.y&&this.y+30<et.y+20)
{
return true;
}
if(this.x+20>et.x&&this.x+20<et.x+30&&this.y+30>et.y&&this.y+30<et.y+20)
{
return true;
}
}
}
}
break;
case 3:
for(int i=0;i<ets.size();i++)
{
Enemy et=ets.get(i);
if(this!=et)
{
if(et.direct==0||et.direct==2)//敌人的坦克的方向
{
if(this.x>et.x&&this.x<et.x+20&&this.y>et.y&&this.y<et.y+30)
{
return true;
}
if(this.x>et.x&&this.x<et.x+20&&this.y+20>et.y&&this.y+20<et.y+30)
{
return true;
}
}
if(et.direct==1||et.direct==3)
{
if(this.x>et.x&&this.x<et.x+20&&this.y>et.y&&this.y<et.y+20)
{
return true;
}
if(this.x>et.x&&this.x<et.x+30&&this.y+20>et.y&&this.y+20<et.y+20)
{
return true;
}
}
}
}
break;
}
return false;
}
public Enemy(int x,int y)
{
super(x,y);
this.x=x;
this.y=y;
}
public void run() {
while (true)
{
switch(direct)
{
case 0:
for(int i=0;i<30;i++)
{
if(y>0&&!this.isTouchOther())
{
y-=speed;
}
try{
Thread.sleep(50);
}catch(Exception e)
{
e.printStackTrace();
}
}
break;
case 1:
for(int i=0;i<30;i++)
{
if(x<400&&!this.isTouchOther()){
x+=speed;
}
try{
Thread.sleep(50);
}catch(Exception e)
{
e.printStackTrace();
}
}
break;
case 2:
for(int i=0;i<30;i++)
{
if(y<300&&!this.isTouchOther()){
y+=speed;
}
try{
Thread.sleep(50);
}catch(Exception e)
{
e.printStackTrace();
}
}
break;
case 3:
for(int i=0;i<30;i++)
{
if(x>0&&!this.isTouchOther())
{
x-=speed;
}
try{
Thread.sleep(50);
}catch(Exception e)
{
e.printStackTrace();
}
}
break;
}
this.direct=(int)(Math.random()*4);
if(this.isLive==false)
{
break;
}
this.times++;
if(times%2==0)
{
if(isLive)
{
//判断是否需要加入子弹
if(ss.size()<5)
{
Shot s=null;
//没有子弹,,添加子弹
switch(direct)
{
case 0:
s=new Shot(x+10,y,0);
ss.add(s);
break;
case 1:
s=new Shot(x+30,y+10,1);
ss.add(s);
break;
case 2:
s=new Shot(x+10,y+30,2);
ss.add(s);
break;
case 3:
s=new Shot(x,y+10,3);
ss.add(s);
break;
}
Thread t=new Thread(s);
t.start();
}
}
}
}
}
}
//定义子弹类
class Shot implements Runnable
{
int x;
int y;
int direct;
int speed=1;
boolean isLive=true;
public Shot(int x,int y,int direct)
{
this.x=x;
this.y=y;
this.direct=direct;
}
//子弹的 线程
public void run() {
while(true)
{
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
switch (direct)
{
case 0:
y=y-speed;
break;
case 1:
x=x+speed;
break;
case 2:
y=y+speed;
break;
case 3:
x=x-speed;
break;
}
if(x<0||x>400||y<0||y>300)
{
this.isLive=false;
break;
}
}
}
}
上面是全部代码了,运行的时候按exit的时候就会报错, Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
里面是报这句有空指针的:Recorder.setEts(mp.ets);