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

📄 tank.java

📁 简单的坦克大战 可以打敌人 更多功能还在编写当中
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.*;

public class Tank {
		
	public static final int XSPEED = 5, YSPEED = 5;
	
	public static final int WIDTH = 30, HEIGHT = 30;
	
	private int x,y;
	
	private int oldX, oldY;
	
	private boolean tU = false, tR = false, tD = false, tL = false;
	
	enum Direction {U,UR,R,RD,D,DL,L,LU,STOP};
	
	private Direction dir = Direction.STOP;
	private Direction ptDir = Direction.D;
	
	private static Random r = new Random();
	
	private int step = r.nextInt(10) + 3;
	
	TankFrame tf;
	
	private boolean good = true;
	private boolean live = true;

	public Tank(int x, int y, boolean good) {
		this.x = x;
		this.y = y;
		this.oldX = x;
		this.oldY = y;
		this.good = good;
	}
	
	public Tank(int x, int y, boolean good, Direction dir, Direction ptDir, TankFrame tf) {
		this(x,y,good);
		this.tf = tf;
		this.dir = dir;
		this.ptDir = ptDir;
	}
	
	public void paint(Graphics g) {
		if(live == false) {
			if(!good) {
				tf.tanks.remove(this);
			}
			return;
		}
		Color c = g.getColor();
		if(good == true) {
			g.setColor(Color.RED);
		} else {
			g.setColor(Color.BLUE);
		}
		g.fillOval(x, y, Tank.WIDTH, Tank.HEIGHT);
		g.setColor(c);
		
		move();
		
		switch(ptDir) {
		case U: 
			g.drawLine(x + Tank.WIDTH / 2, y + Tank.HEIGHT / 2, x + Tank.WIDTH / 2, y);
			break;
		case UR:
			g.drawLine(x + Tank.WIDTH / 2, y + Tank.HEIGHT / 2, x + Tank.WIDTH, y);
			break;
		case R:
			g.drawLine(x + Tank.WIDTH / 2, y + Tank.HEIGHT / 2, x + Tank.WIDTH, y + Tank.HEIGHT / 2);
			break;
		case RD:
			g.drawLine(x + Tank.WIDTH / 2, y + Tank.HEIGHT / 2, x + Tank.WIDTH, y + Tank.HEIGHT);
			break;
		case D:
			g.drawLine(x + Tank.WIDTH / 2, y + Tank.HEIGHT / 2, x + Tank.WIDTH / 2, y + Tank.HEIGHT);
			break;
		case DL:
			g.drawLine(x + Tank.WIDTH / 2, y + Tank.HEIGHT / 2, x, y + Tank.HEIGHT);
			break;
		case L:
			g.drawLine(x + Tank.WIDTH / 2, y + Tank.HEIGHT / 2, x, y + Tank.HEIGHT / 2);
			break;
		case LU:
			g.drawLine(x + Tank.WIDTH / 2, y + Tank.HEIGHT / 2, x, y);
			break;
		}
		
	}
	
	public void move() {
		
		this.oldX = x;
		this.oldY = y;
		
		switch(dir) {
		case U: 
			y -= YSPEED; 
			break;
		case UR:
			x += XSPEED;
			y -= YSPEED;
			break;
		case R:
			x += XSPEED;
			break;
		case RD:
			x += XSPEED;
			y += YSPEED;
			break;
		case D:
			y += YSPEED;
			break;
		case DL:
			x -= XSPEED;
			y += YSPEED;
			break;
		case L:
			x -= XSPEED;
			break;
		case LU:
			x -= XSPEED;
			y -= YSPEED;
			break;
		case STOP:
			break;
		}
		
		if(dir != Direction.STOP) {
			ptDir = dir;
		}
		
		if(x < 0) {
			x = 0;
		}
		if(y < 30) {
			y = 30;
		}
		if(x + Tank.WIDTH > tf.GAME_WIDTH) {
			x = tf.GAME_WIDTH - Tank.WIDTH;
		}
		if(y + Tank.HEIGHT > tf.GAME_HEIGHT) {
			y = tf.GAME_HEIGHT - Tank.HEIGHT;
		}
		
		if(!good) {
			Direction[] dirs = Direction.values();
			if(step == 0) {
				step = r.nextInt(10) + 3;
				int rn = r.nextInt(dirs.length);
				dir = dirs[rn];
			}
			step--;
			if(r.nextInt(40) > 37) {
				this.fire();
			}
		}
			
	}
	
	public void keyPressed(KeyEvent k) {
		int i = k.getKeyCode();
		switch(i) {
		case KeyEvent.VK_UP :
			tU = true;
			break;
		case KeyEvent.VK_RIGHT :
			tR = true;
			break;
		case KeyEvent.VK_DOWN :
			tD = true;
			break;
		case KeyEvent.VK_LEFT :
			tL = true;
			break;	
		}
		cDirection();
	}
	

	public void keyReleased(KeyEvent k) {
		int i = k.getKeyCode();
		switch(i) {
		case KeyEvent.VK_CONTROL :
			fire();
			break;
		case KeyEvent.VK_UP :
			tU = false;
			break;
		case KeyEvent.VK_RIGHT :
			tR = false;
			break;
		case KeyEvent.VK_DOWN :
			tD = false;
			break;
		case KeyEvent.VK_LEFT :
			tL = false;
			break;	
		}
		cDirection();
	}
	
	public void cDirection() {
		if(tU && !tR && !tD && !tL) {
			dir = Direction.U;
		} else if(tU && tR && !tD && !tL) {
			dir = Direction.UR;
		} else if(!tU && tR && !tD && !tL) {
			dir = Direction.R;
		} else if(!tU && tR && tD && !tL) {
			dir = Direction.RD;
		} else if(!tU &&! tR && tD && !tL) {
			dir = Direction.D;
		} else if(!tU && !tR && tD && tL) {
			dir = Direction.DL;
		} else if(!tU && !tR && !tD && tL) {
			dir = Direction.L;
		} else if(tU && !tR && !tD && tL) {
			dir = Direction.LU;
		} else if(!tU && !tR && !tD && !tL) {
			dir = Direction.STOP;
		}
	}
	
	public Rectangle getRect() {
		return new Rectangle(x, y, WIDTH, HEIGHT);
	}
	
	public Missile fire() {
		if(!live) {
			return null;
		}
		int x = this.x + Tank.WIDTH / 2 - Missile.WIDTH / 2;
		int y = this.y + Tank.HEIGHT / 2 - Missile.HEIGHT / 2;
		Missile m = new Missile(x, y, ptDir, good, tf);
		tf.missiles.add(m);
		return m;
	}
	
	public boolean isLive() {
		return live;
	}

	public void setLive(boolean live) {
		this.live = live;
	}
	
	public boolean isGood() {
		return good;
	}
	
	public void stay() {
		x = oldX;
		y = oldY;
	}
	
	public boolean hitWall(Wall w) {
		if(this.getRect().intersects(w.getRect()) && this.isLive()) {
			stay();
			return true;
		}
		return false;
	}

}

⌨️ 快捷键说明

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