坦克大战游戏 所有代码,W向上运动S向下,A向左,D向右运动 J发射子弹。两个java文件,分别为MyTankGame3.java和Members.java文件,复制黏贴到myeclipse上就可以

坦克大战游戏 全部代码,W向上运动S向下,A向左,D向右运动 J发射子弹。。两个java文件,分别为MyTankGame3.java和Members.java文件,复制黏贴到myeclipse上就可以
/*
 * 功能:坦克大战游戏的4.0版本,
 * 1画出坦克
 * 2我的坦克可以上下左右移动
 * 3,可以发射子弹,子弹可以连发(最多连发5颗)
 * 4,当我的坦克击中敌人坦克时,敌人坦克就消失(爆炸的效果)
 * 5,我被击中后显示爆炸效果
 * 
 * 6,防止敌人坦克重叠运动
 * 6.1决定把判断是否碰撞的函数写到EnemyTank里面去
 * 7,可以分关
 * 7.1做一个开始的panel,它是一个空的
 * 7.2闪烁效果
 * 8可以暂停继续游戏
 * 8.1当用户点击暂停时,子弹的速度和坦克的速度设为0,并让坦克的方向不发生变化
 * 9可以记录玩家的成绩
 * 9.1 用文件流(单机版的游戏一般都是用文件流保存,大型网络游戏才会用数据库)
 * 9.2单写一个记录类,记录玩家及坦克的信息
 * 9.3 先完成保存共击毁了多少敌人坦克
 * 10java如何操作声音文件
 */
第一个JAVA文件:
package com.test1;

import javax.swing.*;
import java.awt.*;
import java.util.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class MyTankGame3 extends JFrame implements ActionListener {

	MyPanel mp = null;
	// 定义一个开始的面板
	MyStartPanel msp = null;
	// 做出我需要的菜单
	JMenuBar jmb = null;
	// 开始游戏
	JMenu jm1 = null;
	JMenuItem jmi1 = null;
	JMenuItem jmi2 = null;
	JMenuItem jmi3 = null;
	JMenuItem jmi4 = null;

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		MyTankGame3 demo = new MyTankGame3();
	}

	public MyTankGame3() {
		// // mp=new MyPanel();
		// //
		// // this.add(mp);
		// Thread t=new Thread(mp);
		// t.start();
		// this.addKeyListener(mp);
		//
		// this.setLocation(350,100);
		// 创建菜单及菜单选项
		jmb = new JMenuBar();
		jm1 = new JMenu("游戏(G)");
		jm1.setMnemonic('G');
		jmi1 = new JMenuItem("开始新游戏(N)");
		jmi2 = new JMenuItem("退出游戏(E)");
		jmi3 = new JMenuItem("存盘退出游戏(C)");

		jmi4 = new JMenuItem("继续上局游戏(S)");
		jmi4.addActionListener(this);
		jmi4.setActionCommand("conGame");
		jmi3.addActionListener(this);
		jmi3.setActionCommand("save");
		jmi2.addActionListener(this);
		jmi2.setActionCommand("exit");
		jmi2.setMnemonic('E');
		jm1.add(jmi1);
		jm1.add(jmi2);
		jm1.add(jmi3);
		jm1.add(jmi4);
		jmb.add(jm1);
		// 对jmi1响应
		jmi1.addActionListener(this);
		jmi1.setActionCommand("new game");

		msp = new MyStartPanel();
		Thread t = new Thread(msp);
		t.start();
		this.setJMenuBar(jmb);
		this.add(msp);
		this.setTitle("霍磊 我的坦克");
		this.setSize(600, 500);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		// 对用户不同的点击做出处理
		if (e.getActionCommand().equals("new game")) {
			// 创建战场面板
			mp = new MyPanel("newGame");
			Thread t = new Thread(mp);
			t.start();
			// 先删除旧的面板
			this.remove(msp);
			this.add(mp);

			this.addKeyListener(mp);
			this.setVisible(true);

		} else if (e.getActionCommand().equals("exit")) {
			// 用户点击了退出系统菜单
			// 保存击毁敌人数量
			Recorder.keepRecording();
			System.exit(0);
		}
		// 对存盘退出做处理
		else if (e.getActionCommand().equals("save")) {
			Recorder rd = new Recorder();
			rd.setEts(mp.ets);
			// 保存击毁敌人的数量和敌人的坐标
			rd.keepRecAndEnemyTank();
			System.exit(0);
		} else if (e.getActionCommand().equals("conGame")) {
			// 创建战场面板
			mp = new MyPanel("Con");
			// mp.flag="Con";
			// mp.nodes=new Recorder().getNodesAndEnNums();
			Thread t = new Thread(mp);
			t.start();
			// 先删除旧的面板
			this.remove(msp);
			this.add(mp);

			this.addKeyListener(mp);
			this.setVisible(true);

		}
	}

}

