missile.java

来自「J2SE坦克大战源码 分为24个版本」· Java 代码 · 共 111 行

JAVA
111
字号
import java.awt.*;
import java.util.List;

public class Missile {
	
	int x;
	int y;
	MyTank.Direction dir;
	public static final int XSPEED = 10;
	public static final int YSPEED = 10;
	public static final int WI = 10;
	public static final int HE = 10;
	private boolean live = true;
	private boolean good;
	
	TankClient tc = null;
	
	public Missile(int x, int y,boolean good, MyTank.Direction dir,TankClient tc) {
		this.x = x;
		this.y = y;
		this.good = good;
		this.dir = dir;
		this.tc = tc;
	}
	
	public void draw(Graphics g){
		if(!live)return;
		
		Color c = g.getColor();
		g.setColor(Color.BLACK);
		g.fillOval(x, y, 11, 11);
		g.setColor(c);				
		move();
	}
	
	public void move(){	
		switch(dir){
		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 L:
			x -= XSPEED;
		break;
		case LU:
			x -= XSPEED;
			y -= YSPEED;
		break;
		case STOP:		
		break;
		
		}	
		
		if( x<0 || y< 0 || x > TankClient.GAME_WEIGHT || y>TankClient.GAME_HEIGHT){
			live = false;
		}
	}
	
	public Rectangle getRect(){
		return new Rectangle(x,y,WI,HE);
	}
	
	public boolean hitTank(MyTank t){
		if(this.live && this.getRect().intersects(t.getRect()) && t.isLive() && this.good != t.isGood()){
			t.setLive(false);
			Explode ep = new Explode(x,y,tc);
			tc.explodes.add(ep);
			setLive(false);
			return true;
		}
			return false;
	}
	
	public boolean hitTanks(List<MyTank> tanks){
		for(int i=0;i < tanks.size();i++){
		if(hitTank(tanks.get(i))){
			return true;
		}
		}
			return false;
		
	}

	public boolean isLive() {
		return live;
	}

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

	
}

⌨️ 快捷键说明

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