Java 坦克大战单机版 源代码

尚学堂版本的修改版


玩法:

方向键:控制走动

Ctrl:控制发弹

A:超级子弹

X:八方向发弹

=:添加敌方坦克

F2:重新开始

其他功能:

在头上实时显示血条

吃到血块时能补血

自动积分

敌方坦克过少时自动添加

收获:

系统复习了J2SE的基本上所有内容

对软件工程的理解进一步加深

对面向对象的思想及其优点进一步加深

熟悉了eclipse的使用,包括打包发布以及Doc的生成


源代码:

//TankClient.java

package edu.wkx;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 坦克客户端主类
 * @author Java_Bean 王凯旋
 *
 */

@SuppressWarnings("serial")
public class TankClient extends Frame {

	static final int GAME_WIDTH = 800;
	static final int GAME_HEIGHT = 600;
	private int x = 50;
	private int y = 50;
	private int refreshTime = 16;
	private int bloodTime = 500;
	private int bloodStep = 0;
	private int maxBloods = 10;
	private int minTanks = 3;
	private int maxTanks = 20;
	private Image bufImg = null;
	private Color bgColor = Color.GREEN;
	private boolean hasGetdist = false;
	private int disX = 0;
	private int disY = 0;
	private int enemyNum = 10;
	private static Random rand = new Random();
	private boolean start = false;
	private int score = 0;
	private int killPoint = 5;

	private List explodes = new ArrayList();
	private List tanks = new ArrayList();
	private List missiles = new ArrayList();
	private List walls = new ArrayList();
	private List bloods = new ArrayList();

	/**
	 * 启动窗口
	 */
	
