代码链接:https://download.csdn.net/download/qq_52354698/86405067?spm=1001.2014.3001.5501
//编写方法判断当前的这个敌人坦克是否和enemyTanks中的其他坦克发生重叠或者碰撞
public boolean isTouchEnemyTank() {
switch (this.getDirect()) {
case 0:
for (int i = 0; i < enemyTanks.size(); i++) {
//从vector中依次取出敌人坦克
EnemyTank enemyTank = enemyTanks.get(i);
if (enemyTank != this) {
if (enemyTank.getDirect() == 0 || enemyTank.getDirect() == 2) {
if (this.getX() >= enemyTank.getX() && this.getX() <= enemyTank.getX() + 40 && this.getY() >= enemyTank.getY() && this.getY() <= enemyTank.getY() + 60) {
return true;
}
if (this.getX() + 40 >= enemyTank.getX() && this.getX() + 40 <= enemyTank.getX() + 40 && this.getY() >= enemyTank.getY() && this.getY() <= enemyTank.getY() + 60) {
return true;
}
}
if (enemyTank.getDirect() == 1 || enemyTank.getDirect() == 3) {
if (this.getX() >= enemyTank.getX() && this.getX() <= enemyTank.getX() + 60 && this.getY() >= enemyTank.getY() && this.getY() <= enemyTank.getY() + 40) {
return true;
}
if (this.getX() + 40 >= enemyTank.getX() && this.getX() + 40 <= enemyTank.getX() + 60 && this.getY() >= enemyTank.getY() && this.getY() <= enemyTank.getY() + 40) {
return true;
}
}
}
}
break;
case 1:
for (int i = 0; i < enemyTanks.size(); i++) {
//从vector中依次取出敌人坦克
EnemyTank enemyTank = enemyTanks.get(i);
if (enemyTank != this) {
if (enemyTank.getDirect() == 0 || enemyTank.getDirect() == 2) {
if (this.getX() + 60 >= enemyTank.getX() && this.getX() + 60 <= enemyTank.getX() + 40 && this.getY() >= enemyTank.getY() && this.getY() <= enemyTank.getY() + 60) {
return true;
}
if (this.getX() + 60 >= enemyTank.getX() && this.getX() + 60 <= enemyTank.getX() + 40 && this.getY() + 40 >= enemyTank.getY() && this.getY() + 40 <= enemyTank.getY() + 60) {
return true;
}
}
if (enemyTank.getDirect() == 1 || enemyTank.getDirect() == 3) {
if (this.getX() + 60 >= enemyTank.getX() && this.getX() + 60 <= enemyTank.getX() + 60 && this.getY() >= enemyTank.getY() && this.getY() <= enemyTank.getY() + 40) {
return true;
}
if (this.getX() + 60 >= enemyTank.getX() && this.getX() + 60 <= enemyTank.getX() + 60 && this.getY() + 40 >= enemyTank.getY() && this.getY() + 40 <= enemyTank.getY() + 40) {
return true;
}
}
}
}
break;
case 2:
for (int i = 0; i < enemyTanks.size(); i++) {
//从vector中依次取出敌人坦克
EnemyTank enemyTank = enemyTanks.get(i);
if (enemyTank != this) {
if (enemyTank.getDirect() == 0 || enemyTank.getDirect() == 2) {
if (this.getX() >= enemyTank.getX() && this.getX() <= enemyTank.getX() + 40 && this.getY() + 60 >= enemyTank.getY() && this.getY() + 60 <= enemyTank.getY() + 60) {
return true;
}
if (this.getX() + 40 >= enemyTank.getX() && this.getX() + 40 <= enemyTank.getX() + 40 && this.getY() + 60 >= enemyTank.getY() && this.getY() + 60 <= enemyTank.getY() + 60) {
return true;
}
}
if (enemyTank.getDirect() == 1 || enemyTank.getDirect() == 3) {
if (this.getX() >= enemyTank.getX() && this.getX() <= enemyTank.getX() + 60 && this.getY() + 60 >= enemyTank.getY() && this.getY() + 60 <= enemyTank.getY() + 40) {
return true;
}
if (this.getX() + 40 >= enemyTank.getX() && this.getX() + 40 <= enemyTank.getX() + 60 && this.getY() + 60 >= enemyTank.getY() && this.getY() + 60 <= enemyTank.getY() + 40) {
return true;
}
}
}
}
break;
case 3:
for (int i = 0; i < enemyTanks.size(); i++) {
//从vector中依次取出敌人坦克
EnemyTank enemyTank = enemyTanks.get(i);
if (enemyTank != this) {
if (enemyTank.getDirect() == 0 || enemyTank.getDirect() == 2) {
if (this.getX() >= enemyTank.getX() && this.getX() <= enemyTank.getX() + 40 && this.getY() >= enemyTank.getY() && this.getY() <= enemyTank.getY() + 60) {
return true;
}
if (this.getX() >= enemyTank.getX() && this.getX() <= enemyTank.getX() + 40 && this.getY() + 40 >= enemyTank.getY() && this.getY() + 40 <= enemyTank.getY() + 60) {
return true;
}
}
if (enemyTank.getDirect() == 1 || enemyTank.getDirect() == 3) {
if (this.getX() >= enemyTank.getX() && this.getX() <= enemyTank.getX() + 60 && this.getY() >= enemyTank.getY() && this.getY() <= enemyTank.getY() + 40) {
return true;
}
if (this.getX() >= enemyTank.getX() && this.getX() <= enemyTank.getX() + 60 && this.getY() + 40 >= enemyTank.getY() && this.getY() + 40 <= enemyTank.getY() + 40) {
return true;
}
}
}
}
break;
}
return false;
}
记录我方击毁敌方坦克总数
package com.qdu.tankgame;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
/**
* @author dell
* @version 1.0
* 记录相关的信息,与文件交互
*/
public class Recorder {
private static int allEnemyTanksNum = 0;
private static FileWriter fileWriter = null;
private static BufferedWriter bufferedWriter = null;
private static String recordFile = "e:\\myRecord.txt";
//当游戏退出时,将数据保存到文件中
public static void keepRecord() throws IOException {
bufferedWriter = new BufferedWriter(new FileWriter(recordFile));
bufferedWriter.write(allEnemyTanksNum);
bufferedWriter.close();
}
public static int getAllEnemyTanksNum() {
return allEnemyTanksNum;
}
public static void setAllEnemyTanksNum(int allEnemyTanksNum) {
Recorder.allEnemyTanksNum = allEnemyTanksNum;
}
public static void addAllEnemyTankNum() {
Recorder.allEnemyTanksNum++;
}
}
当窗口关闭时,获取到剩余的地方坦克的信息,将其存储到文件中
for (int i = 0; i < enemyTanks.size(); i++) {
EnemyTank enemyTank = enemyTanks.get(i);
if (enemyTank.isLive) {
String record = enemyTank.getX() + " " + enemyTank.getY() + " " + enemyTank.getDirect();
bufferedWriter.write(record + "\r\n");
}
}
在执行程序的时候,提供用户选择:1.开始新游戏,2.继续上局游戏
如继续上局游戏,则从文件中读取上次游戏结束,保存到文件中的敌方坦克的坐标,然后将其绘制在地图上,以到达继续游戏的效果
package com.qdu.tankgame;
/**
* @author dell
* @version 1.0
* 一个Node对象就表示一个敌人坦克的信息
*/
public class Node {
private int x;
private int y;
private int direct;
public Node(int x, int y, int direct) {
this.x = x;
this.y = y;
this.direct = direct;
}
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 int getDirect() {
return direct;
}
public void setDirect(int direct) {
this.direct = direct;
}
}
使用一个播放音乐的类
package com.qdu.tankgame;
import javax.sound.sampled.*;
import java.io.File;
import java.io.IOException;
/**
* @author dell
* @version 1.0
*/
public class AePlayWave extends Thread {
private String filename;
public AePlayWave(String wavfile) { //构造器 , 指定文件
filename = wavfile;
}
public void run() {
File soundFile = new File(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();
}
}
}
new AePlayWave("src\\111.wav").start();