JAVA小游戏——飞机大战

游戏简介

还记得风靡一时的飞机大战游戏么,不如现在用Java上手写一个玩 :D
操作简易,鼠标控制英雄机的移动。
击毁不同种类的飞行物会得到相应分数,击落蜜蜂可能加命或者由单发子弹变为双发。
运行页面如图(单击页面开始游戏)
JAVA小游戏——飞机大战_第1张图片
游戏素材:戳这里

游戏结构

World主类:包含游戏框架并在main方法中实现各种功能。
FlyingObject类:所有飞行物都继承于此类。
HeroPlane类:设计英雄机的属性和方法。
BigAirplane、SmallAirplane类:敌机的属性和方法。
Bee类:蜜蜂的属性和方法。
Sky类:游戏背景的移动。
Bullet类:子弹的属性和方法。

游戏源码

World类

import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.util.Arrays;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class World extends JPanel{
    public static final int WIDTH = 400;
    public static final int HEIGHT = 700;
    private Sky sky = new Sky();
    private HeroPlane hero = new HeroPlane();
    private FlyingObject[] enemies = {};
    private Bullet[] bullets = {};

    public static final int START = 0;
    public static final int RUNNING = 1;
    public static final int PAUSE = 2;
    public static final int GAME_OVER = 3;
    private int state = START;
    //状态页面图片初始化
    private static BufferedImage start;
    private static BufferedImage pause;
    private static BufferedImage gameover;
    static{
        start = FlyingObject.loadImage("start.png");
        pause = FlyingObject.loadImage("pause.png");
        gameover = FlyingObject.loadImage("gameover.png");
    }

    //创建随机敌人对象
    public FlyingObject nextOne(){
        Random rand = new Random();
        int type = rand.nextInt(20);
        if(type<4){
            return new Bee(60,50);
        }else if(type<15){
            return new SmallAirplane(49,36);
        }else{
            return new BigAirplane(66,99);
        }
    }

    //限定敌人入场间隔时间
    int enterIndex = 0;
    int shootIndex2 = 0;
    public void enterAction(){
        enterIndex++;
        if(enterIndex%40==0){
            FlyingObject obj = nextOne();

            enemies = Arrays.copyOf(enemies, enemies.length+1);
            enemies[enemies.length-1] = obj;
        }
    }

    //实现飞行物走步
    public void stepAction(){
        sky.step();
        for(int i=0;ifor(int i=0;i//实现子弹入场
    int shootIndex = 0;
    public void shootAction(){
        shootIndex++;
        if(shootIndex%30==0){
            Bullet[] bs = hero.shoot();
            bullets = Arrays.copyOf(bullets,bullets.length+bs.length);
            System.arraycopy(bs,0,bullets,bullets.length-bs.length,bs.length);
        }
    }

    //删除越界对象
    public void outOfBoundsAction(){
        int index = 0;
        FlyingObject[] enemyLives = new FlyingObject[enemies.length];
        for(int i=0;iif(!e.outOfBounds()&&!e.isRemove()){
                enemyLives[index++] = e;
            }
        }
        enemies = Arrays.copyOf(enemyLives, index);

        index = 0;
        Bullet[] bulletLives = new Bullet[bullets.length];
        for(int i=0;iif(!b.outOfBounds()&&!b.isRemove()){
                bulletLives[index++] = b;
            }
        }
        bullets = Arrays.copyOf(bulletLives, index);
    }

    //根据击中的敌人类型判断奖励类型
    int score = 0;
    public void bangAction(){
        for(int i=0;ifor(int j=0;jif(b.isLife()&&f.isLife()&&b.hit(f)){
                    b.goDead();
                    f.goDead();
                    if(f instanceof SmallAirplane){
                        SmallAirplane e = (SmallAirplane)f;
                        score = score + e.getScore();
                    }else if(f instanceof BigAirplane){
                        BigAirplane e = (BigAirplane)f;
                        score = score + e.getScore();
                    }else if(f instanceof Bee){
                        Bee e = (Bee)f;
                        int type = e.getAwardType();
                        switch(type){
                        case Bee.ADD_FIRE:
                            hero.addFire();
                            break;
                        case Bee.ADD_LIFE:
                            hero.addLife();
                            break;
                        }
                    }
                }
            }

        }
    }

    //英雄机与敌人的碰撞
    public void hitAction(){
        for(int i=0;iif(f.isLife()&&hero.isLife()&&f.hit(hero)){
                f.goDead();
                hero.reduceLife();
                hero.clearFire();
                break;
            }
        }
    }

    //判断游戏是否结束
    public void checkGameOverAction(){
        if(hero.getLife()<=0){
            state = GAME_OVER;
        }
    }

    //设置定时器执行
    public void action(){
        //添加鼠标移动事件的侦听
        MouseAdapter m = new MouseAdapter(){
            public void mouseMoved(MouseEvent e){
                if(state==RUNNING){
                    int x = e.getX();
                    int y = e.getY();
                    hero.moveTo(x, y);
                }
            }
            //鼠标点击事件的侦听
            public void mouseClicked(MouseEvent e){
                switch(state){
                case START:
                    state = RUNNING;
                    break;
                case GAME_OVER:
                    score = 0;
                    hero = new HeroPlane();
                    enemies = new FlyingObject[0];
                    bullets = new Bullet[0];
                    state = START;
                    break;
                }
            }
            //鼠标移出事件的侦听
            public void mouseExited(MouseEvent e){
                if(state==RUNNING){
                    state = PAUSE;
                }
            }
            //鼠标移入事件的侦听
            public void mouseEntered(MouseEvent e){
                if(state==PAUSE){
                    state = RUNNING;
                }
            }
        };
        this.addMouseListener(m);
        this.addMouseMotionListener(m);

        Timer timer = new Timer();
        int interval = 10;
        timer.schedule(new TimerTask(){
            public void run(){
                if(state==RUNNING){
                    enterAction();
                    stepAction();
                    shootAction();
                    outOfBoundsAction();
                    bangAction();
                    hitAction();
                    checkGameOverAction();
                }
                repaint();
            }

        },interval,interval);

    }
    //画出对象
        public void paint(Graphics g){
            sky.paint(g);
            hero.paint(g);
            for(int i=0;ifor(int i=0;i//画出score和life的值
            g.drawString("SCORE:"+score,10,20);
            g.drawString("LIFE:"+hero.getLife(),10,40);
            //画出状态图
            switch(state){
            case START:
                g.drawImage(start,-10,-30,null);
                break;
            case PAUSE:
                g.drawImage(pause,-10,0,null);
                break;
            case GAME_OVER:
                g.drawImage(gameover,-10,0,null);
                break;
            }
        }

    //主方法
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        World world = new World();
        frame.add(world);

        frame.setSize(WIDTH, HEIGHT);
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        world.action();

    }   

}

FlyingObject类

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.imageio.ImageIO;

public abstract class FlyingObject {
    protected int width;
    protected int height;
    protected int x;
    protected int y;

    public static final int LIFE = 0;
    public static final int DEAD = 1;
    public static final int REMOVE = 2;
    protected int state = LIFE;

    public FlyingObject(){}

    public FlyingObject(int width,int height){
        this.width = width;
        this.height = height;
        Random r = new Random();
        x = r.nextInt(World.WIDTH-this.width+1);
        y = -this.height;
    }
    //定义导入图片的方法
    public static BufferedImage loadImage(String fileName){
        try{
            BufferedImage img = ImageIO.read(FlyingObject.class.getResource(fileName));
            return img;
        }catch(Exception e){
            e.printStackTrace();
            throw new RuntimeException();
        }
    }
    //获取图像的抽象方法
    public abstract BufferedImage getImage();

    public void paint(Graphics g){
        g.drawImage(getImage(), x, y, null);
    }
    //判断飞行物状态
    public boolean isLife(){
        return state == LIFE;
    }
    public boolean isDead(){
        return state == DEAD;
    }
    public boolean isRemove(){
        return state == REMOVE;
    }

    public void goDead(){
        state = DEAD;
    }
    //飞行物走步的抽象方法
    public abstract void step();

    //判断对象是否越界的抽象方法
    public abstract boolean outOfBounds();

    //判断飞行物之间是否发生碰撞
    public boolean hit(FlyingObject other){
        int x1 = this.x - other.width;
        int x2 = this.x + this.width;
        int y1 = this.y - other.height;
        int y2 = this.y + this.height;
        int x = other.x;
        int y = other.y;

        return x>=x1 && x<=x2 && y>=y1 && y<=y2;
    }
}

HeroPlane类

import java.awt.image.BufferedImage;
import java.util.Arrays;

public class HeroPlane extends FlyingObject{
    //导入英雄机的图像
    private static BufferedImage[] images;
    static{
        images = new BufferedImage[6];
        for(int i=0;i"hero"+i+".png");
        }
    }
    private int life;
    private int fire;

    /*
     *构造方法 
     */
    public HeroPlane(){
        width = 97;
        height = 124;
        x = 140;
        y = 400;
        life = 3;
        fire = 0;
    }

    public void step(){

    }
    //英雄机随鼠标移动
    public void moveTo(int x,int y){
        this.x = x-this.width/2;
        this.y = y-this.height/2;
    }

    //获取英雄机在不同状态下的图像
    int index = 0;
    int deadIndex = 2;
    public BufferedImage getImage(){
        if(isLife()){
            return images[index++%2];
        }else if(isDead()){
            BufferedImage img = images[deadIndex++];
            if(deadIndex==images.length){
                state = REMOVE;
            }
            return img;
        }
        return null;
    }
    //控制子弹的位置和火力
    public Bullet[] shoot(){
        int xStep = this.width/4;
        int yStep = 20;
        if(fire>0){
            Bullet[] bs = new Bullet[2];
            bs[0] = new Bullet(this.x+1*xStep,this.y-yStep);
            bs[1] = new Bullet(this.x+3*xStep,this.y-yStep);
            fire-=2;
            return bs;
        }else{
            Bullet[] b = new Bullet[1];
            b[0] = new Bullet(this.x+2*xStep,this.y-yStep);
            return b;
        }
    }

    public boolean outOfBounds(){
        return false;
    }

    public void addLife(){
        life++;
    }

    public void addFire(){
        fire += 40;
    }

    public int getLife(){
        return life;
    }

    public void reduceLife(){
        life--;
    }

    public void clearFire(){
        fire = 0;
    }
}

BigAirplane类

import java.awt.image.BufferedImage;

public class BigAirplane extends FlyingObject{
    //导入大敌机的图像
    private static BufferedImage[] images;
    static{
        images = new BufferedImage[5];
        for(int i=0;i"bigplane"+i+".png");
        }
    }
    private int speed;

    public BigAirplane(int width,int height){
        super(width,height);
        speed = 2;
    }

    //大敌机走步
    public void step(){
        y += speed;
    }

    //获取大敌机在不同状态下的图像
    int deadIndex = 1;
    public BufferedImage getImage(){
        if(isLife()){
            return images[0];
        }else if(isDead()){
            BufferedImage img = images[deadIndex++];
            if(deadIndex==images.length){
                state = REMOVE;
            }
            return img;
        }
        return null;
    }
    //判断大敌机是否已越界
    public boolean outOfBounds(){
        return this.y>=World.HEIGHT;
    }

    public int getScore(){
        return 3;
    }

    public Bullet[] shoot(){
        int yStep = 20;
        Bullet[] b = new Bullet[1];
        b[0] = new BulletFromEnemy(this.x+this.width/2,this.y+yStep);
        return b;
    }

}

SmallAirplane类

import java.awt.image.BufferedImage;

public class SmallAirplane extends FlyingObject{
    //导入小敌机的图像
    private static BufferedImage[] images;
    static{
        images = new BufferedImage[5];
        for(int i=0;i"airplane"+i+".png");
        }
    }
    int speed;

    SmallAirplane(int width,int height){
        super(width,height);
        speed = 2;
    }
    //小敌机走步
    public void step(){
        y += speed;
    }

    //获取小敌机在不同状态下的图像
    int deadIndex = 1;
    public BufferedImage getImage(){
        if(isLife()){
            return images[0];
        }else if(isDead()){
            BufferedImage img = images[deadIndex++];
            if(deadIndex==images.length){
                state = REMOVE;
            }
            return img;
        }
        return null;
    }
    //判断小敌机是否已越界
    public boolean outOfBounds(){
        return this.y>=World.HEIGHT;
    }

    public int getScore(){
        return 5;
    }
}

Bee类

import java.awt.image.BufferedImage;
import java.util.Random;

public class Bee extends FlyingObject{
    //导入小蜜蜂的图像
    private static BufferedImage[] images;
    static{
        images = new BufferedImage[5];
        for(int i=0;i"bee"+i+".png");
        }
    }
    private int xSpeed;
    private int ySpeed;
    private int awardType;
    public static final int ADD_FIRE = 0;
    public static final int ADD_LIFE = 1;

    public Bee(int width,int height){
        super(width,height);
        xSpeed = 1;
        ySpeed = 2;
        Random r = new Random();
        awardType = r.nextInt(2);
    }

    //小蜜蜂走步
    public void step(){
        x += xSpeed;
        y += ySpeed;
        if(x>=World.WIDTH-this.width||x<=0){
            xSpeed = (-1)*xSpeed;
        }
    }

    //获取小蜜蜂在不同状态下的图像
    int deadIndex = 1;
    public BufferedImage getImage(){
        if(isLife()){
            return images[0];
        }else if(isDead()){
            BufferedImage img = images[deadIndex++];
            if(deadIndex==images.length){
                state = REMOVE;
            }
            return img;
        }
        return null;
    }
    //判断小蜜蜂是否已越界
    public boolean outOfBounds(){
        return this.y>=World.HEIGHT;
    }

    public int getAwardType(){
        return this.awardType;
    }
}