	public void launchFrame() {
		this.setTitle("TankWar");
		this.setLocation(300, 300);
		this.setSize(GAME_WIDTH, GAME_HEIGHT);
		this.setResizable(false);
		this.addWindowListener(new WindowAdapter() {

			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}

		});
		this.addMouseListener(new MouseAdapter() {

			public void mouseReleased(MouseEvent e) {
				hasGetdist = false;
			}

		});
		this.addMouseMotionListener(new MouseAdapter() {

			public void mouseDragged(MouseEvent e) {

				if (!hasGetdist) {
					disX = e.getX();
					disY = e.getY();
					hasGetdist = true;
				}
				setLocation(e.getXOnScreen() - disX, e.getYOnScreen() - disY);
			}

		});
		this.setBackground(bgColor);
		this.addKeyListener(new KeyMonitor());
		this.setVisible(true);
		gameInit();
	}
	
	private void gameInit() {
		tanks.add(new Tank(x, y, true, this));
		addTank(10);
		walls.add(new Wall(300, 400, 300, 30));
		walls.add(new Wall(400, 300, 10, 200));
		score = 0;
		start = true;
	}

	// 重写paint方法

	private void addBlood() {
		bloodStep++;
		if (bloodStep >= bloodTime && bloods.size() < maxBloods) {
			bloodStep = 0;
			Blood blood = new Blood(x, y);
			blood.setX(rand.nextInt(GAME_WIDTH - blood.getSize()));
			blood.setY(rand.nextInt(GAME_HEIGHT - blood.getSize() - 25) + 25);
			for (int i = 0; i < tanks.size(); i++)
				if (blood.getRect().intersects(tanks.get(i).getRect()))
					return;
			for (int i = 0; i < walls.size(); i++)
				if (blood.getRect().intersects(walls.get(i).getRect()))
					return;
			for (int i = 0; i < bloods.size(); i++)
				if (blood.getRect().intersects(bloods.get(i).getRect()))
					return;
			bloods.add(blood);
		}
	}

	/**
	 * 游戏对象绘制方法
	 */
	
	public void paint(Graphics g) {
		if (start) {
			addBlood();
			g.drawString("MissilesCount : " + missiles.size(), 10, 40);
			g.drawString("ExplodeCount : " + explodes.size(), 10, 50);
			g.drawString("TanksCount : " + (tanks.size()), 10, 60);
			g.drawString("WallsCount : " + walls.size(), 10, 70);
			g.drawString("BloodsCount : " + bloods.size(), 10, 80);
			g.drawString("Score : " + score, 10, 100);
			for (int i = 0; i < explodes.size(); i++)
				explodes.get(i).draw(g);
			for (int i = 0; i < missiles.size(); i++) {
				Missile missile = missiles.get(i);
				for (int j = 0; j < walls.size(); j++) {
					Wall w = walls.get(j);
					if (missile.hitWall(w))
						missiles.remove(missile);
				}
				for (int k = 0; k < tanks.size(); k++) {
					Tank tank = tanks.get(k);
					if (missiles.contains(missile) && missile.hitTank(tank)) {
						missiles.remove(missile);
						int life = tank.getLife() - missile.getAttackHurt();
						if (life <= 0) {
							score += killPoint;
							tanks.remove(tank);
							explodes.add(new Explode(tank.getX()
									+ tank.getSize() / 2, tank.getY()
									+ tank.getSize() / 2, this));
							if (tank.isGood())
								gameOver();
						} else {
							tank.setLife(life);
						}
					}
				}
			}
			for (int i = 0; i < tanks.size(); i++) {
				Tank t = tanks.get(i);
				t.draw(g);
				if (t.isGood())
					g.drawString("TankLife : " + t.getLife(), 10, 90);
				for (int j = 0; j < bloods.size(); j++) {
					Blood blood = bloods.get(j);
					if (blood.getRect().intersects(t.getRect())) {
						bloods.remove(blood);
						t.setLife(t.getLife() + blood.getBlood());
					}
				}
			}
			for (int i = 0; i < missiles.size(); i++)
				missiles.get(i).draw(g);
			for (int i = 0; i < walls.size(); i++)
				walls.get(i).draw(g);
			for (int i = 0; i < bloods.size(); i++)
				bloods.get(i).draw(g);
			if(tanks.size()0){
			boolean addAble=true;
			Tank t = new Tank(x, y, false, TankClient.this);
			t.setX(rand.nextInt(GAME_WIDTH - t.getSize()));
			t.setY(rand.nextInt(GAME_HEIGHT - t.getSize() - 25) + 25);
			for (int i = 0; i < tanks.size(); i++)
				if (t.getRect().intersects(tanks.get(i).getRect())){
					addAble=false;
					break;
				}
			for (int i = 0; i < walls.size(); i++)
				if (t.getRect().intersects(walls.get(i).getRect())){
					addAble=false;
					break;
				}
			if(addAble){
				tanks.add(t);
				tankNum--;	
			}
		}
	}

	/**
	 * 主方法
	 * @param args 命令行参数
	 */

	public static void main(String[] args) {
		TankClient tc = new TankClient();
		tc.launchFrame();
		new Thread(tc.new PaintThread()).start();
	}

	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 getRefreshTime() {
		return refreshTime;
	}

	public void setRefreshTime(int refreshTime) {
		this.refreshTime = refreshTime;
	}

	public Image getBufImg() {
		return bufImg;
	}

	public void setBufImg(Image bufImg) {
		this.bufImg = bufImg;
	}

	public Color getBgColor() {
		return bgColor;
	}

	public void setBgColor(Color bgColor) {
		this.bgColor = bgColor;
	}

	public boolean isHasGetdist() {
		return hasGetdist;
	}

	public void setHasGetdist(boolean hasGetdist) {
		this.hasGetdist = hasGetdist;
	}

	public int getDisX() {
		return disX;
	}

	public void setDisX(int disX) {
		this.disX = disX;
	}

	public int getDisY() {
		return disY;
	}

	public void setDisY(int disY) {
		this.disY = disY;
	}

	public List getExplodes() {
		return explodes;
	}

	public void setExplodes(List explodes) {
		this.explodes = explodes;
	}

	public List getTanks() {
		return tanks;
	}

	public void setTanks(List tanks) {
		this.tanks = tanks;
	}

	public List getMissiles() {
		return missiles;
	}

	public void setMissiles(List missiles) {
		this.missiles = missiles;
	}

	public int getEnemyNum() {
		return enemyNum;
	}

	public void setEnemyNum(int enemyNum) {
		this.enemyNum = enemyNum;
	}
	public List getWalls() {
		return walls;
	}

	public void setWalls(List walls) {
		this.walls = walls;
	}


}

//Tank.java

package edu.wkx;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.util.Random;

/**
 * 坦克类
 * @author Java_Bean 王凯旋
 *
 */