class MyStartPanel extends JPanel implements Runnable {
	int times = 0;

	public void paint(Graphics g) {
		super.paint(g);
		g.fillRect(0, 0, 400, 300);
		// 提示信息
		if (times % 2 == 0) {
			// 开关信息的字体
			g.setColor(Color.white);
			Font myFont = new Font("华文新魏", Font.BOLD, 30);
			g.setFont(myFont);
			g.drawString("Stage:  1", 150, 150);
		}
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		while (true) {
			try {
				Thread.sleep(500);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			times++;
			// 重画
			this.repaint();
		}
	}
}

// 我的面板
class MyPanel extends JPanel implements KeyListener, Runnable {
	// 定义一个我的坦克,把创建的工作都放在构造函数里做
	Hero hero = null;

	// 判断是续上局还是新游戏
	// String flag="newGame";
	// 定义敌人坦克组
	Vector<EnemyTank> ets = new Vector<EnemyTank>();
	Vector<Node> nodes = new Vector<Node>();
	Vector<Bomb> bombs = new Vector<Bomb>();

	int enSize = 10;
	Image image1 = null;
	Image image2 = null;
	Image image3 = null;
	Image image4 = null;

	// 重新paint
	public void paint(Graphics g) {

		super.paint(g);
		g.setColor(Color.black);
		g.fillRect(0, 0, 400, 300);
		// 画出提示信息

		this.showInfo(g);

		if (hero.isLive) {
			this.drawTank(hero.getX(), hero.getY(), g, hero.getDirect(), 1);
		}
		// 从ss中取出每颗子弹
		for (int i = 0; i < hero.ss.size(); i++) {
			Shot myShot = hero.ss.get(i);
			if (myShot != null && myShot.isLive == true) {

				g.draw3DRect(myShot.x, myShot.y, 1, 1, false);
			}
			if (myShot.isLive == false) {
				// 从ss中删除该子弹
				hero.ss.remove(myShot);
			}
		}
		// 画出炸弹

		for (int i = 0; i < bombs.size(); i++) {
			// 取出炸弹
			Bomb b = bombs.get(i);
			// System.out.println(b.x+"  "+b.y);
			// System.out.println(b.life);
			if (b.life > 7) {
				g.drawImage(image1, b.x, b.y, 30, 30, this);
			} else if (b.life > 6) {
				g.drawImage(image2, b.x, b.y, 30, 30, this);
			} else if (b.life > 3) {
				g.drawImage(image3, b.x, b.y, 30, 30, this);
			} else {
				g.drawImage(image4, b.x, b.y, 30, 30, this);
			}
			// 让b的生命值减小
			b.lifeDown();
			// 如何炸弹生命值为0,就是把该炸弹重bombs向量去
			if (b.life == 0) {
				bombs.remove(b);
			}
		}
		// 画出敌人坦克
		for (int i = 0; i < ets.size(); i++) {
			EnemyTank et = ets.get(i);
			if (et.isLive) {
				this.drawTank(et.getX(), et.getY(), g, et.getDirect(), 0);
				// 再画出敌人坦克
				for (int j = 0; j < et.ss.size(); j++) {
					// 取出子弹
					Shot enemyShot = et.ss.get(j);
					if (enemyShot.isLive) {
						g.draw3DRect(enemyShot.x, enemyShot.y, 1, 1, false);
					} else {
						// 如果敌人坦克死亡就从vector去掉
						et.ss.remove(enemyShot);
					}
				}
			}
		}

	}

