飞机大战 游戏规则:游戏可以产生小的敌机、大的敌机、小蜜蜂,这三类都是随机概率出现的,游戏打开的时候,鼠标单击,游戏开始, 自动发射子弹,英雄机跟随鼠标移动,当鼠标移到窗口外时,游戏暂停,当鼠标又移回时,游戏继续,子弹打中敌机和小蜜 蜂,当生命降到0时,消失,敌机 撞击到英雄机, 英雄机生命值-1, 直到0时, 游戏结束, 小敌机: 分数+,大敌机: 分数+ 奖励(生命值加成, 火力加成),小蜜蜂: 奖励(生命值加成, 火力加成)。
源码地址:https://gitee.com/lxlxlxl233/fly-battle
package com.chinasofti.shoot;
import java.util.Random;
//敌机
public class Airplane extends FlyingObject implements Enemy{
private int speed=2;//走步
/*构造方法*/
public Airplane(){
image=ShootGame.airplane;
width=image.getWidth();
height=image.getHeight();
Random rand =new Random();
x=rand.nextInt(ShootGame.WIDTH-this.width);
y=-this.height;
}
@Override
public int getScore() {
return 5;
}
@Override
public void step() {
y+=speed;
}
@Override
public boolean outOfBounds() {
return this.y>ShootGame.HEIGHT;
}
}
package com.chinasofti.shoot;
//奖励
public interface Award {
public int LIFE=1;//命
public int DOUBLE_FIRE=0;//火力值
/*获取奖励 0:火力值,1:命*/
public int getType();
}
package com.chinasofti.shoot;
import java.util.Random;
//小蜜蜂
public class Bee extends FlyingObject implements Award {
private int xspeed=1;//x轴速度
private int yspeed=2;//y轴速度
private int awardType;
public Bee() {
image=ShootGame.bee;
width=image.getWidth();
height=image.getHeight();
Random rand=new Random();
x=rand.nextInt(ShootGame.WIDTH-this.width);
y=-this.height;
awardType= rand.nextInt(2);
}
@Override
public int getType() {
return awardType;
}
@Override
public void step() {
y+=yspeed;
x+=xspeed;
if(x>ShootGame.WIDTH-this.width){//最右边
xspeed=-1;
}
if(x<0){//最左边
xspeed=1;
}
}
@Override
public boolean outOfBounds() {
return this.y>ShootGame.HEIGHT;
}
}
package com.chinasofti.shoot;
import java.util.Random;
//子弹
public class Bullet extends FlyingObject{
private int speed=3;
public Bullet(int x,int y) {
image=ShootGame.bullet;
width=image.getWidth();
height=image.getHeight();
Random rand =new Random();
this.x=x;//随着英雄机移动
this.y=y;//随着英雄机移动
}
@Override
public void step() {
y-=speed;
}
@Override
public boolean outOfBounds() {
return this.y<-this.height;
}
}
package com.chinasofti.shoot;
//敌人
public interface Enemy {
/*得分*/
public int getScore();
}
package com.chinasofti.shoot;
import java.awt.image.BufferedImage;
//飞行物
public abstract class FlyingObject {
protected BufferedImage image;//图片
protected int width;//长
protected int height;//宽
p