public class Tank {
	private int x;
	private int y;
	private int size = 50;
	private int xSpeed = 10;
	private int ySpeed = 10;
	private boolean bU = false;
	private boolean bD = false;
	private boolean bL = false;
	private boolean bR = false;
	private Direction dir = Direction.STOP;
	private Direction fireDir = Direction.R;
	private boolean good = true;
	private static Random rand = new Random();
	private int moveStep = 0;
	private int moveSpeed = 5;
	private int fireStep = 0;
	private int fireSpeed = 100;
	private int turnStep = 0;
	private int turnSpeed = 100;
	private int oriX = 0;
	private int oriY = 0;
	private int superFireSize = 100;
	private int totalLife = 100;
	private int life = 0;
	private int lifeBarLength = 7;
	private int lifeBarWidth = 70;

	private Color goodColor = Color.RED;
	private Color enemyColor = Color.DARK_GRAY;
	private Color fireColor = Color.BLUE;
	private Color lifeBarColor = Color.BLUE;
	private TankClient tc = null;

	enum Direction {
		U, D, L, R, LU, RU, LD, RD, STOP
	};

	/**
	 * 构造方法
	 * @param x 坦克出现位置的x坐标
	 * @param y 坦克出现位置的y坐标
	 * @param good 坦克的类别
	 */

	public Tank(int x, int y, boolean good) {
		this.x = x;
		this.y = y;
		this.good = good;
		this.life = this.totalLife;
	}

	/**
	 * 构造方法
	 * @param x 坦克出现位置的x坐标
	 * @param y 坦克出现位置的y坐标
	 * @param good 坦克的类别
	 * @param tc 坦克所属主类的引用
	 */
	
	public Tank(int x, int y, boolean good, TankClient tc) {
		this(x, y, good);
		this.tc = tc;
		moveStep = rand.nextInt(moveSpeed);
		fireStep = rand.nextInt(fireSpeed);
		turnStep = rand.nextInt(turnSpeed);
	}

	/**
	 * 在屏幕上画出它自己
	 * @param g 图形类
	 */

	public void draw(Graphics g) {
		Color orgColor = g.getColor();
		if (good)
			g.setColor(goodColor);
		else {
			g.setColor(enemyColor);
			turnStep++;
			if (turnStep == turnSpeed) {
				Direction[] dirs = Direction.values();
				dir = dirs[rand.nextInt(dirs.length)];
				if (dir != Direction.STOP)
					fireDir = dir;
				turnStep = 0;
			}
			moveStep++;
			if (moveStep == moveSpeed) {
				move();
				moveStep = 0;
			}
			fireStep++;
			if (fireStep == fireSpeed) {
				fire();
				fireStep = 0;
			}
		}
		g.fillOval(x, y, size, size);

		g.setColor(fireColor);
		switch (fireDir) {
		case U:
			g.drawLine(x + size / 2, y + size / 2, x + size / 2, y);
			break;
		case D:
			g.drawLine(x + size / 2, y + size / 2, x + size / 2, y + size);
			break;
		case L:
			g.drawLine(x + size / 2, y + size / 2, x, y + size / 2);
			break;
		case R:
			g.drawLine(x + size / 2, y + size / 2, x + size, y + size / 2);
			break;
		case LU:
			g.drawLine(x + size / 2, y + size / 2, x, y);
			break;
		case RU:
			g.drawLine(x + size / 2, y + size / 2, x + size, y);
			break;
		case LD:
			g.drawLine(x + size / 2, y + size / 2, x, y + size);
			break;
		case RD:
			g.drawLine(x + size / 2, y + size / 2, x + size, y + size);
			break;
		}

		// lifeBarlifeBarLength
		g.setColor(lifeBarColor);
		g.drawRect(x + size / 2 - lifeBarWidth / 2, y - 5 - lifeBarLength,
				lifeBarWidth, lifeBarLength);
		g.setColor(Color.RED);
		g.fillRect(
				x + size / 2 - lifeBarWidth / 2 + 2,
				y - 5 - lifeBarLength + 2,
				(int) ((double) life / (double) totalLife * (double) (lifeBarWidth - 4)+1),
				lifeBarLength - 3);

		g.setColor(orgColor);
	}

