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

📄 missile.java

📁 java小项目,坦克大战,学习的好资料 eclipse下可运行
💻 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;
	public static final int HEIGHT = 10;
	
	private boolean live = true;
	private boolean good;
	int x,y;
	Tank.Direction dir;
	private TankClient tc;
	Tank t;
	MyHome mh;
	
	public Missile(int x, int y, boolean good,Tank.Direction dir) {
		this.x = x;
		this.y = y;
		this.good = good;
		this.dir = dir;
	}

	public Missile(int x, int y, boolean good,Tank.Direction dir, TankClient tc) {
		this(x,y,good,dir);
		this.tc = tc;
	}

	public void draw(Graphics g) {
		if(!live){
			tc.missiles.remove(this);
			return;
		}
		
		Color c = g.getColor();
		if(good) {
			g.setColor(Color.red);
		} else g.setColor(Color.black);
		g.fillOval(x, y,WIDTH, HEIGHT);
		g.setColor(c);
		
		move();
	}

	private void move() {
		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;
		case STOP:
			break;
		}
		
		if(x < 0 || y < 0 || x > TankClient.GAME_WIDTH || y > TankClient.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(this.live && this.getRect().intersects(t.getRect()) && t.isLive() && this.good != t.isGood()) {
			if(t.isGood()) {
				t.setLife(t.getLife() - 20);
				if(t.getLife() <= 0) t.setLive(false);
			} else t.setLive(false);
			this.live = false;
			Explode e =new Explode(x,y,tc);
			tc.explodes.add(e);
			return true;
		}
		return false;
	}
	
	public boolean hitTanks(List<Tank> enemytanks) {
		for(int i=0;i<enemytanks.size();i++) {
			if(hitTank(enemytanks.get(i))) {
				return true;
			}
		}
		return false;
	}
	
	public boolean hitWall(Wall w) {
		if(this.getRect().intersects(w.getRect())) {
			this.live = false;
			Explode e =new Explode(x,y,tc);
			tc.explodes.add(e);
			return true;
		}
		return false;
	}
	
	public boolean hithome(MyHome mh) {
		if(this.live && mh.isLife() && this.getRect().intersects(mh.getRect())) {
			this.live = false;
			mh.setLife(false);
			Explode e =new Explode(x,y,tc);
			tc.explodes.add(e);
			return true;
		}
		return false;
	}
	
	public boolean hitProtectWall(ProtectWall pw) {
		if(this.live && pw.isLife() && this.getRect().intersects(pw.getRect())) {
			this.live = false;
			pw.setLife(false);
			Explode e =new Explode(x,y,tc);
			tc.explodes.add(e);
			return true;
		}
		return false;
	}
}

⌨️ 快捷键说明

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