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

📄 missile.java

📁 纯java编写的坦克大战游戏.把项目引入eclipse后立即可运行。在此基础上可进行二次开发哦。
💻 JAVA
字号:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.List;


public class Missile {
	int x, y;
	Direction dir;
	boolean live = true;
	boolean good;
	TankClient tc;
	
	public static final int WIDTH = 10;
	public static final int HEIGHT = 10;
	public static final int XSPEED = 10;
	public static final int YSPEED = 10;
	
	public Missile(int x, int y, Direction dir, boolean good, TankClient tc) {
		this.x = x;
		this.y = y;
		this.dir = dir;
		this.good = good;
		this.tc = tc;
	}
	
	public void draw(Graphics g) {
		
		if(!live) {
			tc.missiles.remove(this);
			return;
		}
		
		this.collidesWithWall(tc.w1);
		this.collidesWithWall(tc.w2);
		
		collidesWithMissiles(tc.missiles);
		
		hitTanks(tc.tanks);
		hitTank(tc.myTank);
		
		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;
		default:
			break;
		}
		
		if(x < 0 || y < 0 || x > TankClient.GAME_WIDTH || y > TankClient.GAME_HEIGHT) {
			live = false;
		}
	}
	
	public boolean hitTank(Tank t) {
		if(t.live && this.good != t.good && this.getRect().intersects(t.getRect())) {
			if(!t.good)
				t.live = false;
			else {
				t.life -= 20;
				if(t.life <= 0) t.live = false;
			}
			
			live = false;
			Explode e = new Explode(x, y, tc);
			tc.explodes.add(e);
			return true;
		}
		return false;
	}
	
	private boolean hitTanks(List<Tank> tanks) {
		for(int i=0; i<tanks.size(); i++) {
			Tank t = tanks.get(i);
			if(hitTank(t)) {
				return true;
			}
		}
		return false;
	}
	
	private void collidesWithWall(Wall w) {
		if(this.getRect().intersects(w.getRect())) {
			this.live = false;
			tc.explodes.add(new Explode(x, y, tc));
		}
	}
	
	private boolean collidesWithMissile(Missile m) {
		if(this != m && this.good != m.good && this.getRect().intersects(m.getRect())) {
			this.live = false;
			m.live = false;
			tc.explodes.add(new Explode(x, y, tc));
			return true;
		}
		return false;
	}
	
	private void collidesWithMissiles(List<Missile> missiles) {
		for(int i=0; i<missiles.size(); i++) {
			if(this.collidesWithMissile(missiles.get(i))) {
				return;
			}
		}
	}
	
	public Rectangle getRect() {
		return new Rectangle(x, y, WIDTH, HEIGHT);
	}
}

⌨️ 快捷键说明

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