java 坦克大战 素材_Java坦克大战(四)

本文介绍了一个使用Java实现的坦克大战游戏,包括播放声音的类AePlayWave以及记录和恢复游戏状态的Recorder类。Recorder类用于保存和读取游戏进度,如敌人坦克的数量、位置和方向。此外,还涉及到游戏元素如坦克、子弹和炸弹的类定义及其行为逻辑。
摘要由CSDN通过智能技术生成

packagecom.fanghua6;importjava.io.BufferedReader;importjava.io.BufferedWriter;importjava.io.File;importjava.io.FileReader;importjava.io.FileWriter;importjava.io.IOException;importjava.util.Vector;importjavax.sound.sampled.AudioFormat;importjavax.sound.sampled.AudioInputStream;importjavax.sound.sampled.AudioSystem;importjavax.sound.sampled.DataLine;importjavax.sound.sampled.SourceDataLine;//播放声音的类

class AePlayWave extendsThread {privateString filename;publicAePlayWave(String wavfile) {

filename=wavfile;

}public voidrun() {

File soundFile= newFile(filename);

AudioInputStream audioInputStream= null;try{

audioInputStream=AudioSystem.getAudioInputStream(soundFile);

}catch(Exception e1) {

e1.printStackTrace();return;

}

AudioFormat format=audioInputStream.getFormat();

SourceDataLine auline= null;

DataLine.Info info= new DataLine.Info(SourceDataLine.class, format);try{

auline=(SourceDataLine) AudioSystem.getLine(info);

auline.open(format);

}catch(Exception e) {

e.printStackTrace();return;

}

auline.start();int nBytesRead = 0;//这是缓冲

byte[] abData = new byte[512];try{while (nBytesRead != -1) {

nBytesRead= audioInputStream.read(abData, 0, abData.length);if (nBytesRead >= 0)

auline.write(abData,0, nBytesRead);

}

}catch(IOException e) {

e.printStackTrace();return;

}finally{

auline.drain();

auline.close();

}

}

}//继续上一局的记记录点的类

classNode {intx;inty;intdirect;public Node(int x, int y, intdirect) {this.x =x;this.y =y;this.direct =direct;

}

}//记录类,同时也可以保存玩家的设置(一般都是事先写在缓存里面的)