	private void move() {
		switch (dir) {
		case U:
			y -= ySpeed;
			break;
		case D:
			y += ySpeed;
			break;
		case L:
			x -= xSpeed;
			break;
		case R:
			x += xSpeed;
			break;
		case LU:
			x -= xSpeed;
			y -= ySpeed;
			break;
		case RU:
			x += xSpeed;
			y -= ySpeed;
			break;
		case LD:
			x -= xSpeed;
			y += ySpeed;
			break;
		case RD:
			x += xSpeed;
			y += ySpeed;
			break;
		case STOP:
			break;
		}
		boolean condition = x < 0 || x + size > TankClient.GAME_WIDTH
				|| y - 20 < 0 || y + size > TankClient.GAME_HEIGHT;
		for (int i = 0; i < tc.getWalls().size(); i++) {
			Wall wall = tc.getWalls().get(i);
			if (this.getRect().intersects(wall.getRect()))
				condition = true;
		}
		for (int i = 0; i < tc.getTanks().size(); i++) {
			Tank t = tc.getTanks().get(i);
			if (!this.equals(t) && this.getRect().intersects(t.getRect()))
				condition = true;
		}
		if (condition) {
			x = oriX;
			y = oriY;
			return;
		}
		oriX = x;
		oriY = y;
	}

	/**
	 * 键盘按下的处理
	 * @param key 键位值
	 */

	public void keyPressed(int key) {
		if (good) {
			switch (key) {
			case KeyEvent.VK_UP:
				bU = true;
				makeDir();
				move();
				break;
			case KeyEvent.VK_DOWN:
				bD = true;
				makeDir();
				move();
				break;
			case KeyEvent.VK_LEFT:
				bL = true;
				makeDir();
				move();
				break;
			case KeyEvent.VK_RIGHT:
				bR = true;
				makeDir();
				move();
				break;
			case KeyEvent.VK_CONTROL:
				fire();
				break;
			case KeyEvent.VK_A:
				bigFire();
				break;
			case KeyEvent.VK_X:
				xFire();
				break;
			}
		}
	}

	private void fire() {
		tc.getMissiles().add(
				new Missile(x + size / 2, y + size / 2, fireDir, this, tc));
	}

	private void bigFire() {
		tc.getMissiles().add(
				new Missile(x + size / 2, y + size / 2, fireDir, this, tc,
						superFireSize));
	}

	private void xFire() {
		Direction[] dirs = Direction.values();
		for (int i = 0; i < dirs.length - 1; i++)
			tc.getMissiles().add(
					new Missile(x + size / 2, y + size / 2, dirs[i], this, tc));
	}

	/**
	 * 按键释放的处理
	 * @param key 键位值
	 */

	public void keyReleased(int key) {
		if (good) {
			switch (key) {
			case KeyEvent.VK_UP:
				bU = false;
				makeDir();
				break;
			case KeyEvent.VK_DOWN:
				bD = false;
				makeDir();
				break;
			case KeyEvent.VK_LEFT:
				bL = false;
				makeDir();
				break;
			case KeyEvent.VK_RIGHT:
				bR = false;
				makeDir();
				break;
			}
		}
	}

	private void makeDir() {
		if (bU && !bD && !bL && !bR)
			fireDir = dir = Direction.U;
		if (!bU && bD && !bL && !bR)
			fireDir = dir = Direction.D;
		if (!bU && !bD && bL && !bR)
			fireDir = dir = Direction.L;
		if (!bU && !bD && !bL && bR)
			fireDir = dir = Direction.R;
		if (bU && !bD && bL && !bR)
			fireDir = dir = Direction.LU;
		if (bU && !bD && !bL && bR)
			fireDir = dir = Direction.RU;
		if (!bU && bD && bL && !bR)
			fireDir = dir = Direction.LD;
		if (!bU && bD && !bL && bR)
			fireDir = dir = Direction.RD;
	}

	public Rectangle getRect() {
		return new Rectangle(x, y, size, size);
	}

	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 getSize() {
		return size;
	}

	public void setSize(int size) {
		this.size = size;
	}

	public int getxSpeed() {
		return xSpeed;
	}

	public void setxSpeed(int xSpeed) {
		this.xSpeed = xSpeed;
	}

	public int getySpeed() {
		return ySpeed;
	}

	public void setySpeed(int ySpeed) {
		this.ySpeed = ySpeed;
	}

