missile.java

来自「坦克大战游戏」· Java 代码 · 共 94 行

JAVA
94
字号
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
public class Missile {
	public static final int XSPEED=10;
	public static final int YSPEED=10;
	int x,y;
	Tank.Direction dir;
	private boolean live=true;
	private TankWar tw=null;
	public Missile(int x, int y,Tank.Direction dir,TankWar tw) {
		this.x = x;
		this.y = y;
		this.dir = dir;
		this.tw=tw;
	}	
	
	public void draw(Graphics g) 
	{	
		Color c=g.getColor();
		g.setColor(Color.black);
		g.fillOval(x, y, 10,10);
		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;
		} 
		if(this.x>0&&this.x<800&&this.y>0&&this.y<600)
			this.setLive(true);
		else
			this.setLive(false);
		
	}

	public boolean isLive() {
		return live;
	}
	
	public Rectangle getRect()
	{
		return new Rectangle(x,y,10,10);
		
	}
	
	public boolean HitTank(Tank t)
	{
		if(this.getRect().intersects(t.getRect())&&t.isLive())
		{
			t.setLive(false);
			this.setLive(false);
			Explode e = new Explode(x, y, tw);
			tw.explodes.add(e);
			return true;
		}
		return false;
	}

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

⌨️ 快捷键说明

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