	public MyPanel(String flag) {
		// 回复记录
		Recorder.getRecording();
		hero = new Hero(100, 100);

		// 初始化敌人的坦克
		if (flag.equals("newGame")) {

			for (int i = 0; i < enSize; i++) {
				// 创建一辆敌人坦克对象
				EnemyTank et = new EnemyTank((i + 1) * 30, 0);
				et.setColor(0);
				et.setDirect(2);
				// 将MyPanel上敌人坦克向量交给该敌人坦克
				et.setEts(ets);

				// 启动敌人坦克
				Thread t = new Thread(et);
				t.start();
				// 给敌人坦克添加一个子弹
				Shot s = new Shot(et.x + 10, et.y + 30, 2);
				// 加入给敌人坦克
				et.ss.add(s);
				Thread t2 = new Thread(s);
				t2.start();

				// 加入
				ets.add(et);
			}
		} else {
			nodes = new Recorder().getNodesAndEnNums();
			for (int i = 0; i < nodes.size(); i++) { // 创建一辆敌人坦克对象
				Node node = nodes.get(i);
				EnemyTank et = new EnemyTank(node.x, node.y);
				et.setColor(0);
				et.setDirect(node.direct);
				// 将MyPanel上敌人坦克向量交给该敌人坦克
				et.setEts(ets);

				// 启动敌人坦克
				Thread t = new Thread(et);
				t.start();
				// 给敌人坦克添加一个子弹
				Shot s = new Shot(et.x + 10, et.y + 30, 2);
				// 加入给敌人坦克
				et.ss.add(s);
				Thread t2 = new Thread(s);
				t2.start();

				// 加入
				ets.add(et);
			}

		}

		// 初始化图片
		image1 = Toolkit.getDefaultToolkit().getImage(
				Panel.class.getResource("/bomb_1.jpg"));
		image2 = Toolkit.getDefaultToolkit().getImage(
				Panel.class.getResource("/bomb_2.jpg"));
		image3 = Toolkit.getDefaultToolkit().getImage(
				Panel.class.getResource("/bomb_3.jpg"));
		image4 = Toolkit.getDefaultToolkit().getImage(
				Panel.class.getResource("/bomb_4.jpg"));
		Bomb b1 = new Bomb(1, 1);
		bombs.add(b1);
	}

	// 画出提示信息
	public void showInfo(Graphics g) {
		// 画出提示信息坦克(该坦克不参与战斗)
		this.drawTank(80, 330, g, 0, 0);
		g.setColor(Color.black);
		g.drawString(Recorder.getEnNum() + " ", 110, 350);
		this.drawTank(130, 330, g, 0, 1);
		g.setColor(Color.black);
		g.drawString(Recorder.getMyLife() + " ", 160, 350);

		// 画出玩家的总成绩
		g.setColor(Color.black);
		Font f = new Font("宋体", Font.BOLD, 20);
		g.setFont(f);
		g.drawString("老人家总成绩", 420, 30);

		this.drawTank(420, 60, g, 0, 0);

		g.setColor(Color.black);
		g.drawString(Recorder.getAllEnNum() + " ", 460, 80);

	}

	// 判断敌人的子弹是否击中我方坦克
	public void hitMe() {
		// 取出每一个敌人的坦克
		for (int i = 0; i < this.ets.size(); i++) {
			// 取出坦克
			EnemyTank et = ets.get(i);
			// 取出每一刻子弹
			for (int j = 0; j < et.ss.size(); j++) {
				Shot enemyShot = et.ss.get(j);
				if (hero.isLive) {
					if (this.hitTank(enemyShot, hero)) {
					}
				}
			}
		}

	}

	// 判断我的子弹是否击中敌人的坦克
	public void hitEnemyTank() {
		// 判断是否击中敌人坦克
		for (int i = 0; i < hero.ss.size(); i++) {
			// 取出子弹
			Shot myShot = hero.ss.get(i);
			// 判断子弹是否有效
			if (myShot.isLive) {
				// 取出每个坦克与之判断
				for (int j = 0; j < ets.size(); j++) {
					EnemyTank et = ets.get(j);
					if (et.isLive) {
						if (this.hitTank(myShot, et)) {

							// 减少敌人数量
							Recorder.reduceEnNum();
							// 增加我的记录
							Recorder.addEnNumRec();
						}
					}
				}
			}

		}

	}