Sky类

import java.awt.Graphics;
import java.awt.image.BufferedImage;

public class Sky extends FlyingObject{
    //导入天空的图像
    private static BufferedImage image;
    static{
        image = loadImage("background.png");
    }
    private int speed;
    private int y1;

    public Sky(){
        width = World.WIDTH;
        height = World.HEIGHT;
        x = 0;
        y = 0;
        y1 = -this.height;
        speed = 1;
    }
    //天空走步
    public void step(){
        y += speed;
        y1 += speed;
        if(y>=this.height){
            y = -this.height;
        }
        if(y1>=this.height){
            y1 = -this.height;
        }
    }
    //获取天空的图像
    public BufferedImage getImage(){
        return image;
    }

    public void paint(Graphics g){
        g.drawImage(getImage(), x, y, null);
        g.drawImage(getImage(), x, y1, null);
    }

    public boolean outOfBounds(){
        return false;
    }
}

Bullet类

import java.awt.image.BufferedImage;

public class Bullet extends FlyingObject{
    //导入子弹的图像
    private static BufferedImage image;
    static{
        image = loadImage("bullet.png");
    }
    private int speed;

    public Bullet(int x,int y){
        width = 8;
        height = 14;
        this.x = x;
        this.y = y;
        speed = 3;
    }
    //子弹走步
    public void step(){
        y -= speed;
    }
    //获取子弹的图像
    public BufferedImage getImage(){
        if(isLife()){
            return image;
        }
        if(isDead()){
            state = REMOVE;
        }
        return null;
    }
    //判断子弹是否已越界
    public boolean outOfBounds(){
        return this.y<=-this.height;
    }
}

你可能感兴趣的:(JAVA)