	public boolean isbU() {
		return bU;
	}

	public void setbU(boolean bU) {
		this.bU = bU;
	}

	public boolean isbD() {
		return bD;
	}

	public void setbD(boolean bD) {
		this.bD = bD;
	}

	public boolean isbL() {
		return bL;
	}

	public void setbL(boolean bL) {
		this.bL = bL;
	}

	public boolean isbR() {
		return bR;
	}

	public void setbR(boolean bR) {
		this.bR = bR;
	}

	public Direction getDir() {
		return dir;
	}

	public void setDir(Direction dir) {
		this.dir = dir;
	}

	public Direction getFireDir() {
		return fireDir;
	}

	public void setFireDir(Direction fireDir) {
		this.fireDir = fireDir;
	}

	public boolean isGood() {
		return good;
	}

	public void setGood(boolean good) {
		this.good = good;
	}

	public Color getGoodColor() {
		return goodColor;
	}

	public void setGoodColor(Color goodColor) {
		this.goodColor = goodColor;
	}

	public Color getEnemyColor() {
		return enemyColor;
	}

	public void setEnemyColor(Color enemyColor) {
		this.enemyColor = enemyColor;
	}

	public Color getFireColor() {
		return fireColor;
	}

	public void setFireColor(Color fireColor) {
		this.fireColor = fireColor;
	}

	public TankClient getTc() {
		return tc;
	}

	public void setTc(TankClient tc) {
		this.tc = tc;
	}

	public static Random getRand() {
		return rand;
	}

	public static void setRand(Random rand) {
		Tank.rand = rand;
	}

	public int getMoveStep() {
		return moveStep;
	}

	public void setMoveStep(int moveStep) {
		this.moveStep = moveStep;
	}

	public int getMoveSpeed() {
		return moveSpeed;
	}

	public void setMoveSpeed(int moveSpeed) {
		this.moveSpeed = moveSpeed;
	}

	public int getFireStep() {
		return fireStep;
	}

	public void setFireStep(int fireStep) {
		this.fireStep = fireStep;
	}

	public int getFireSpeed() {
		return fireSpeed;
	}

	public void setFireSpeed(int fireSpeed) {
		this.fireSpeed = fireSpeed;
	}

	public int getTurnStep() {
		return turnStep;
	}

	public void setTurnStep(int turnStep) {
		this.turnStep = turnStep;
	}

	public int getTurnSpeed() {
		return turnSpeed;
	}

	public void setTurnSpeed(int turnSpeed) {
		this.turnSpeed = turnSpeed;
	}

	public int getOriX() {
		return oriX;
	}

	public void setOriX(int oriX) {
		this.oriX = oriX;
	}

	public int getOriY() {
		return oriY;
	}

	public void setOriY(int oriY) {
		this.oriY = oriY;
	}

	public int getSuperFireSize() {
		return superFireSize;
	}

	public void setSuperFireSize(int superFireSize) {
		this.superFireSize = superFireSize;
	}

	public int getTotalLife() {
		return totalLife;
	}

	public void setTotalLife(int totalLife) {
		this.totalLife = totalLife;
	}

	public int getLife() {
		return life;
	}

	public void setLife(int life) {
		if(life>this.totalLife)
			life=this.totalLife;
		if(life<0)
			life=0;
		this.life = life;
	}
}

//Wall.java

package edu.wkx;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;

/**
 * 墙类
 * @author Java_Bean 王凯旋
 *
 */

public class Wall {
	private int x = 0;
	private int y = 0;
	private int width = 0;
	private int height = 0;
	private Color color = Color.BLACK;

	/**
	 * 构造方法
	 * @param x 墙出现的x坐标
	 * @param y 墙出现的y坐标
	 * @param width 墙的宽度
	 * @param height 墙的高度
	 */
	
	public Wall(int x, int y, int width, int height) {
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;
	}
	
	/**
	 * 构造方法
	 * @param x 墙出现的x坐标
	 * @param y 墙出现的y坐标
	 * @param width 墙的宽度
	 * @param height 墙的高度
	 * @param color 墙的颜色
	 */
	
	public Wall(int x, int y, int width, int height, Color color) {
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;
		this.color = color;
	}

	/**
	 * 在屏幕上画出它自己
	 * @param g 图形类
	 */
	
