java写飞机大战三

前文有提到,我放飞机可以吃到三种技能,也就是有三种不同的状态。

  1. 无敌技能
  2. 三射技能
  3. 变弱技能
而我方飞机又是游戏角色之一,所以也具备基本的游戏角色的

  1. 获取范围,以进行碰撞检测
  2. 死亡
  3. 移动
  4. 减血
  5. 碰撞
  6. 获取攻击力  
在这里,比如无敌技能不会死,但是没有无敌技能的话,一撞就死。
所以,在不同的状态下,碰撞检测之后的操作就是不一样的。
可以用if else之类的语句去判断,但是如果后续修改游戏,比如多出一种,无敌+三射技能,要加的判断会比较多。

所以这里用到一个设计模式中的状态模式,但是状态较少的时候,代码量反而会交简单的逻辑判断语句多。
还是用一个接口来表示我方飞机的状态。
具体代码:
public interface MainPlaneState {
	// 吃到无敌技能
	public void eatStrong();

	// 吃到三弹齐发技能
	public void eatTriple();

	// 吃到变弱技能
	public void eatWeek();

	// 发射子弹
	public void fire();

	// 和敌方飞机撞击
	public void hit(GameRole gameRole);
}

不同状态的具体实现:
正常状态:

import com.mybeatplane.helper.GameConstant;
import com.mybeatplane.interfase.GameObserver;
import com.mybeatplane.interfase.GameRole;
import com.mybeatplane.role.Bullet;
import com.mybeatplane.role.MainPlane;
import com.mybeatplane.role.NormalPcPlane;

public class NormalState implements MainPlaneState {

	private MainPlane mainPlane;

	public MainPlane getMainPlane() {
		return mainPlane;
	}

	public void setMainPlane(MainPlane mainPlane) {
		this.mainPlane = mainPlane;
	}

	public NormalState(MainPlane mainPlane) {
		this.mainPlane = mainPlane;
	}

	// 正常状态下吃到无敌技能会变无敌
	public void eatStrong() {
		mainPlane.setState(mainPlane.getStrongState());
	}

	// 正常状态下吃到三射技能会变成三射状态
	public void eatTriple() {
		mainPlane.setState(mainPlane.getTripleShootState());
	}

	// 正常状态下吃到变弱技能 不起作用
	public void eatWeek() {
	}

	// 正常状态下子弹是单发的
	public void fire() {
		int currentX = mainPlane.getCurrentX()
				+ (GameConstant.MainPlaneData.RADIUS - GameConstant.BulletData.RADIUS)
				/ 2;
		int currentY = mainPlane.getCurrentY();
		GameObserver observer = mainPlane.getObserver();
		Bullet bullet = new Bullet(currentX, currentY, observer);
		observer.onBulletAppear(bullet);
	}

	// 正常状态下,如果我方飞机撞上对方飞机,我方飞机死亡
	public void hit(GameRole gameRole) {
		if (mainPlane.getRect().intersects(gameRole.getRect())) {
			mainPlane.godie();
		}
	}
}


无敌状态:

import com.mybeatplane.helper.GameConstant;
import com.mybeatplane.interfase.GameObserver;
import com.mybeatplane.interfase.GameRole;
import com.mybeatplane.role.Bullet;
import com.mybeatplane.role.MainPlane;
import com.mybeatplane.role.NormalPcPlane;

public class StrongState implements MainPlaneState {

	private MainPlane mainPlane;

	public MainPlane getMainPlane() {
		return mainPlane;
	}

	public void setMainPlane(MainPlane mainPlane) {
		this.mainPlane = mainPlane;
	}

	public StrongState(MainPlane mainPlane) {
		this.mainPlane = mainPlane;
	}

	// 无敌状态下吃到无敌技能不起作用
	public void eatStrong() {

	}

	// 无敌状态下吃到三射技能不起作用
	public void eatTriple() {

	}

	// 无敌状态下吃到变弱技能,会变成正常状态
	public void eatWeek() {
		mainPlane.setState(mainPlane.getNormalState());
	}

	// 无敌状态下发射子弹是单发的
	public void fire() {
		int currentX = mainPlane.getCurrentX()
				+ (GameConstant.MainPlaneData.RADIUS - GameConstant.BulletData.RADIUS)
				/ 2;
		int currentY = mainPlane.getCurrentY();
		GameObserver observer = mainPlane.getObserver();
		Bullet bullet = new Bullet(currentX, currentY, observer);
		observer.onBulletAppear(bullet);
	}

	// 无敌状态下撞击敌方飞机不受到伤害,敌方飞机死亡
	public void hit(GameRole gameRole) {
		gameRole.godie();
	}
}


三射状态:

import java.util.ArrayList;
import java.util.List;

import com.mybeatplane.helper.GameConstant;
import com.mybeatplane.interfase.GameObserver;
import com.mybeatplane.interfase.GameRole;
import com.mybeatplane.role.Bullet;
import com.mybeatplane.role.MainPlane;
import com.mybeatplane.role.NormalPcPlane;

public class TripleShootState implements MainPlaneState {

	private MainPlane mainPlane;

	public TripleShootState(MainPlane mainPlane) {
		this.mainPlane = mainPlane;
	}

	public MainPlane getMainPlane() {
		return mainPlane;
	}

	public void setMainPlane(MainPlane mainPlane) {
		this.mainPlane = mainPlane;
	}

	// 三射状态吃到无敌技能不起作用
	public void eatStrong() {
	}

	// 三射状态吃到三射技能不起作用
	public void eatTriple() {
	}

	// 三射状态吃到变弱技能 变成正常状态
	public void eatWeek() {
		mainPlane.setState(mainPlane.getNormalState());
	}

	// 三射状态下子弹一次产生三个子弹发射
	public void fire() {
		List<Bullet> newBullets = new ArrayList<Bullet>();
		int currentXOne = mainPlane.getCurrentX()
				+ (GameConstant.MainPlaneData.RADIUS - GameConstant.BulletData.RADIUS)
				/ 2;
		int currentXTwo = mainPlane.getCurrentX()
				- GameConstant.BulletData.RADIUS;
		int currentXThree = mainPlane.getCurrentX()
				+ GameConstant.MainPlaneData.RADIUS;
		int currentY = mainPlane.getCurrentY();
		GameObserver observer = mainPlane.getObserver();
		Bullet bulletOne = new Bullet(currentXOne, currentY, observer);
		Bullet bulletTwo = new Bullet(currentXTwo, currentY, observer);
		Bullet bulletThree = new Bullet(currentXThree, currentY, observer);
		newBullets.add(bulletOne);
		newBullets.add(bulletTwo);
		newBullets.add(bulletThree);
		observer.onBulletsAppear(newBullets);
	}

	// 三射状态下我方飞机撞击到敌方飞机,我方飞机死亡
	public void hit(GameRole gameRole) {
		if (mainPlane.getRect().intersects(gameRole.getRect())) {
			mainPlane.godie();
		}
	}
}

先到这里。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值