classRecorder {//记录每关有多少敌人

private static int enNum = 20;//设置我有多少可以用的坦克

private static int myLife = 3;//记录一共消灭多少敌人

private static int allEnNum = 0;//从文件中恢复记录点

static Vector nodes = new Vector();private static FileWriter fw = null;private static BufferedWriter bw = null;private static FileReader fr = null;private static BufferedReader br = null;private Vector ets = new Vector();//完成读取的函数(记录点和敌人的数量)

public VectorgetNodesAndEnNums() {try{

fr= new FileReader("E:/Tanke.txt");

br= newBufferedReader(fr);

String n= "";

n= br.readLine();//设置先读第一行

allEnNum =Integer.parseInt(n);//接着读

while ((n = br.readLine()) != null) {

String[] xyz= n.split(" ");//在知道只有三条数据的情况下,用了这个方法。否则要用for循环

Node node = new Node(Integer.parseInt(xyz[0]),

Integer.parseInt(xyz[1]), Integer.parseInt(xyz[2]));

nodes.add(node);

}

}catch(Exception e) {//TODO Auto-generated catch block

e.printStackTrace();

}finally{try{

br.close();

fr.close();

}catch(Exception e) {//TODO Auto-generated catch block

e.printStackTrace();

}

}returnnodes;

}public VectorgetEts() {returnets;

}public void setEts(Vectorets) {this.ets =ets;

}//保存击毁敌人的数量和击毁敌人坦克的坐标、方向

public voidkeepRecAndEnemyTank() {try{

fw= new FileWriter("E:/Tanke.txt");

bw= newBufferedWriter(fw);

bw.write(allEnNum+ "\r\n");//保存当前的敌人坦克的坐标和方向

for (int i = 0; i < ets.size(); i++) {//取出第一个坦克

EnemyTank et =ets.get(i);if(et.isLive) {//活的就保存

String recode = et.x + " " + et.y + " " +et.direct;//写入

bw.write(recode + "\r\n");

}

}

}catch(IOException e) {//TODO Auto-generated catch block

e.printStackTrace();

}finally{try{

bw.close();

}catch(IOException e) {//TODO Auto-generated catch block

e.printStackTrace();

}try{

fw.close();

}catch(IOException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

}

}//从文件中读取,记录

public static voidgetRecoring() {try{

fr= new FileReader("E:/Tanke.txt");

br= newBufferedReader(fr);

String n=br.readLine();

allEnNum=Integer.parseInt(n);

}catch(Exception e) {//TODO Auto-generated catch block

e.printStackTrace();

}finally{try{

br.close();

fr.close();

}catch(Exception e) {//TODO Auto-generated catch block

e.printStackTrace();

}

}

}//把玩家击毁敌人坦克数量保存到文件中

public static voidkeepRecording() {try{

fw= new FileWriter("E:/Tanke.txt");

bw= newBufferedWriter(fw);

bw.write(allEnNum+ "\r\n");

}catch(IOException e) {//TODO Auto-generated catch block

e.printStackTrace();

}finally{try{

bw.close();

}catch(IOException e) {//TODO Auto-generated catch block

e.printStackTrace();

}try{

fw.close();

}catch(IOException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

}

}public static intgetAllEnNum() {returnallEnNum;

}public static void setAllEnNum(intallEnNum) {

Recorder.allEnNum=allEnNum;

}public static intgetEnNum() {returnenNum;

}public static void setEnNum(intenNum) {

Recorder.enNum=enNum;

}public static intgetMyLife() {returnmyLife;

}public void setMyLife(intmyLife) {

Recorder.myLife=myLife;

}//减少敌人数

public static voidreduceEnNum() {

enNum--;

}//消灭敌人

public static voidaddEnNumRec() {

allEnNum++;

}

}//炸弹类(没必要定义为线程,因为它不会移动,没有坐标改变)

classBomb {//定义炸弹的坐标

intx, y;int life = 9;//炸弹的生命(三张图片)//可以看出isLive很有用,它可以决定类(或者对象)要不要展现在面板上

boolean isLive = true;public Bomb(int x, inty) {this.x =x;this.y =y;

}//炸弹减少生命值

public voidlifeDown() {if (life > 0) {

life--;

}else{this.isLive = false;

}

}

}//子弹类

class Shot implementsRunnable {intx;inty;intdirect;//设置子弹的消亡(默认活着的)

boolean isLive = true;//speed要给个初始值1,之前给0,按J键,子弹没有动

int speed = 1;public Shot(int x, int y, intdirect) {super();this.x =x;this.y =y;this.direct =direct;

}

@Overridepublic voidrun() {//TODO Auto-generated method stub

while (true) {//设置子弹休息50毫秒

try{

Thread.sleep(50);

}catch(InterruptedException e) {//TODO Auto-generated catch block

e.printStackTrace();

}switch(direct) {case 0:

y-=speed;break;case 1:

x+=speed;break;case 2:

y+=speed;break;case 3:

x-=speed;break;

}//测试用:System.out.println("子弹坐标x=" + x + "y=" + y);//子弹什么时候死亡//判断该子弹是否碰到边缘

if (x < 0 || x > 400 || y < 0 || y > 300) {this.isLive = false;break;

}

}

}

}//坦克类

classTank1_2 {int x = 0;int y = 0;boolean isLive = true;//坦克方向:0表示上,1表示右,2表示下,3表示左

int direct = 0;int speed = 1;//坦克的颜色

intcolor;public intgetColor() {returncolor;

}public void setColor(intcolor) {this.color =color;

}public intgetSpeed() {returnspeed;

}public void setSpeed(intspeed) {this.speed =speed;

}public intgetDirect() {returndirect;

}public void setDirect(intdirect) {this.direct =direct;

}public intgetX() {returnx;

}public void setX(intx) {this.x =x;

}public intgetY() {returny;

}public void setY(inty) {this.y =y;

}//构造函数

public Tank1_2(int x, inty) {this.x =x;this.y =y;

}

}//敌人的坦克(做成线程,会移动)

class EnemyTank extends Tank1_2 implementsRunnable {int times = 0;//定义一个向量,可以访问MyPanel上所有敌人的坦克

Vector ets = new Vector();//定义向量,可以存放敌人的子弹

Vector ss = new Vector();//敌人添加子弹,应该刚刚创建坦克和敌人子弹死亡后

public EnemyTank(int x, inty) {super(x, y);

}//得到MyPaneld的敌人坦克向量

public void setEts(Vectorvv) {//此时拥有一种能力,可以拿到yPan el上所有敌人的坦克

this.ets =vv;

}//判断是否碰到别的敌人的坦克

public booleanisTouchOtherEnemy() {boolean b = false;switch (this.direct) {case 0://我的坦克向上

for (int i = 0; i < ets.size(); i++) {//取出第一个坦克(很有可能是自己哦)

EnemyTank et =ets.get(i);//如果不是自己

if (et != this) {//如果敌人的方向是向下或者向下

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++) {

EnemyTank et=ets.get(i);if (et != this) {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 + 30

&& 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++) {

EnemyTank et=ets.get(i);if (et != this) {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++) {

EnemyTank et=ets.get(i);if (et != this) {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 + 30

&& 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;

}returnb;

}

@Overridepublic voidrun() {//TODO Auto-generated method stub

while (true) {try{//设置坦克休息一会

Thread.sleep(50);

}catch(InterruptedException e) {//TODO Auto-generated catch block

e.printStackTrace();

}switch (this.direct) {case 0://说明坦克正在向上运动(继续往上走,符合实际)//y -= speed;设置坦克平滑移动的效果

for (int i = 0; i < 30; i++) {if (y > 0 && !this.isTouchOtherEnemy()) {

y-=speed;

}try{

Thread.sleep(50);

}catch(InterruptedException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

}break;case 1:for (int i = 0; i < 30; i++) {//这里注意坐标起点问题不是(400x300)

if (x < 400 && !this.isTouchOtherEnemy()) {

x+=speed;

}try{

Thread.sleep(50);

}catch(InterruptedException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

}break;case 2:for (int i = 0; i < 30; i++) {if (y < 300 && !this.isTouchOtherEnemy()) {

y+=speed;

}try{

Thread.sleep(50);

}catch(InterruptedException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

}break;case 3:for (int i = 0; i < 30; i++) {if (x > 0 && !this.isTouchOtherEnemy()) {

x-=speed;

}try{

Thread.sleep(50);

}catch(InterruptedException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

}break;

}this.times++;//设置3秒发一颗子弹

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 = newThread(s);

t.start();

}

}

}//让坦克随机产生一个新的方向

this.direct = (int) (Math.random() * 4);//判断敌人坦克是否死亡了(双等号)

if (this.isLive == false) {//让坦克死亡,后退出线程

return;

}

}

}

}//我的坦克

class Hero1_2 extendsTank1_2 {//多个子弹,用向量创建

Vector ss = new Vector();//子弹

Shot s = null;public Hero1_2(int x, inty) {super(x, y);

}//坦克开火

public voidshotEnemy() {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 = newThread(s);

t.start();

}public voidmoveUp() {

y-=speed;

}public voidmoveRight() {

x+=speed;

}public voidmoveDown() {

y+=speed;

}public voidmoveLeft() {

x-=speed;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值