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

📄 missile.java

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

public class Missile {
	
	public static final int XSPEED = 5;
	public static final int YSPEED = 5;
	
	public static final int WIDTH = 10, HEIGHT = 10;
	
	private int x,y;
	
	private Tank.Direction dir;
	
	private TankFrame tf;
	
	private boolean live = true;
	
	private boolean good;
	
	public Missile(int x, int y, Tank.Direction dir) {
		this.x = x;
		this.y = y;
		this.dir = dir;
	}
	
	public Missile(int x, int y, Tank.Direction dir, boolean good, TankFrame tf) {
		this(x,y,dir);
		this.tf = tf;
		this.good = good;
	}
	
	public void draw(Graphics g) {
		if(live == false) {
			tf.missiles.remove(this);
			return;
		}
		Color c = g.getColor();
		if(!good) {
			g.setColor(Color.BLACK);
		} else {
			g.setColor(Color.WHITE);
		}
		g.fillOval(x, y, WIDTH, HEIGHT);
		g.setColor(c);
		
		move();
	}
	
	public void move() {
		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;
		}
		if(x < 0 || y < 0 || x > TankFrame.GAME_WIDTH || y > TankFrame.GAME_HEIGHT) {
			live = false;
		}
	}
	
	public boolean isLive() {
		return live;
	}
	
	public Rectangle getRect() {
		return new Rectangle(x, y, WIDTH, HEIGHT);
	}
	
	public boolean hitTank(Tank t) {
		if(getRect().intersects(t.getRect()) && t.isLive() && good != t.isGood()) {
			live = false;
			t.setLive(false);
			tf.explodes.add(new Explode(x, y, tf));
			return true;
		}
		return false;
	}
	
	public boolean hitTanks(List<Tank> tanks) {
		for(int i=0; i<tanks.size(); i++) {
			if(hitTank(tanks.get(i))) {
				return true;
			}
		}
		return false;
	}
	
	public boolean hitWall(Wall w) {
		if(this.getRect().intersects(w.getRect()) && this.isLive()) {
			live = false;
			return true;
		}
		return false;
	}
	
}

⌨️ 快捷键说明

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