	public void draw(Graphics g) {
		Color orgColor = g.getColor();
		g.setColor(color);
		g.fillRect(x, y, width, height);
		g.setColor(orgColor);
	}
	
	/**
	 * 得到代表所占范围的矩形
	 * @return 代表他所占范围的矩形
	 */

	public Rectangle getRect() {
		return new Rectangle(x, y, width, height);
	}
}

//Explode.java

package edu.wkx;
import java.awt.Color;
import java.awt.Graphics;

/**
 * 爆炸类
 * @author Java_Bean 王凯旋
 *
 */

public class Explode {
	private int x = 0;
	private int y = 0;
	private Color color = Color.WHITE;
	private int totalStep = 70;
	private int speed = 8;
	private int step = 0;
	private TankClient tc = null;
	
	/**
	 * 构造函数
	 * @param x 爆炸地点的x坐标
	 * @param y 爆炸地点的y坐标
	 * @param tc 所属主类的引用
	 */
	
	public Explode(int x, int y, TankClient tc) {
		this.x = x;
		this.y = y;
		this.tc = tc;
	}
	
	/**
	 * 在屏幕上能画出他自己
	 * @param g 图形类
	 */

	public void draw(Graphics g) {
		Color orgColor = g.getColor();
		g.setColor(color);
		g.fillOval(x - step / 2, y - step / 2, step, step);
		step+=speed;
		if (step >= totalStep) {
			step = 0;
			tc.getExplodes().remove(this);
		}
		g.setColor(orgColor);
	}

	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 Color getColor() {
		return color;
	}

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

	public int getTotalStep() {
		return totalStep;
	}

	public void setTotalStep(int totalStep) {
		this.totalStep = totalStep;
	}

	public int getStep() {
		return step;
	}

	public void setStep(int step) {
		this.step = step;
	}

	public TankClient getTc() {
		return tc;
	}

	public void setTc(TankClient tc) {
		this.tc = tc;
	}
	
	public int getSpeed() {
		return speed;
	}

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


}

//Missile.java

package edu.wkx;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;

/**
 * 子弹类
 * @author Java_Bean 王凯旋
 *
 */

public class Missile {

	private int x = 0;
	private int y = 0;
	private int size = 7;
	private Color color = Color.BLACK;
	private int xSpeed = 15;
	private int ySpeed = 15;
	private Tank.Direction dir = Tank.Direction.STOP;
	private TankClient tc = null;
	private Tank t = null;
	private int attackHurt = 10;
	private int bigAttackHurt = 100;

	/**
	 * 构造函数
	 * @param x 子弹所在位置的x坐标
	 * @param y 子弹所在位置的y坐标
	 * @param dir 子弹飞出的方向
	 * @param t 发出子弹的坦克的引用
	 * @param tc 子弹所属主类的引用
	 */
	
	public Missile(int x, int y, Tank.Direction dir, Tank t, TankClient tc) {
		this.x = x - size / 2;
		this.y = y - size / 2;
		this.dir = dir;
		this.tc = tc;
		this.t = t;
	}

	/**
	 * 构造函数
	 * @param x 子弹所在位置的x坐标
	 * @param y 子弹所在位置的y坐标
	 * @param dir 子弹飞出的方向
	 * @param t 发出子弹的坦克的引用
	 * @param tc 子弹所属主类的引用
	 * @param size 子弹的大小
	 */
	
	public Missile(int x, int y, Tank.Direction dir, Tank t, TankClient tc,
			int size) {
		this.size = size;
		this.x = x - size / 2;
		this.y = y - size / 2;
		this.dir = dir;
		this.tc = tc;
		this.t = t;
		this.attackHurt = bigAttackHurt;
	}

	/**
	 * 在屏幕上画出他自己
	 * @param g 图形类
	 */
	
	public void draw(Graphics g) {
		Color orgColor = g.getColor();
		g.setColor(color);
		g.fillOval(x, y, size, size);
		g.setColor(orgColor);
		move();
	}
	
