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

📄 missile.java

📁 游戏坦克大战java源码,运行TankClient.java进入游戏
💻 JAVA
字号:
import java.awt.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Missile {

	
	private static final int XSPEED = 7;
	private static final int YSPEED = 7;
	private boolean good;
	
	public static final int WIDTH = 10;
	public static final int HEIGHT = 10;
	int x, y;
	Direction dir;

	private boolean live = true;
	private TankClient tc;
	
	private static  Toolkit tk = Toolkit.getDefaultToolkit();
	private static Image[] MisImgs = null;
	private static Map<String,Image> imgs = new HashMap<String,Image>();
	static  { 
		MisImgs = new Image[] {
		tk.getImage(Missile.class.getClassLoader().getResource("images/missileL.gif")),
		tk.getImage(Missile.class.getClassLoader().getResource("images/missileLU.gif")),
		tk.getImage(Missile.class.getClassLoader().getResource("images/missileU.gif")),
		tk.getImage(Missile.class.getClassLoader().getResource("images/missileRU.gif")),
		tk.getImage(Missile.class.getClassLoader().getResource("images/missileR.gif")),
		tk.getImage(Missile.class.getClassLoader().getResource("images/missileRD.gif")),
		tk.getImage(Missile.class.getClassLoader().getResource("images/missileD.gif")),
		tk.getImage(Missile.class.getClassLoader().getResource("images/missileLD.gif")),

		};
		
		imgs.put("L", MisImgs[0]);  imgs.put("LU", MisImgs[1]);
		imgs.put("U", MisImgs[2]);  imgs.put("RU", MisImgs[3]);
		imgs.put("R", MisImgs[4]);  imgs.put("RD", MisImgs[5]);				
		imgs.put("D", MisImgs[6]);  imgs.put("LD", MisImgs[7]);
	}

	public boolean isLive() {
		return live;
	}

	public Missile(int x, int y, Direction dir) {
		this.dir = dir;
		this.x = x;
		this.y = y;
	}

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

	public void draw(Graphics g) {
		if(!live) {
			tc.missiles.remove(this);
			return;
		}
		switch (dir) {
		case L:
			g.drawImage(imgs.get("L"), x, y, null);
			break;
		case LU:
			g.drawImage(imgs.get("LU"), x, y, null);
			break;
		case U:
			g.drawImage(imgs.get("U"), x, y, null);
			break;
		case RU:
			g.drawImage(imgs.get("RU"), x, y, null);
			break;
		case R:
			g.drawImage(imgs.get("R"), x, y, null);
			break;
		case RD:
			g.drawImage(imgs.get("RD"), x, y, null);
			break;
		case D:
			g.drawImage(imgs.get("D"), x, y, null);
			break;
		case LD:
			g.drawImage(imgs.get("LD"), x, y, null);
			break;
		}

		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 (x < 0 || y < 0 || x > TankClient.GAME_WIDTH
				|| y > TankClient.GAME_HEIGHT) {
			live = false;
			tc.missiles.remove(this);
			
		}

	}
	
	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;
			Expolde e = new Expolde(x, y, tc);
			tc.expoldes.add(e);
			return true;
		}
		else 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.live && this.getRect().intersects(w.getRect())) {
			this.live = false;
			return true;
		}
		return false;
			
	}

}









⌨️ 快捷键说明

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