java写飞机大战四

先实现MainPlane,我放飞机。

public class MainPlane implements GameRole {
	// 是否活着
	private boolean live;
	// 状态(无敌状态||三弹齐发状态||正常状态)
	private MainPlaneState state;

	// 三个不同的状态
	private StrongState strongState;
	private NormalState normalState;
	private TripleShootState tripleShootState;

	// 游戏客户端
	private GameObserver observer;

	// 当前的X和Y
	private int currentX, currentY;
	// 半径
	private int radius;

	// 默认是活着的
	public MainPlane(int currentX, int currentY, GameObserver observer) {
		this.currentX = currentX;
		this.currentY = currentY;
		this.radius = GameConstant.MainPlaneData.RADIUS;
		this.live = true;
		this.observer = observer;
		this.strongState = new StrongState(this);
		this.normalState = new NormalState(this);
		this.tripleShootState = new TripleShootState(this);
		this.state = normalState;
	}

	// 出现在屏幕上
	public void appears(Graphics g) {
		if (isLive()) {
			Color preColor = g.getColor();
			g.setColor(GameConstant.ColorHelper.MAIN_PALNE_COLOR);
			g.fillOval(currentX, currentY, radius, radius);
			g.setColor(preColor);
		}
	}

	// 按下上下左右来移动
	// 游戏的设定,到了左||右边缘再继续向左||右,会出现在右||左边缘,但是上下不行。
	public void onkeyPressed(KeyEvent keyEvent) {
		int keyCode = keyEvent.getKeyCode();
		switch (keyCode) {
		case KeyEvent.VK_LEFT:
			if (currentX == 0) {
				currentX += GameConstant.FrameData.WIN_WIDTH;
			}
			currentX -= GameConstant.MainPlaneData.SPEED;
			break;
		case KeyEvent.VK_UP:
			if (currentY == GameConstant.MainPlaneData.RADIUS) {
				return;
			} else {
				currentY -= GameConstant.MainPlaneData.SPEED;
			}
			break;
		case KeyEvent.VK_RIGHT:
			if (currentX == GameConstant.FrameData.WIN_WIDTH) {
				currentX -= GameConstant.FrameData.WIN_WIDTH;
			}
			currentX += GameConstant.MainPlaneData.SPEED;
			break;
		case KeyEvent.VK_DOWN:
			if (currentY == GameConstant.FrameData.WIN_HEIGHT
					- GameConstant.MainPlaneData.RADIUS) {
				return;
			} else {
				currentY += GameConstant.MainPlaneData.SPEED;
			}
			break;
		default:
			break;
		}
	}

	// 以下是重写GameRole接口的方法

	// 获取范围,以进行碰撞检测
	public Rectangle getRect() {
		return new Rectangle(currentX, currentY, radius, radius);
	}

	// 我方飞机死亡
	public void godie() {
		this.live = false;
		observer.onMainPlaneDie();
	}

	// 我方飞机没有自动移动的动作
	public void move() {
		return;
	}

	// 我方飞机没有减血操作
	public void discreaseBlood(int discreaseValue) {
		return;
	}

	// 我方飞机撞上敌方飞机,直接死亡
	public void hit(GameRole gameRole) {
		state.hit(gameRole);
	}

	//...getter and setter省略

	@Override
	public int getAttackValue() {
		// TODO Auto-generated method stub
		return 0;
	}
}

子弹的实现:getter和setter省略

public class Bullet implements GameRole {
	private boolean isLive;
	private int radius;
	private int currentX;
	private int currentY;
	private int speed;
	private int attackValue;
	private GameObserver observer;

	public void paint(Graphics g) {
		if (isLive) {
			Color preColor = g.getColor();
			g.setColor(GameConstant.ColorHelper.MISSLECOLOR);
			g.fillOval(currentX, currentY, radius, radius);
			g.setColor(preColor);
		}
	}

	public Bullet(int currentX, int currentY, GameObserver observer) {
		this.currentX = currentX;
		this.currentY = currentY;
		this.observer = observer;
		this.isLive = true;
		this.radius = GameConstant.BulletData.RADIUS;
		this.speed = GameConstant.BulletData.SPEED;
		this.attackValue = GameConstant.BulletData.ATTACKVALUE;
	}

	/*----------------------------接口方法----------------------------*/
	// 获取范围以进行碰撞检测
	public Rectangle getRect() {
		return new Rectangle(currentX, currentY, radius, radius);
	}

	// 子弹消失
	public void godie() {
		this.isLive = false;
		observer.onBulletDisapper(this);
	}

	// 游戏角色撞击之后,都进行减血的操作
	public void hit(GameRole gameRole) {
		if (this.getRect().intersects(gameRole.getRect())) {
			gameRole.discreaseBlood(attackValue);
			godie();
		}
	}

	// 子弹的移动
	public void move() {
		currentY -= GameConstant.BulletData.SPEED;
		if (currentY < 0) {
			godie();
		}
	}

	// 子弹减血就是直接死亡
	public void discreaseBlood(int discreaseValue) {
		godie();
	}

	// 获取攻击力
	public int getAttackValue() {
		return attackValue;
	}
}

敌方飞机的实现:省略getter和setter

public class NormalPcPlane implements GameRole {

	private int currentX, currentY;
	// 血值
	private int bloodValue;
	// 半径
	private int radius;
	// 是否活着
	private boolean isLive;

	private GameObserver observer;

	public NormalPcPlane(GameObserver observer) {
		this.observer = observer;
		this.currentX = GameController.createX();
		this.currentY = GameController.createY();
		this.radius = GameConstant.NormalPcPlaneData.RADIUS;
		this.bloodValue = GameConstant.NormalPcPlaneData.BLOOD;
		this.isLive = true;
	}

	// 出现在屏幕上
	public void paint(Graphics g) {
		if (isLive) {
			Color preColor = g.getColor();
			g.setColor(GameConstant.ColorHelper.PCNORMALCOLOR);
			g.fillOval(currentX, currentY, radius, radius);
			g.setColor(preColor);
		}
	}

	// 获取所在范围以进行碰撞检测
	public Rectangle getRect() {
		return new Rectangle(currentX, currentY, radius, radius);
	}

	// 敌方飞机死亡,会通知observer做出相应的处理
	public void godie() {
		this.isLive = false;
		observer.onNormalPcPlaneDie(this);
	}

	// 普通敌方飞机的减血操作
	public void discreaseBlood(int discreaseValue) {
		bloodValue -= discreaseValue;
		if (bloodValue <= 0) {
			godie();
		}
	}

	// 敌方飞机自动向下移动的操作
	public void move() {
		currentY += GameConstant.NormalPcPlaneData.INIT_SPEED;
		if (currentY > GameConstant.FrameData.WIN_HEIGHT) {
			godie();
		}
	}

	// 敌方飞机的碰撞检测
	public void hit(GameRole gameRole) {
		if (getRect().intersects(gameRole.getRect())) {
			godie();
		}
	}

	@Override
	public int getAttackValue() {
		// TODO Auto-generated method stub
		return 0;
	}
}

BOSS飞机在逻辑上和普通飞机略有差别,就不贴了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值