java写飞机大战三

来源:互联网 发布:ip网络功放怎么用 编辑:程序博客网 时间:2024/04/29 16:16

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

  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();}}}

先到这里。

0 0
原创粉丝点击