⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tank.java

📁 坦克大战代码
💻 JAVA
字号:
package com.caizi;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class Tank {

	private int x, y, oldX, oldY;
	private static final int X_SPEED = 3;
	private static final int Y_SPEED = 3;
	private static final int WIDTH = 40;
	private static final int HIGHTH = 40;
	
	private static Toolkit t = Toolkit.getDefaultToolkit();
	private static Image[] images = {
		t.getImage(Explode.class.getClassLoader().getResource("images/tankU.gif")),
		t.getImage(Explode.class.getClassLoader().getResource("images/tankRU.gif")),
		t.getImage(Explode.class.getClassLoader().getResource("images/tankR.gif")),
		t.getImage(Explode.class.getClassLoader().getResource("images/tankDR.gif")),
		t.getImage(Explode.class.getClassLoader().getResource("images/tankD.gif")),
		t.getImage(Explode.class.getClassLoader().getResource("images/tankUL.gif")),
		t.getImage(Explode.class.getClassLoader().getResource("images/tankL.gif")),
		t.getImage(Explode.class.getClassLoader().getResource("images/tankUL.gif")),
};
	private static Map<String, Image> tankMap = new HashMap<String, Image>();
	static {
		tankMap.put("N", images[0]);
		tankMap.put("NE", images[1]);
		tankMap.put("E", images[2]);
		tankMap.put("SE", images[3]);
		tankMap.put("S", images[4]);
		tankMap.put("SW", images[5]);
		tankMap.put("W", images[6]);
		tankMap.put("NW", images[7]);
	}
	private static int step = 0;	
	private static Random random = new Random();
	
	private BloodBar bar = new BloodBar();

	private int blood = 100;
	private boolean live = true;
	private boolean l = false, r = false, u = false, d = false;
	private boolean friend = true;
	private boolean robot = true;	

	private Direction dir = Direction.STOP;
	private Direction fireDir = Direction.S;
	TankClient tc = null;

	public Tank(int x, int y, boolean friend) {
		super();
		this.x = x;
		this.y = y;
		this.friend = friend;
	}

	public Tank(int x, int y, boolean friend, TankClient tc) {
		this(x, y, friend);
		this.tc = tc;
	}

	public Tank(int x, int y, boolean friend, boolean robot, TankClient tc) {
		this(x, y, friend, tc);
		this.robot = robot;
	}

	public int getBlood() {
		return blood;
	}

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

	public boolean isLive() {
		return live;
	}

	public void setLive(boolean live) {
		this.live = live;
	}

	public boolean isRobot() {
		return robot;
	}

	public boolean isFriend() {
		return friend;
	}

	public void draw(Graphics g) {
		bar.draw(g);
		
		switch (fireDir) {
		case N:
			g.drawImage(tankMap.get("N"), x, y, null);
			break;
		case NE:
			g.drawImage(tankMap.get("NE"), x, y, null);
			break;
		case E:
			g.drawImage(tankMap.get("E"), x, y, null);
			break;
		case SE:
			g.drawImage(tankMap.get("SE"), x, y, null);
			break;
		case S:
			g.drawImage(tankMap.get("S"), x, y, null);
			break;
		case SW:
			g.drawImage(tankMap.get("SW"), x, y, null);
			break;
		case W:
			g.drawImage(tankMap.get("W"), x, y, null);
			break;
		case NW:
			g.drawImage(tankMap.get("NW"), x, y, null);
			break;
		}
		move();
		robotFire();
	}

	public void keyPress(KeyEvent e) {
		int keyType = e.getKeyCode();
		switch (keyType) {
		case KeyEvent.VK_LEFT:
			l = true;
			break;
		case KeyEvent.VK_RIGHT:
			r = true;
			break;
		case KeyEvent.VK_UP:
			u = true;
			break;
		case KeyEvent.VK_DOWN:
			d = true;
			break;
		}
		setDir();
	}

	public void keyRelease(KeyEvent e) {
		int keyType = e.getKeyCode();
		switch (keyType) {
		case KeyEvent.VK_SPACE:
			fire();
			break;
		case KeyEvent.VK_LEFT:
			l = false;
			break;
		case KeyEvent.VK_RIGHT:
			r = false;
			break;
		case KeyEvent.VK_UP:
			u = false;
			break;
		case KeyEvent.VK_DOWN:
			d = false;
			break;
		}
		setDir();
	}

	private void setDir() {
		if (l & !r & !u & !d) {
			dir = Direction.W;
			fireDir = Direction.W;
		} else if (!l & r & !u & !d) {
			dir = Direction.E;
			fireDir = Direction.E;
		} else if (!l & !r & u & !d) {
			dir = Direction.N;
			fireDir = Direction.N;
		} else if (!l & !r & !u & d) {
			dir = Direction.S;
			fireDir = Direction.S;
		} else if (l & u & !r & !d) {
			dir = Direction.NW;
			fireDir = Direction.NW;
		} else if (!l & u & r & !d) {
			dir = Direction.NE;
			fireDir = Direction.NE;
		} else if (!l & !u & r & d) {
			dir = Direction.SE;
			fireDir = Direction.SE;
		} else if (l & !u & !r & d) {
			dir = Direction.SW;
			fireDir = Direction.SW;
		} else if (!l & !u & !r & !d) {
			dir = Direction.STOP;
		}
	}

	private void move() {
		this.oldX = this.x;
		this.oldY = this.y;
		if (robot) {
			if (step == 0) {
				step = random.nextInt(6) + 3;
				Direction[] dirArray = dir.values();
				int dirInt = random.nextInt(dirArray.length);
				dir = dirArray[dirInt];
				if (dir != Direction.STOP) {
					fireDir = dir;
				}
			}
			step--;
		}
		switch (dir) {
		case N:
			y -= Y_SPEED;
			break;
		case NE:
			x += X_SPEED;
			y -= Y_SPEED;
			break;
		case E:
			x += X_SPEED;
			break;
		case SE:
			x += X_SPEED;
			y += Y_SPEED;
			break;
		case S:
			y += Y_SPEED;
			break;
		case SW:
			x -= X_SPEED;
			y += Y_SPEED;
			break;
		case W:
			x -= X_SPEED;
			break;
		case NW:
			x -= X_SPEED;
			y -= Y_SPEED;
			break;
		case STOP:
			break;
		}
		if (x < 0)
			x = 0;
		else if (x > TankClient.WINDOW_WIDTH - this.WIDTH)
			x = TankClient.WINDOW_WIDTH - this.WIDTH;
		if (y < 30)
			y = 30;
		else if (y > TankClient.WINDOW_HEIGHT - this.HIGHTH)
			y = TankClient.WINDOW_HEIGHT - this.HIGHTH;
	}

	public void robotFire() {
		if (this.isRobot()) {
			int temp = (int) (Math.random() * 100);
			if (temp > 90) {
				fire();
			}
		}
	}

	public void fire() {
		if (this.isLive()) {
			int x = this.x + this.WIDTH / 2 - Missile.WIDTH / 2;
			int y = this.y + this.HIGHTH / 2 - Missile.HIGHTH / 2;
			Missile missile = new Missile(x, y, fireDir, this.friend, this.tc);
			tc.missiles.add(missile);
		}
	}

	public Rectangle getRect() {
		return new Rectangle(this.x, this.y, this.WIDTH, this.HIGHTH);
	}

	public void beHitted() {
		this.blood -= 50;
		if (this.blood <= 0) {
			this.setLive(false);
		}
	}

	public void stay() {
		this.x = this.oldX;
		this.y = this.oldY;
	}

	public boolean hitWall(Wall wall) {
		if (this.isLive() && this.getRect().intersects(wall.getRect())) {
			stay();
			return true;
		}
		return false;
	}

	public void hitTank(Tank tank) {
		if (this.isLive() && tank.isLive() && this != tank
				&& this.getRect().intersects(tank.getRect())) {
			this.stay();
			tank.stay();
		}
	}

	public void hitTank(List<Tank> tanks) {
		for (int i = 0; i < tanks.size(); i++) {
			Tank tank = tanks.get(i);
			this.hitTank(tank);
		}
	}
	public void eatLife(Life life){
		if(this.isLive() && life.isLive() && this.getRect().intersects(life.getRect())){
			this.blood += 100;
			life.setLive(false);
		}
	}

	private class BloodBar{
		public void draw(Graphics g){
			Color c = g.getColor();
			g.setColor(Color.WHITE);
			g.drawRect(x, y-10, WIDTH, 10);
			g.setColor(Color.RED);
			int width = WIDTH*blood/100;
			g.fillRect(x, y-10, width, 10);
			g.setColor(c);
		}
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -