package tanke.game;
import javax.swing.*;
import java.util.Vector;
public class enemytank extends tank implements Runnable {//敌人坦克
Vector shots = new Vector<>();//在敌人坦克类,使用vector保存多个shot
//增加成员,enemytank可以得到敌人坦克的vector
Vector enemytanks = new Vector<>();
boolean islive = true;
public enemytank(int x, int y) {
super(x, y);
}
public Vector getEnemytanks() {
return enemytanks;
}
//提供方法可以将mypanel里的属性enemytank成员设置到我们的enemytank里的属性
public void setEnemytanks(Vector enemytanks) {
this.enemytanks = enemytanks;
}
//编写方法,判断当前这个坦克是否和enemeytank中的其他坦克重叠
public boolean istouchenemytank() {
//判断当前敌方坦克的方向
switch (this.getDirect()) {
case 0://上
for(int i=0;i= 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.getY() + 40
&&this.getY()<=enemytank.getX()+60
&&this.getY()>=enemytank.getY()
) {
return true;
}
}
//敌方坦克是右/左
//敌方x范围x,x+60
//敌方y范围有,y+40
if (enemytank.getDirect() == 1 || enemytank.getDirect() == 3) {
//当前坦克左上角坐标我方x,我方y。
if (this.getX() >= enemytank.getX()
&& this.getX() <= enemytank.getX() + 60
&& this.getY() >= enemytank.getY()
&& this.getY() >= enemytank.getY() + 40) {
return true;
}
//当前坦克右上角坐标是我方x+40,y。
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= 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() + 60
&&this.getY()+40>=enemytank.getY()) {
return true;
}
}
//敌方坦克是右/左
//敌方x范围x,x+60
//敌方y范围有,y+40
if (enemytank.getDirect() == 1 || enemytank.getDirect() == 3) {
//当前坦克右上角坐标我方x+60,我方y。
if (this.getX() +60>= enemytank.getX()
&& this.getX()+60 <= enemytank.getX() + 60
&& this.getY() >= enemytank.getY()
&& this.getY() >= enemytank.getY() + 40) {
return true;
}
//当前坦克右下角坐标是我方x+60,y+40。
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= 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() + 60
&&this.getY()+60>=enemytank.getY()) {
return true;
}
}
//敌方坦克是右/左
//敌方x范围x,x+60
//敌方y范围有,y+40
if (enemytank.getDirect() == 1 || enemytank.getDirect() == 3) {
//当前坦克左下角坐标我方x,我方y+60。
if (this.getX() >= enemytank.getX()
&& this.getX() <= enemytank.getX() + 60
&& this.getY()+60 >= enemytank.getY()
&& this.getY() +60>= enemytank.getY() + 40) {
return true;
}
//当前坦克右下角坐标是我方x+60,y+40。
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= 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() + 60
&&this.getY()+40>=enemytank.getY()) {
return true;
}
}
//敌方坦克是右/左
//敌方x范围x,x+60
//敌方y范围有,y+40
if (enemytank.getDirect() == 1 || enemytank.getDirect() == 3) {
//当前坦克右上角坐标我方x,我方y。
if (this.getX() >= enemytank.getX()
&& this.getX() <= enemytank.getX() + 60
&& this.getY() >= enemytank.getY()
&& this.getY() >= enemytank.getY() + 40) {
return true;
}
//当前坦克右下角坐标是我方x,y+40。
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;
}
@Override
public void run() {
while (true) {
//我们判断如果shots size()=0,创建一颗子弹放入列
if (islive && shots.size() < 1) {//shots集合启动
shot s = null;
switch (getDirect()) {//判断坦克的方向,创建对应子弹
case 0:
s = new shot(getX() + 20, getY(), 0);
break;
case 1:
s = new shot(getX() + 60, getY() + 20, 1);
break;
case 2:
s = new shot(getX() + 20, getY() + 60, 2);
break;
case 3:
s = new shot(getX(), getY() + 20, 3);
break;
}
shots.add(s);
new Thread(s).start();
}
//根据坦克的方向继续推动
switch (getDirect()) {
case 0:
//让坦克保持一个方向走30步
for (int i = 0; i < 30; i++) {
if (getY() >= 0&&!istouchenemytank()) {//不撞墙并且不发生碰撞
moveup();
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
break;
case 1:
for (int i = 0; i < 30; i++) {
if (getX() + 60 < 1000&&!istouchenemytank()) {
moveright();
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
break;
case 2:
for (int i = 0; i < 30; i++) {
if (getY() + 60 < 750&&!istouchenemytank()) {
movedown();
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
break;
case 3:
for (int i = 0; i < 30; i++) {
if (getX() > 0&&!istouchenemytank()) {
moveleft();
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
break;
}
setDirect((int) (Math.random() * 4));//随机改变坦克方向0-3
//写并发程序一定要考虑清楚线程什么时候结束
if (!islive) {
break;//islive为假,退出线程
}
}
}
}
package tanke.game;
import java.util.Vector;
public class mytanke extends tank{
//d定义一个shot对象,表示射击行为
shot shot=null;
//可以发射多颗子弹
Vector shots=new Vector<>();
public mytanke(int x, int y) {
super(x, y);
}
//射击
public void shotenemytank(){//创建shot对象,根据当前mytank的位置和方向创建shot
//控制面板上最多五颗子弹
if(shots.size()==5){
return;
}
switch (getDirect()){
case 0://向上
shot =new shot(getX()+20,getY(),0);
break;
case 1://向右
shot =new shot(getX()+60,getY()+20,1);
break;
case 2://向下
shot =new shot(getX()+20,getY()+60,2);
break;
case 3://向左
shot =new shot(getX(),getY()+20,3);
break;
}
//把新创建的shot放入到shots
shots.add(shot);
new Thread(shot).start();
}
}
package tanke.game;
import java.io.*;
import java.util.Vector;
//记录相关的信息和文件交互
public class recorder {
//定义变量,记录我方击中敌方坦克数
private static int allenemytanknum = 0;
//定义io对象,准备写数据到文件中
private static FileWriter fw = null;
private static BufferedWriter bw = null;
private static BufferedReader br=null;
//把记录文件保存在src下
private static String recordfile = "src\\myrecord.txt";
private static Vector enemytanks = null;
//定义一个node的vector,用于保存敌人的信息node
private static Vector nodes=new Vector<>();
public static void setEnemytanks(Vector enemytanks) {
recorder.enemytanks = enemytanks;
}
//返回文件的目录
public static String getRecordfile() {
return recordfile;
}
//增加一个方法,用于读取recordfile,恢复相关信息
//该方法在继续上局游戏的时候调用
public static Vector getnodesandEnemytankrec(){
try {
br=new BufferedReader(new FileReader(recordfile));
allenemytanknum=Integer.parseInt(br.readLine());//显示行数
String line="";
while ((line= br.readLine())!=null){
String[] xyd = line.split(" ");
node node = new node(Integer.parseInt(xyd[0]), Integer.parseInt(xyd[1]),
Integer.parseInt(xyd[1]));
nodes.add(node);
}
} catch(IOException e) {
e.printStackTrace();
} finally {
if(br!=null){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return nodes;
}
public static void keeprecord() {//保存敌人坦克的坐标方向
try {
bw = new BufferedWriter(new FileWriter(recordfile));
bw.write(allenemytanknum + "\r\n");
//遍历敌方坦克的vector,然后根据情况保存即可
//oop,定义一个属性然后通过方法得到敌人坦克的vector
for (int i = 0; i < enemytanks.size(); i++) {
enemytank enemytank = enemytanks.get(i);
if (enemytank.islive) {//建议判断
//保存enemy信息
String record = enemytank.getX() + " " + enemytank.getY()
+ "" + enemytank.getDirect();
//写入到文件
bw.write(record + "\r\n");
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static int getAllenemytanknum() {
return allenemytanknum;
}
public static void setAllenemytanknum(int allenemytanknum) {
recorder.allenemytanknum = allenemytanknum;
}
//当我方坦克击中敌方坦克,加坦克数量
public static void addallenemytanknum() {
recorder.allenemytanknum++;
}
}
package tanke.game;
public class bomb {//炸弹
int x,y;//炸弹的坐标
int life=9;//炸弹的生命周期
boolean islive=true;//是否还存活
public bomb(int x, int y) {
this.x = x;
this.y = y;
}
//减少生命值
public void lifedown(){//配合图片出现的爆炸效果
if(life>0){
life--;
}else{
islive=false;
}
}
}
package tanke.game;
public class shot implements Runnable{
int x;//子弹x的坐标
int y;//子弹y的坐标
int direct=0;//子弹方向
int speed=2;//子弹的速度
boolean islve=true;//子弹是否存活
//构造器
public shot(int x, int y, int direct) {
this.x = x;
this.y = y;
this.direct = direct;
}
@Override
public void run() {
while (true){
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}//根据方向改变x,y坐标
switch (direct){
case 0://上
y-=speed;
break;
case 1://右
x+=speed;
break;
case 2://下
y+=speed;
break;
case 3://左
x-=speed;
break;
}//测试,输出x,y坐标
System.out.println(x+"\t"+y);
//当子弹移动到面板的边界就应该销魂
if(!(x>=0&&x<=1000 &&y>=0&&y<=750&&islve)){
System.out.println("子线程退出");
islve=false;
break;
}
}
}
}
package tanke.game;
import com.sun.deploy.security.SelectableSecurityManager;
//
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.util.Vector;
//为了监听键盘事件,实现keylistener
//为了让panel不停的重绘子弹,需要将mypannel实现runnable,当做一个线程使用
public class mypanel extends JPanel implements KeyListener, Runnable {
mytanke mytanke = null;
//定义敌人坦克放入到vector集合中
Vector enemytanks = new Vector<>();
//定义一个存放node对象的vector恢复敌方坦克的坐标和方向
Vector nodes=new Vector<>();
//定义一个vector,用于存放炸弹,当子弹击中坦克,加入bomb对象到bombs
Vector bombs = new Vector<>();
//定义三张炸弹图片,用于显示炸弹效果
Image image1 = null;
Image image2 = null;
Image image3 = null;
int enemytanksize = 3;
public mypanel(String key) {//将mypanel对象的enemytanks设置给recorder的enemytank
//判断记录的文件是否存在,存在就正常执行。文件不存在提示只能开启游戏
File file=new File(recorder.getRecordfile());
if(file.exists()){
nodes=recorder.getnodesandEnemytankrec();}else {
System.out.println("文件不存在只能开启游戏");
key="1";
}
recorder.setEnemytanks(enemytanks);
mytanke = new mytanke(500, 100);
switch (key){
case "1"://开始游戏
for (int i = 0; i < enemytanksize; i++) {
enemytank enemytank = new enemytank(100 * (i + 1), 0);
//将enemytanks设置给enemeytank
enemytank.setEnemytanks(enemytanks);
enemytank.setSpeed(2);
enemytank.setDirect(2);//设置方向
//启动敌方的坦克线程,让他动起来
new Thread(enemytank).start();
//给敌方坦克加入一颗子弹
shot shot = new shot(enemytank.getX() + 20, enemytank.getY() + 60, enemytank.getDirect());
//加入敌方坦克vector保存多个shot
enemytank.shots.add(shot);
new Thread(shot).start();//启动坦克
enemytanks.add(enemytank);
}
break;
case "2"://继续上局游戏
for (int i = 0; i < nodes.size(); i++) {
node node=nodes.get(i);
enemytank enemytank = new enemytank(node.getX(), node.getY());
//将enemytanks设置给enemeytank
enemytank.setEnemytanks(enemytanks);
enemytank.setSpeed(2);
enemytank.setDirect(node.getDirect());//设置方向
//启动敌方的坦克线程,让他动起来
new Thread(enemytank).start();
//给敌方坦克加入一颗子弹
shot shot = new shot(enemytank.getX() + 20, enemytank.getY() + 60, enemytank.getDirect());
//加入敌方坦克vector保存多个shot
enemytank.shots.add(shot);
new Thread(shot).start();//启动坦克
enemytanks.add(enemytank);
}
break;
default:
System.out.println("你的输出有误");
}
//c初始化敌人坦克
//初始化图片对象
image1 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bomb_1.gif"));
image2 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bomb_2.gif"));
image3 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bomb_3.gif"));
mytanke.setSpeed(2);
//这里播放指定的音乐
new AePlayWave("src\\111.wav").start();
}
//编写方法显示我方击中敌方坦克的信息
public void showinfo(Graphics g) {
//画出总成绩
g.setColor(Color.BLACK);
Font font=new Font("宋体",Font.BOLD,25);
g.setFont(font);
g.drawString("你累计击毁敌方坦克",1020,30);
drawtank(1020,60,g,0,0);//画出敌方坦克
g.setColor(Color.BLACK);//重新设置为黑色,因为画坦克时用的青色
g.drawString(recorder.getAllenemytanknum()+"",1080,100);
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.fillRect(0, 0, 1000, 750);//填充矩形默认是黑色
showinfo(g);
//画出坦克-方封装方法
if (mytanke != null && mytanke.islive) {
drawtank(mytanke.getX(), mytanke.getY(), g, mytanke.getDirect(), 1);
}//画出mytank射出的子弹
// if(mytanke.shot!=null&&mytanke.shot.islve!=false){
g.fill3DRect(mytanke.shot.x,mytanke.shot.y,1,1,false);
// g.draw3DRect(mytanke.shot.x,mytanke.shot.y,2,2,false);
// }
//将坦克子弹遍历取出
for (int i = 0; i < mytanke.shots.size(); i++) {
shot shot = mytanke.shots.get(i);
if (shot != null && shot.islve != false) {
g.fill3DRect(shot.x, shot.y, 1, 1, false);
// g.draw3DRect(mytanke.shot.x,mytanke.shot.y,2,2,false);
} else {//如果该shot对象已经无效就从集合中拿掉
mytanke.shots.remove(shot);
}
}
//如果bombs集合中有对象就画出
for (int i = 0; i < bombs.size(); i++) {
//取出炸弹根据当前bomb对象的life值去画出对应的图片
bomb bomb = bombs.get(i);
if (bomb.life > 6) {
g.drawImage(image1, bomb.x, bomb.y, 60, 60, this);
} else if (bomb.life > 3) {
g.drawImage(image2, bomb.x, bomb.y, 60, 60, this);
} else {
g.drawImage(image3, bomb.x, bomb.y, 60, 60, this);
}
//让这个炸弹生命值减少
bomb.lifedown();
//如果bomb life为0,就从bombs集合删除
if (bomb.life == 0) {
bombs.remove(bomb);
}
}
//画出敌人坦克,遍历vector
for (int i = 0; i < enemytanks.size(); i++) {//取出坦克
enemytank enemytank = enemytanks.get(i);
//判断坦克是否存活
if (enemytank.islive) {//当敌方坦克是活的才画出坦克
drawtank(enemytank.getX(), enemytank.getY(),
g, enemytank.getDirect(), 0);
//画出敌方坦克所有子弹
for (int j = 0; j < enemytank.shots.size(); j++) {
//取出子弹
shot shot = enemytank.shots.get(j);
if (shot.islve) {//子弹存活
g.draw3DRect(shot.x, shot.y, 1, 1, false);
} else {
enemytank.shots.remove(shot);
}
}
}
}
}
//x坦克坐标左上角,g是画笔,direct坦克方向,type 坦克类型
public void drawtank(int x, int y, Graphics g, int direct, int type) {
switch (type) {
case 0://我们的坦克
g.setColor(Color.cyan);
break;
case 1://敌方的坦克
g.setColor(Color.yellow);
break;
}//接下来根据坦克方向绘制坦克
//direct表示方向(0;向上,1;向右 2;向下 3;向左
switch (direct) {
case 0://表示向上
g.fill3DRect(x, y, 10, 60, false);//画出坦克左边轮子
g.fill3DRect(x + 30, y, 10, 60, false);//画出坦克右边轮子
g.fill3DRect(x + 10, y + 10, 20, 40, false);//画出坦克盖子
g.fillOval(x + 10, y + 20, 20, 20);//画出圆
g.drawLine(x + 20, y + 30, x + 20, y);//画出炮筒直线
break;
case 1://向右
g.fill3DRect(x, y, 60, 10, false);//画出坦克左边轮子
g.fill3DRect(x, y + 30, 60, 10, false);//画出坦克右边轮子
g.fill3DRect(x + 10, y + 10, 40, 20, false);//画出坦克盖子
g.fillOval(x + 20, y + 10, 20, 20);//画出圆
g.drawLine(x + 30, y + 20, x + 60, y + 20);//画出炮筒直线
break;
case 2://向下
g.fill3DRect(x, y, 10, 60, false);//画出坦克左边轮子
g.fill3DRect(x + 30, y, 10, 60, false);//画出坦克右边轮子
g.fill3DRect(x + 10, y + 10, 20, 40, false);//画出坦克盖子
g.fillOval(x + 10, y + 20, 20, 20);//画出圆
g.drawLine(x + 20, y + 30, x + 20, y + 60);//画出炮筒直线
break;
case 3://向左
g.fill3DRect(x, y, 60, 10, false);//画出坦克左边轮子
g.fill3DRect(x, y + 30, 60, 10, false);//画出坦克右边轮子
g.fill3DRect(x + 10, y + 10, 40, 20, false);//画出坦克盖子
g.fillOval(x + 20, y + 10, 20, 20);//画出圆
g.drawLine(x + 30, y + 20, x, y + 20);//画出炮筒直线
break;
default:
System.out.println("暂时没有处理");
}
}
//如果坦克发射多颗子弹那么判断就需要取出子弹集合,与敌方坦克集合进行判断
//编写方法,判断我方的子弹是否击中敌人坦克
//s什么时候判断我方的子弹击中敌方坦克run方法
public void hitenemytank() {
//遍历我们的子弹
for (int j = 0; j < mytanke.shots.size(); j++) {
shot shot = mytanke.shots.get(j);
if (shot != null && shot.islve == true) {//我的子弹是否活着
//遍历敌人所有的坦克
for (int i = 0; i < enemytanks.size(); i++) {
enemytank enemytank = enemytanks.get(i);
hittank(mytanke.shot, enemytank);
}
}
}
}
public void hitmytank() {//编写方法,判断敌人是否击中我的坦克
//遍历敌方坦克
for (int i = 0; i < enemytanks.size(); i++) {
enemytank enemytank = enemytanks.get(i);
//遍历敌方坦克对象的所有子弹
for (int j = 0; j < enemytank.shots.size(); j++) {
//取出子弹
shot shot = enemytank.shots.get(j);
//判断shot是否击中我的坦克
if (mytanke.islive && shot.islve) {
hittank(shot, mytanke);
}
}
}
}
public void hittank(shot s, tank enemytank) {
//判断s击中坦克
switch (enemytank.getDirect()) {
case 0://坦克向上
case 2://坦克向下
if (s.x > enemytank.getX() && s.x < enemytank.getX() + 40 &&
s.y > enemytank.getY() && s.y < enemytank.getY() + 60) {
s.islve = false;
enemytank.islive = false;
//当我的子弹击中敌方坦克,将enemytank从vector拿掉
enemytanks.remove(enemytank);
//当击中坦克就加一个坦克数因为坦克有敌方和我方
//所以我们进行类型判断
if(enemytank instanceof enemytank){
recorder.addallenemytanknum();
}
//创建bomb对象,加入到bomb集合
bomb bomb = new bomb(enemytank.getX(), enemytank.getY());
bombs.add(bomb);
}
break;
case 1://坦克向右
case 3://坦克向左
if (s.x > enemytank.getX() && s.x < enemytank.getX() + 60
&& s.y > enemytank.getY() && s.y < enemytank.getY() + 40) {
s.islve = false;
enemytank.islive = false;//子弹击中后拿到该坦克
enemytanks.remove(enemytank);
if(enemytank instanceof enemytank){
recorder.addallenemytanknum();
}
bomb bomb = new bomb(enemytank.getX(), enemytank.getY());
bombs.add(bomb);
}
break;
}
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_W) {
mytanke.setDirect(0);
if (mytanke.getY() > 0) {
mytanke.moveup();
}
} else if (e.getKeyCode() == KeyEvent.VK_D) {
mytanke.setDirect(1);
if (mytanke.getX() + 60 < 1000) {
mytanke.moveright();
}
} else if (e.getKeyCode() == KeyEvent.VK_A) {
mytanke.setDirect(3);
if (mytanke.getX() > 0) {
mytanke.moveleft();
}
} else if (e.getKeyCode() == KeyEvent.VK_S) {
mytanke.setDirect(2);
if (mytanke.getY() + 60 < 750)
mytanke.movedown();
}
// if(e.getKeyCode()==KeyEvent.VK_J){//如果按下j子弹发射
// //判断当前坦克的子弹是否消亡,发射一颗子弹的情况
// if(mytanke.shot==null||!mytanke.shot.islve){
// mytanke.shotenemytank();}
// }
if (e.getKeyCode() == KeyEvent.VK_J) {
mytanke.shotenemytank();
}
this.repaint();//让画版重新绘制
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void run() {//每隔100毫秒,重绘区域,刷新绘图区域,子弹就移动
while (true) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
//判断是否击中了敌方坦克
// if(mytanke.shot!=null &&mytanke.shot.islve==true){//我的子弹是否活着
// //遍历敌人所有的坦克
// for(int i=0;i
package tanke.game;
import javax.swing.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Scanner;
public class tankgame extends JFrame {
mypanel mp=null;
static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
tankgame tankgame = new tankgame();
}
public tankgame(){
System.out.println("请输入选择 1:新游戏 2:继续上局");
String key=scanner.next();
mp= new mypanel(key);
Thread thread = new Thread(mp);
thread.start();
this.add(mp);//把面板(游戏的绘图区域)
this.setSize(1300,950);
this.addKeyListener(mp);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
//在jfram中增加相关关闭窗口的处理
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.out.println("监听到关闭窗口");
recorder.keeprecord();
System.exit(0);
}
});
}
}
package tanke.game;
import javax.sound.sampled.*;
import java.io.File;
import java.io.IOException;
/**
* @author 韩顺平
* @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();
}
}
}
package tanke.game;
//表示敌人坦克的信息
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;
}
}