一、注意事项:用了三个接口Award、Enemy、ShootBang
接口1:
/**打掉蜜蜂得奖励*/
public interface Award {
int DOUBLEFIRE=0;
int LIFE=1;
int getType();
}
接口2:
//打掉敌机得分
public interface Enemy {
int getScoer();
}
接口3:
//是否被子弹击中
public interface ShootBang {
boolean shootBy(Bullet b);
}
1.父类-----FlyingObject
import java.awt.image.BufferedImage;
public abstract class FlyingObject {
protected BufferedImage image;
protected int width;
protected int height;
protected int x;
protected int y;
//走步的抽象方法
public abstract void step();
//越界判断
public abstract boolean isoutOfBounds();
}
2.子弹-----Bullet
public class Bullet extends FlyingObject{
private int speed=3;
//构造方法
public Bullet(int x,int y){//传入的参数是英雄机的x y坐标
image=ShootGame.bullet;
width=image.getWidth();
height=image.getHeight();
this.x=x;
this.y=y;
}
@Override//子弹走步
public void step() {
y -= speed;
}
@Override/**子弹越界方法重写*/
public boolean isoutOfBounds() {
if(this.y<-this.height){
return true;
}
return false;
}
}
3.蜜蜂-----Bee
import java.util.Random;
public class Bee extends FlyingObject implements Award,ShootBang{
private int xspeed=1;
private int yspeed=2;
//构造方法
public Bee(){
image=ShootGame.bee;
width=image.getWidth();
height=image.getHeight();
Random rand = new Random();
x=rand.nextInt(ShootGame.WIDTH-this.width);
y=-height;
}
@Override/**获得奖励两种情况????*/
public int getType() {
Random rand = new Random();
return rand.nextInt(2);
}
@Override/**蜜蜂走步*/
public void step() {
x += xspeed;
y += yspeed;
if(x>=(ShootGame.WIDTH-this.width)){
xspeed=-xspeed;
}
if(x<=0){
xspeed=-xspeed;
}
}
@Override/**是否被子弹击中*/
public boolean shootBy(Bullet b) {
if(b.x>this.x && b.x
this.y && b.y
ShootGame.HEIGHT){
return true;
}
return false;
}
}
4.敌机-----Airplane
import java.util.Random;
public class Airplane extends FlyingObject implements Enemy,ShootBang{
private int speed=3;
//构造方法
public Airplane(){
image=ShootGame.airplane;
width=image.getWidth();
height=image.getHeight();
Random rand = new Random();
x=rand.nextInt(ShootGame.WIDTH-this.width);
y=-height;
}
@Override/**打掉一个敌机得5分*/
public int getScoer() {
return 5;
}
@Override/**敌机走步*/
public void step() {
y += speed;
}
@Override/**判断是否被子弹击中*/
public boolean shootBy(Bullet b) {
if(b.x>this.x && b.x
this.y && b.y
ShootGame.HEIGHT){
return true;
}
return false;
}
}
5.英雄机-----Hero
import java.awt.image.BufferedImage;
public class Hero extends FlyingObject{
private int doubleFire;
private int life;
private BufferedImage[] images; //帮助图片切换
private int index; //帮助图片切换
//构造方法
public Hero(){
image=ShootGame.hero0;
width=image.getWidth();
height=image.getHeight();
x=150;
y=350;
doubleFire=0;
life=3;
index=0;
images=new BufferedImage[]{ShootGame.hero0,ShootGame.hero1};
}
@Override/**英雄机走步是图片200毫秒切换一次*/
public void step() {
int num=(index++)/10%(images.length);
image=images[num];
}
//使侦听的坐标和图片的坐标匹配(默认匹配左上角)
public void moveTo(int x,int y){
this.x=x-this.width/2;
this.y=y-this.height/2;
}
//发射子弹--分两种情况
public Bullet[] shoot(){
if(doubleFire>0){
Bullet[] bs = new Bullet[2];
int x=this.width/4;
bs[0]=new Bullet(this.x+x,this.y-20);
bs[1]=new Bullet(this.x+3*x,this.y-20);
doubleFire -= 2;
return bs;
}else{
Bullet[] bs = new Bullet[1];
int x=this.width/4;
bs[0]=new Bullet(this.x+2*x,this.y-20);
return bs;
}
}
public int getLife(){
return life;
}
//加命
public void addLife(){
life++;
}
//双倍火力
public void addDoubleFire(){
doubleFire += 40;
}
@Override/**判断是否越界 方法重写*/
public boolean isoutOfBounds() {
return false;
}
//英雄机碰撞
public boolean checkBang(FlyingObject other){
int heroX=this.x+this.width/2;
int heroY=this.y+this.height/2;
int x1=other.x-this.width/2;
int x2=other.x+other.width+this.width/2;
int y1=other.y-this.height/2;
int y2=other.y+other.height+this.height/2;
if(heroX>x1 && heroX
y1 && heroY
6.主类-----ShootGame
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Arrays;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ShootGame extends JPanel{
//静态属性 9个
public static BufferedImage background;
public static BufferedImage gameover;
public static BufferedImage pause;
public static BufferedImage start;
public static BufferedImage bullet;
public static BufferedImage hero1;
public static BufferedImage hero0;
public static BufferedImage bee;
public static BufferedImage airplane;
//窗口的尺寸常量
public static final int WIDTH=400;
public static final int HEIGHT=654;
public static final int START=0;
public static final int PAUSE=1;
public static final int GAME_OVER=2;
public static final int RUNNING=3;
private int state;//当前状态
private Bullet[] bullets;
private Hero hero;
private FlyingObject[] flyings;
//构造方法
public ShootGame(){//初始化对象,其中子弹和敌机蜜蜂都是零个
bullets = new Bullet[0];
hero = new Hero();
flyings = new FlyingObject[0];
}
//加载静态资源
static{
try {
background=ImageIO.read(ShootGame.class.getResource("background.png"));
gameover=ImageIO.read(ShootGame.class.getResource("gameover.png"));
pause=ImageIO.read(ShootGame.class.getResource("pause.png"));
start=ImageIO.read(ShootGame.class.getResource("start.png"));
bullet=ImageIO.read(ShootGame.class.getResource("bullet.png"));
hero1=ImageIO.read(ShootGame.class.getResource("hero1.png"));
hero0=ImageIO.read(ShootGame.class.getResource("hero0.png"));
bee=ImageIO.read(ShootGame.class.getResource("bee.png"));
airplane=ImageIO.read(ShootGame.class.getResource("airplane.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
/**paint方法*/
public void paint(Graphics g){
/**画背景*/
g.drawImage(background, 0, 0, null);
/**画子弹*/
paintBullet(g);
/**画蜜蜂和敌机*/
paintFlyings(g);
/**画英雄机*/
g.drawImage(hero.image, hero.x, hero.y, null);
/**画分和命*/
paintScore(g);
/**画状态*/
paintState(g);
}
/**画子弹*/
public void paintBullet(Graphics g){
for(int i=0;i