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

📄 tank.java

📁 纯java编写的坦克大战游戏.把项目引入eclipse后立即可运行。在此基础上可进行二次开发哦。
💻 JAVA
字号:
import static java.awt.event.KeyEvent.VK_CONTROL;
import static java.awt.event.KeyEvent.VK_DOWN;
import static java.awt.event.KeyEvent.VK_LEFT;
import static java.awt.event.KeyEvent.VK_RIGHT;
import static java.awt.event.KeyEvent.VK_UP;
import static java.awt.event.KeyEvent.VK_F2;
import static java.awt.event.KeyEvent.VK_A;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.util.List;
import java.util.Random;


public class Tank {
	int x, y;
	int oldX, oldY;
	Direction dir = Direction.STOP;
	Direction ptDir = Direction.D;
	private boolean bL, bU, bR, bD;
	boolean good;
	boolean live = true;
	private static Random r = new Random();
	private int step = r.nextInt(10);
	TankClient tc;
	int life = 100;
	BloodBar bb = new BloodBar(this);
	
	public static final int XSPEED = 5;
	public static final int YSPEED = 5;
	public static final int WIDTH = 30;
	public static final int HEIGHT = 30;
	
	public Tank(int x, int y, Direction dir, boolean good, TankClient tc) {
		this.x = x;
		this.y = y;
		this.dir = dir;
		this.good = good;
		this.tc = tc;
	}
	
	public void draw(Graphics g) {
		if(!live) {
			tc.tanks.remove(this);
			return;
		}
		
		move();
		
		collidesWithWall(tc.w1);
		collidesWithWall(tc.w2);
		
		collidesWithTanks(tc.tanks);
		
		Color c = g.getColor();
		if(good)
			g.setColor(Color.RED);
		else 
			g.setColor(Color.BLUE);
		g.fillOval(x, y, WIDTH, HEIGHT);
		g.setColor(c);
		
		if(good) {
			bb.draw(g);
		}
		
		drawPT(g);
	}

	private void drawPT(Graphics g) {
		
		int ox = this.x + WIDTH/2;
		int oy = this.y + HEIGHT/2;
		
		switch (ptDir) {
		case L:
			g.drawLine(ox, oy, x, y + HEIGHT/2);
			break;
		case LU:
			g.drawLine(ox, oy, x, y);
			break;
		case U:
			g.drawLine(ox, oy, x + WIDTH/2, y);
			break;
		case RU:
			g.drawLine(ox, oy, x + WIDTH, y);
			break;
		case R:
			g.drawLine(ox, oy, x + WIDTH, y + HEIGHT/2);
			break;
		case RD:
			g.drawLine(ox, oy, x + WIDTH, y + HEIGHT);
			break;
		case D:
			g.drawLine(ox, oy, x + WIDTH/2, y + HEIGHT);
			break;
		case LD:
			g.drawLine(ox, oy, x, y + HEIGHT);
			break;
		default:
			break;
		}
	}

	private void move() {
		oldX = x;
		oldY = y;
		
		switch (dir) {
		case L:
			x -= XSPEED;
			break;
		case LU:
			x -= XSPEED;
			y -= YSPEED;
			break;
		case U:
			y -= YSPEED;
			break;
		case RU:
			x += XSPEED;
			y -= YSPEED;
			break;
		case R:
			x += XSPEED;
			break;
		case RD:
			x += XSPEED;
			y += YSPEED;
			break;
		case D:
			y += YSPEED;
			break;
		case LD:
			x -= XSPEED;
			y += YSPEED;
			break;
		default:
			break;
		}
		
		if(this.dir != Direction.STOP) {
			this.ptDir = dir;
		}
		
		if(x < 0) x = 0;
		if(y < 30) y = 30;
		if(x + WIDTH > TankClient.GAME_WIDTH) x = TankClient.GAME_WIDTH - WIDTH;
		if(y + HEIGHT > TankClient.GAME_HEIGHT) y = TankClient.GAME_HEIGHT - HEIGHT;
		
		if(!good) {
			randomTankAction();
		}
	}

	private void randomTankAction() {	
		step --;
		
		if(step <= 0) {
			step = r.nextInt(10);
			Direction[] dirs = Direction.values();
			this.dir = dirs[r.nextInt(dirs.length)];
		}
		
		if(r.nextInt(40) > 36) {
			fire();
		}
	}

	public void keyPressed(KeyEvent e) {
		int key = e.getKeyCode();
		switch (key) {
		case VK_F2:
			if(!this.live) {
				this.relive();
			}
			break;
		case VK_LEFT:
			bL = true;
			break;
		case VK_UP:
			bU = true;
			break;
		case VK_RIGHT:
			bR = true;
			break;
		case VK_DOWN:
			bD = true;
			break;
		default:
			break;
		}
		
		locateDirection();
		
	}

	private void relive() {
		this.live = true;
		this.life = 100;
	}
	
	private void superFire() {
		Direction[] dirs = Direction.values();
		for (int i = 0; i < dirs.length; i++) {
			if(dirs[i] != Direction.STOP) {
				fire(dirs[i]);
			}
		}
	}
	
	private Missile fire(Direction dir) {
		if(!live) {
			return null;
		}
		
		int mx = x + (Tank.WIDTH - Missile.WIDTH) / 2;
		int my = y + (Tank.HEIGHT - Missile.HEIGHT) / 2;
		Missile m = new Missile(mx, my, dir, this.good, this.tc);
		tc.missiles.add(m);
		return m;
	}
	
	private Missile fire() {
		
		return fire(this.ptDir);
	}

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

	public void keyReleased(KeyEvent e) {
		int key = e.getKeyCode();
		switch (key) {
		case VK_A: 
			superFire();
			break;
		case VK_CONTROL:
			fire();
			break;
		case VK_LEFT:
			bL = false;
			break;
		case VK_UP:
			bU = false;
			break;
		case VK_RIGHT:
			bR = false;
			break;
		case VK_DOWN:
			bD = false;
			break;
		default:
			break;
		}
		
		locateDirection();
	
	}

	public Rectangle getRect() {
		return new Rectangle(x, y, WIDTH, HEIGHT);
	}
	
	private void collidesWithWall(Wall w) {
		if(this.getRect().intersects(w.getRect())) {
			this.stay();
		}
	}
	
	public void collidesWithTank(Tank t) {
		if(this != t && this.getRect().intersects(t.getRect())) {
			this.stay();
			t.stay();
		}
	}
	
	public void collidesWithTanks(List<Tank> tanks) {
		for(int i=0; i<tanks.size(); i++) {
			collidesWithTank(tanks.get(i));
		}
	}
	
	public void stay() {
		this.x = oldX;
		this.y = oldY;
	}
	
}

class BloodBar {
	Tank t;
	
	BloodBar(Tank t) {
		this.t = t;
	}
	
	public void draw(Graphics g) {
		Color c = g.getColor();
		g.setColor(Color.RED);
		g.drawRect(t.x, t.y - 12, Tank.WIDTH, 10);
		g.fillRect(t.x, t.y - 12, Tank.WIDTH * t.life / 100, 10);
		g.setColor(c);
	}
}

⌨️ 快捷键说明

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