	private void move() {
		switch (dir) {
		case U:
			y -= ySpeed;
			break;
		case D:
			y += ySpeed;
			break;
		case L:
			x -= xSpeed;
			break;
		case R:
			x += xSpeed;
			break;
		case LU:
			x -= xSpeed;
			y -= ySpeed;
			break;
		case RU:
			x += xSpeed;
			y -= ySpeed;
			break;
		case LD:
			x -= xSpeed;
			y += ySpeed;
			break;
		case RD:
			x += xSpeed;
			y += ySpeed;
			break;
		}
		if (x + size / 2 < 0 || y + size / 2 < 0
				|| x + size / 2 > TankClient.GAME_WIDTH
				|| y + size / 2 > TankClient.GAME_HEIGHT)
			tc.getMissiles().remove(this);
	}
	
	/**
	 * 判断是否撞到了坦克
	 * @param t 所要判断的坦克
	 * @return 是否撞到了坦克
	 */

	public boolean hitTank(Tank t) {
		if (this.getRect().intersects(t.getRect())) {
			if ((this.t.isGood() && !t.isGood())
					|| (!this.t.isGood() && t.isGood()))
				return true;
		}
		return false;
	}

	/**
	 * 判断是否撞到了墙
	 * @param w 所要判断的墙
	 * @return 是否撞到了墙
	 */
	
	public boolean hitWall(Wall w) {
		return this.getRect().intersects(w.getRect());
	}

	/**
	 * 得到表示子弹范围的矩形
	 * @return 表示子弹范围的矩形
	 */
	
	public Rectangle getRect() {
		return new Rectangle(x, y, size, size);
	}

	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 getSize() {
		return size;
	}

	public void setSize(int size) {
		this.size = size;
	}

	public Color getColor() {
		return color;
	}

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

	public int getxSpeed() {
		return xSpeed;
	}

	public void setxSpeed(int xSpeed) {
		this.xSpeed = xSpeed;
	}

	public int getySpeed() {
		return ySpeed;
	}

	public void setySpeed(int ySpeed) {
		this.ySpeed = ySpeed;
	}

	public Tank.Direction getDir() {
		return dir;
	}

	public void setDir(Tank.Direction dir) {
		this.dir = dir;
	}

	public TankClient getTc() {
		return tc;
	}

	public void setTc(TankClient tc) {
		this.tc = tc;
	}

	public Tank getT() {
		return t;
	}

	public void setT(Tank t) {
		this.t = t;
	}

	public int getAttackHurt() {
		return attackHurt;
	}

	public void setAttackHurt(int attackHurt) {
		this.attackHurt = attackHurt;
	}

}

//Blood.java

package edu.wkx;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;

/**
 * 补血血块类
 * @author Java_Bean 王凯旋
 *
 */

public class Blood {
	private int x = 0;
	private int y = 0;
	private int blood = 15;
	private int size = 10;

	private Color color=Color.RED;

	/**
	 * 构造方法
	 * @param x 血块出现的x坐标
	 * @param y 血块出现的y坐标
	 */
	
	public Blood(int x, int y) {
		this.x = x;
		this.y = y;
	}

	/**
	 * 构造方法
	 * @param x 血块出现的x坐标
	 * @param y 血块出现的y坐标
	 * @param blood 一个血块回复的生命值
	 */
	
	public Blood(int x, int y, int blood) {
		this(x, y);
		this.blood = blood;
	}
	
	/**
	 * 判断是否和坦克相撞
	 * @param t 与之相撞的坦克
	 * @return	是否相撞
	 */
	
	public boolean hitTank(Tank t) {
		return this.getRect().intersects(t.getRect());
	}
	
	/**
	 * 在屏幕上画出他自己
	 * @param g 图形类
	 */
	
	void draw(Graphics g) {
		Color orgColor = g.getColor();
		g.setColor(color);
		g.fillOval(x, y, size, size);
		g.setColor(orgColor);
	}
	
	/**
	 * 得到血块所代表的矩形
	 * @return 血块代表的矩形
	 */
	
	public Rectangle getRect() {
		return new Rectangle(x, y, size, size);
	}

	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 getBlood() {
		return blood;
	}

	public void setBlood(int blood) {
		this.blood = blood;
	}
	
	public int getSize() {
		return size;
	}

	public void setSize(int size) {
		this.size = size;
	}
}

刚刚收到邮件,幸运获得了百度之星的纪念T-shirt ,哇哈哈哈~~


你可能感兴趣的:(Java 坦克大战单机版 源代码)