	// 写一个函数判断子弹是否集中坦克
	public boolean hitTank(Shot s, Tank et) {
		boolean b2 = false;
		// 判断坦克方向
		switch (et.direct) {
		case 0:
		case 2:
			if (s.x > et.x && s.x < et.x + 20 && s.y > et.y && s.y < et.y + 30) {
				// 击中
				// 子弹死亡
				// 敌人坦克死亡
				s.isLive = false;
				et.isLive = false;
				b2 = true;
				// 创建一颗炸弹,放入vector
				Bomb b = new Bomb(et.x, et.y);
				bombs.add(b);
			}
			break;
		case 1:
		case 3:
			if (s.x > et.x && s.x < et.x + 30 && s.y > et.y && s.y < et.y + 20) {
				// 击中
				// 子弹死亡
				// 敌人坦克死亡
				s.isLive = false;
				et.isLive = false;
				b2 = true;
				// 创建一颗炸弹,放入vector
				Bomb b = new Bomb(et.x, et.y);
				bombs.add(b);
			}
			break;
		}
		return b2;
	}

	// 画出坦克的函数
	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;
		}
		switch (direct) {
		// 向上
		case 0:// 炮筒向上
			// 画出我的坦克
			// g.setColor(Color.cyan);
			g.fill3DRect(x, y, 5, 30, false);
			g.fill3DRect(x + 15, y, 5, 30, false);
			// g.setColor(Color.red);
			g.fill3DRect(x + 5, y + 5, 10, 20, false);
			g.fillOval(x + 5, y + 10, 10, 10);

			g.setColor(Color.PINK);
			g.drawLine(x, y + 5, x + 5, y + 5);
			g.drawLine(x, y + 10, x + 5, y + 10);
			g.drawLine(x, y + 15, x + 5, y + 15);
			g.drawLine(x, y + 20, x + 5, y + 20);
			g.drawLine(x, y + 25, x + 5, y + 25);

			g.drawLine(x + 15, y + 5, x + 20, y + 5);
			g.drawLine(x + 15, y + 10, x + 20, y + 10);
			g.drawLine(x + 15, y + 15, x + 20, y + 15);
			g.drawLine(x + 15, y + 20, x + 20, y + 20);
			g.drawLine(x + 15, y + 25, x + 20, y + 25);

			g.setColor(Color.white);
			g.drawLine(x + 10, y + 15, x + 10, y - 8);
			break;
		case 1:// 炮筒向右
				// g.setColor(Color.cyan);
			g.fill3DRect(x, y, 30, 5, false);
			g.fill3DRect(x, y + 15, 30, 5, false);
			// g.setColor(Color.red);
			g.fill3DRect(x + 5, y + 5, 20, 10, false);
			g.fillOval(x + 10, y + 5, 10, 10);

			g.setColor(Color.PINK);
			g.drawLine(x + 5, y, x + 5, y + 5);
			g.drawLine(x + 10, y, x + 10, y + 5);
			g.drawLine(x + 15, y, x + 15, y + 5);
			g.drawLine(x + 20, y, x + 20, y + 5);
			g.drawLine(x + 25, y, x + 25, y + 5);

			g.drawLine(x + 5, y + 15, x + 5, y + 19);
			g.drawLine(x + 10, y + 15, x + 10, y + 19);
			g.drawLine(x + 15, y + 15, x + 15, y + 19);
			g.drawLine(x + 20, y + 15, x + 20, y + 19);
			g.drawLine(x + 25, y + 15, x + 25, y + 19);

			g.setColor(Color.white);
			g.drawLine(x + 15, y + 10, x + 30, y + 10);
			break;
		case 2:
			// g.setColor(Color.cyan);
			g.fill3DRect(x, y, 5, 30, false);
			g.fill3DRect(x + 15, y, 5, 30, false);
			// g.setColor(Color.red);
			g.fill3DRect(x + 5, y + 5, 10, 20, false);
			g.fillOval(x + 5, y + 10, 10, 10);

			g.setColor(Color.PINK);
			g.drawLine(x, y + 5, x + 5, y + 5);
			g.drawLine(x, y + 10, x + 5, y + 10);
			g.drawLine(x, y + 15, x + 5, y + 15);
			g.drawLine(x, y + 20, x + 5, y + 20);
			g.drawLine(x, y + 25, x + 5, y + 25);

			g.drawLine(x + 15, y + 5, x + 20, y + 5);
			g.drawLine(x + 15, y + 10, x + 20, y + 10);
			g.drawLine(x + 15, y + 15, x + 20, y + 15);
			g.drawLine(x + 15, y + 20, x + 20, y + 20);
			g.drawLine(x + 15, y + 25, x + 20, y + 25);

			g.setColor(Color.white);
			g.drawLine(x + 10, y + 15, x + 10, y + 30);
			break;
		case 3:
			// g.setColor(Color.cyan);
			g.fill3DRect(x, y, 30, 5, false);
			g.fill3DRect(x, y + 15, 30, 5, false);
			// g.setColor(Color.red);
			g.fill3DRect(x + 5, y + 5, 20, 10, false);
			g.fillOval(x + 10, y + 5, 10, 10);

			g.setColor(Color.PINK);
			g.drawLine(x + 5, y, x + 5, y + 5);
			g.drawLine(x + 10, y, x + 10, y + 5);
			g.drawLine(x + 15, y, x + 15, y + 5);
			g.drawLine(x + 20, y, x + 20, y + 5);
			g.drawLine(x + 25, y, x + 25, y + 5);

			g.drawLine(x + 5, y + 15, x + 5, y + 19);
			g.drawLine(x + 10, y + 15, x + 10, y + 19);
			g.drawLine(x + 15, y + 15, x + 15, y + 19);
			g.drawLine(x + 20, y + 15, x + 20, y + 19);
			g.drawLine(x + 25, y + 15, x + 25, y + 19);

			g.setColor(Color.white);
			g.drawLine(x + 15, y + 10, x, y + 10);
			break;
		}
	}

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub

	}

	// 键按下处理 a向左 w向上 s向下 d向右
	@Override
	public void keyPressed(KeyEvent e) {
		// TODO Auto-generated method stub
		if (e.getKeyCode() == KeyEvent.VK_W) {
			// 设置我的坦克的方向
			this.hero.setDirect(0);
			hero.moveUp();
		} else if (e.getKeyCode() == KeyEvent.VK_D) {
			hero.setDirect(1);
			hero.moveRight();
		} else if (e.getKeyCode() == KeyEvent.VK_S) {
			hero.setDirect(2);
			hero.moveDown();
		} else if (e.getKeyCode() == KeyEvent.VK_A) {
			hero.setDirect(3);
			hero.moveLeft();
		}
		if (e.getKeyCode() == KeyEvent.VK_J) {// 开火
			if (this.hero.ss.size() <= 15) {
				this.hero.shotEnemy();
			}
		}
		this.repaint();
	}

	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		while (true) {
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			this.hitEnemyTank();
			this.hitMe();
			this.repaint();
		}
	}
}

第二个JAVA文件:

package com.test1;

import java.util.Vector;
import java.io.*;

class Node {
	int x;
	int y;
	int direct;

	public Node(int x, int y, int direct) {
		this.x = x;
		this.y = y;
		this.direct = direct;
	}
}

// 记录类,同时也可以保存玩家的设置
class Recorder {
	// 记录每一关有多少敌人
	private static int enNum = 10;
	// 设置我有多少可以用的人
	private static int myLife = 3;
	// 记录总共消灭多少敌人
	private static int allEnNum = 0;
	// 从文件中恢复记录点
	static Vector<Node> nodes = new Vector<Node>();
	private static FileWriter fw = null;
	private static FileReader fr = null;
	private static BufferedReader br = null;
	private static BufferedWriter bw = null;

	public Vector<EnemyTank> getEts() {
		return ets;
	}

	public void setEts(Vector<EnemyTank> ets1) {
		this.ets = ets1;
	}

	private Vector<EnemyTank> ets = new Vector<EnemyTank>();

	// 完成读取任务
	public Vector<Node> getNodesAndEnNums() {

		try {
			fr = new FileReader("d:/myRecording.txt");
			br = new BufferedReader(fr);
			String n = "";
			// 先读取第一行S
			n = br.readLine();
			allEnNum = Integer.parseInt(n);
			while ((n = br.readLine()) != null) {
				String[] xyz = n.split(" ");

				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 (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return nodes;
	}

	// 保存击毁敌人坦克数量和坦克坐标,方向
	public void keepRecAndEnemyTank() {
		// 创建文件流
		try {
			fw = new FileWriter("d:/myRecording.txt");
			bw = new BufferedWriter(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();
				fw.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
	}

	// 从文件中读取记录
	public static void getRecording() {
		try {
			fr = new FileReader("d:/myRecording.txt");
			br = new BufferedReader(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 (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

	// 把玩家击毁敌人坦克保存到文件中
	public static void keepRecording() {
		// 创建文件流
		try {
			fw = new FileWriter("d:/myRecording.txt");
			bw = new BufferedWriter(fw);

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

		} catch (IOException 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 getAllEnNum() {
		return allEnNum;
	}

	public static void setAllEnNum(int allEnNum) {
		Recorder.allEnNum = allEnNum;
	}

	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 reduceEnNum() {
		enNum--;
	}

	public static void addEnNumRec() {
		allEnNum++;
	}

}

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 {
			this.isLive = false;
		}
	}

}

// 子弹类
class Shot implements Runnable {
	int x;
	int y;
	int direct;
	int speed = 6;
	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) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			switch (direct) {
			case 0:
				y = 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;
			}
		}

	}
}

// 坦克类
class Tank {
	// 表示坦克的横坐标,和纵坐标
	int x = 0;
	int y = 0;
	boolean isLive = true;
	// 坦克的颜色
	int color;

	public int getColor() {
		return color;
	}

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

	// 坦克的速度
	int speed = 2;

	public int getSpeed() {
		return speed;
	}

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

	// 坦克方向
	// 0表示向上 1表示向右 2表示向下 3表示向左
	int direct = 0;

	public int getDirect() {
		return direct;
	}

	public void setDirect(int direct) {
		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 Tank(int x, int y) {
		this.x = x;
		this.y = y;
	}

}

// 敌人的坦克
class EnemyTank extends Tank implements Runnable {

	int times = 0;
	// 定义一个向量,可以访问到Mypanel上的所有敌人
	Vector<EnemyTank> ets = new Vector<EnemyTank>();

	// 定义一个向量,可以存放敌人子弹
	Vector<Shot> ss = new Vector<Shot>();

	// 敌人添加子弹应该在刚刚创建坦克和坦克子弹死亡后
	public EnemyTank(int x, int y) {
		super(x, y);
		// TODO Auto-generated constructor stub
	}

	// 得到MypANEL的敌人坦克向量
	public void setEts(Vector<EnemyTank> vv) {
		this.ets = vv;
	}

	// 判断是否碰到敌人坦克
	public boolean isTouchOtherEnemy() {
		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 + 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 + 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 >= 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 + 20 >= et.y
								&& this.y + 20 <= 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 b;
	}

	public void run() {

		while (true) {

			switch (this.direct) {
			case 0:
				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++) {
					if (x < 350 && !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 < 230 && !this.isTouchOtherEnemy()) {
						y += speed;
					}// else System.out.println("x的值是"+y);
					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;
			}
			++times;
			if (times % 2 == 0) {

				if (isLive) {
					if (ss.size() < 9) {
						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();
					}
				}
			}

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

			this.direct = (int) (Math.random() * 4);
			// this.direct=Hero.this.direct;
			// 判断敌人坦克是否死亡
			if (this.isLive == false) {// 让坦克退出线程
				break;
			}
			// 判断子弹是否没有

		}
	}

}

// 我的坦克
class Hero extends Tank {
	Shot s = null;
	Vector<Shot> ss = new Vector<Shot>();

	// 父类的构造函数来初始化子类的变量
	public Hero(int x, int y) {
		super(x, y);

	}

	// 开火
	public void shotEnemy() {
		switch (this.direct) {
		case 0:
			s = new Shot(x + 10, y - 8, 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();

	}

	public void moveUp() {
		y -= 6 * speed;
	}

	public void moveRight() {
		x += 6 * speed;
	}

	public void moveDown() {
		y += 6 * speed;
	}

	public void moveLeft() {
		x -= 6 * speed;
	}

}