以java语言为工具的粗糙的飞机大战游戏的开发与实现

第一步,导入外部图片的素材库

将找到的素材库存放于src目录下新建的img文件夹中
在这里插入图片描述

第二步,构建窗体和面板

这是java制作游戏老生常谈的第一步了,先制作一个合适大小的窗体GameFrame(这里弄个APP类专门保存Frame的大小数据),作为游戏画面盛放的底座(玻璃窗的木制窗沿),然后做一个GamePanel,作为游戏画面盛放的最底层背景,所有的游戏画面都将在这个背景上面得以展现(玻璃窗的玻璃)。

package ui;

public class APP {
    public static int WIDTH = 512;
    public static int HEIGHT = 802;

    public APP() {
    }
}
package ui;

import java.awt.Component;
import javax.swing.JFrame;

public class GameFrame extends JFrame {
    GamePanel panel = new GamePanel();

    public GameFrame() {
        this.add(this.panel);
        this.setTitle("全民飞机大战 by MT");
        this.setSize(APP.WIDTH, APP.HEIGHT);
        this.setLocationRelativeTo((Component)null);
        this.setDefaultCloseOperation(3);
        this.setResizable(false);
    }
package ui;
public class GamePanel extends JPanel {
	public GamePanel() {
	}
}	

第三步,在GamePanel上面构建背景图画

创建一个背景图片bg对象用于管理GamePanel上面的背景图片,包括利用集合等方法让每次调用画背景图的时候使背景图顺序播放等。

package ui;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;

public class bg {
    BufferedImage img;
    int x;
    int y;
    int w;
    int h;
    List<BufferedImage> bgs = new ArrayList();
    int index = 0;

    public bg() {
        for(int i = 1; i <= 5; ++i) {
            try {
                this.bgs.add(ImageIO.read(this.getClass().getResource("/img/bg" + i + ".jpg")));
            } catch (IOException var3) {
                var3.printStackTrace();
            }

            this.img = (BufferedImage)this.bgs.get(0);
            this.w = this.img.getWidth();
            this.h = this.img.getHeight();
            this.x = 0;
            this.y = 0;
        }

    }

    public void changeImg() {
        ++this.index;
        if (this.index >= 5) {
            this.index = 0;
        }

        this.img = (BufferedImage)this.bgs.get(this.index);
    }
}

package ui;
public class GamePanel extends JPanel {
 	bg bg = new bg();
	public GamePanel() {
	 	public void paint(Graphics g) {
        super.paint(g);
        g.drawImage(this.bg.img, this.bg.x, this.bg.y, this.bg.w, this.bg.h, (ImageObserver)null);
		}
	}	
}

第四步,构建英雄机和敌人的机器,支援机器之类(包括顺便把子弹类也构建完毕了吧(因为英雄机里面是用的上的)),顺便做了一个充满了恶意的不能被打掉的导弹类

package ui;

import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;

public class Bullet {
    BufferedImage img;
    int x;
    int y;
    int w;
    int h;

    public Bullet(int hx, int hy) {
        try {
            this.img = ImageIO.read(this.getClass().getResource("/img/fire.png"));
            this.w = this.img.getWidth() / 3;
            this.h = this.img.getHeight() / 3;
            this.x = hx;
            this.y = hy;
        } catch (IOException var4) {
            var4.printStackTrace();
        }

    }

    public void move() {
        this.y -= 10;
    }
	//这里设置的boss是打一下打不死的,所以得计算击打的次数
    public boolean hitByBoss1(Boss1 b1) {
        int b1x = b1.x;
        int b1y = b1.y;
        int b1w = b1.w;
        int b1h = b1.h;
        boolean hit = false;
        hit = this.x <= b1x + b1w && this.x >= b1x - this.w && this.y >= b1y - this.h && this.y <= b1y + b1h;
        return hit;
    }
}

package ui;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;

public class Hero {
    BufferedImage img;
    int x;
    int y;
    int w;
    int h;
    int level;
    int blood;

    public Hero() {
        try {
            this.img = ImageIO.read(this.getClass().getResource("/img/ep14.png"));
            this.w = this.img.getWidth() / 2;
            this.h = this.img.getHeight() / 2;
            this.x = 300;
            this.y = 700;
            this.level = 1;
            this.blood = 3;
        } catch (IOException var2) {
            var2.printStackTrace();
        }

    }

    public void moveTO(int x2, int y2) {
        this.x = x2 - this.w / 2;
        this.y = y2 - this.h / 2;
    }
//等级不同的话射出来的子弹也是不同的
    public List<Bullet> shoot() {
        List<Bullet> bs = new ArrayList();
        Bullet b;
        if (this.level == 1) {
            b = new Bullet(this.x + 26, this.y - 30);
            bs.add(b);
        } else {
            Bullet b1;
            if (this.level == 2) {
                b = new Bullet(this.x + 10, this.y);
                b1 = new Bullet(this.x + 40, this.y);
                bs.add(b);
                bs.add(b1);
            } else {
                Bullet b2;
                if (this.level == 3) {
                    b = new Bullet(this.x, this.y);
                    b1 = new Bullet(this.x + 26, this.y - 10);
                    b2 = new Bullet(this.x + 50, this.y);
                    bs.add(b);
                    bs.add(b1);
                    bs.add(b2);
                } else {
                    Bullet b3;
                    if (this.level == 4) {
                        b = new Bullet(this.x - 20, this.y - 10);
                        b1 = new Bullet(this.x + 10, this.y - 10);
                        b2 = new Bullet(this.x + 40, this.y - 10);
                        b3 = new Bullet(this.x + 70, this.y - 10);
                        bs.add(b);
                        bs.add(b1);
                        bs.add(b2);
                        bs.add(b3);
                    } else if (this.level == 5) {
                        b = new Bullet(this.x - 5, this.y);
                        b1 = new Bullet(this.x + 25, this.y - 10);
                        b2 = new Bullet(this.x + 55, this.y);
                        b3 = new Bullet(this.x + 85, this.y + 15);
                        Bullet b4 = new Bullet(this.x - 35, this.y + 15);
                        bs.add(b);
                        bs.add(b1);
                        bs.add(b2);
                        bs.add(b3);
                        bs.add(b4);
                    }
                }
            }
        }

        return bs;
    }
}
package ui;

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

public class Enmey {
    BufferedImage img;
    int x;
    int y;
    int w;
    int h;
    int sp;
    int ds;
    int boold;

    public Enmey() {
        Random rd = new Random();
        int id = rd.nextInt(11) + 1;
        String path = "/img/ep" + (id < 10 ? "0" : "") + id + ".png";

        try {
            this.img = ImageIO.read(this.getClass().getResource(path));
            this.w = this.img.getWidth();
            this.h = this.img.getHeight();
            this.x = rd.nextInt(APP.WIDTH - this.w);
            this.y = -this.h;
            this.sp = rd.nextInt(8) + 1;
            this.ds = rd.nextInt(3);
            this.boold = rd.nextInt(3) + 1;
        } catch (IOException var5) {
            var5.printStackTrace();
        }

    }

    public void move() {
        if (this.ds == 0) {
            this.y += this.sp;
        } else if (this.ds == 1) {
            this.y += this.sp;
            ++this.x;
        } else {
            this.y += this.sp;
            --this.x;
        }

    }

    public boolean hitByHero(Hero hero) {
        int hx = hero.x;
        int hy = hero.y;
        int hw = hero.w;
        int hh = hero.h;
        boolean hit = false;
        hit = this.x <= hx + hw && this.x >= hx - this.w && this.y >= hy - this.h && this.y <= hy + hh;
        return hit;
    }

    public boolean hitByBullet(Bullet b) {
        int bx = b.x;
        int by = b.y;
        int bh = b.h;
        int bw = b.w;
        boolean hit = false;
        hit = this.x <= bx + bw && this.x >= bx - this.w && this.y >= by - this.h && this.y <= by + bh;
        return hit;
    }
}

package ui;

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

public class Boss1 {
    BufferedImage img;
    int x;
    int y;
    int w;
    int h;
    int ds;
    int sp;
    int boold = 100;
    Random rd = new Random();

    public Boss1() {
        try {
            this.img = ImageIO.read(this.getClass().getResource("/img/ep13.png"));
            this.w = this.img.getWidth() * 2;
            this.h = this.img.getHeight() * 2;
            this.x = 100;
            this.y = -this.h;
            this.sp = this.rd.nextInt(5);
        } catch (IOException var2) {
            var2.printStackTrace();
        }

    }

    public void move() {
        if (this.ds == 0) {
            this.x -= this.sp;
        } else if (this.ds == 1) {
            this.x -= this.sp;
            this.y += this.sp;
        } else if (this.ds == 2) {
            this.y += this.sp;
        } else if (this.ds == 3) {
            this.x += this.sp;
            this.y += this.sp;
        } else if (this.ds == 4) {
            this.x += this.sp;
        } else if (this.ds == 5) {
            this.x += this.sp;
            this.y -= this.sp;
        } else if (this.ds == 6) {
            this.y -= this.sp;
        } else if (this.ds == 7) {
            this.x -= this.sp;
            this.y -= this.sp;
        }

        if (this.x > APP.WIDTH * 3 / 4) {
            this.ds = this.rd.nextInt(3);
        } else if (this.x < 0) {
            this.ds = this.rd.nextInt(5) + 2;
        } else if (this.y < 0) {
            this.ds = this.rd.nextInt(5);
        } else if (this.y > 300) {
            this.ds = this.rd.nextInt(4) + 4;
        }

    }

    public boolean hitByHero(Hero hero) {
        int hx = hero.x;
        int hy = hero.y;
        int hh = hero.h;
        int hw = hero.w;
        boolean hit = false;
        hit = this.x <= hx + hw && this.x >= hx - this.w && this.y >= hy - this.h && this.y <= hy + hh;
        return hit;
    }
}
package ui;

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

public class HelpPlane {
    BufferedImage img;
    int x;
    int y;
    int w;
    int h;
    int sp;
    int boold = 5;

    public HelpPlane() {
        try {
            this.img = ImageIO.read(this.getClass().getResource("/img/ep12.png"));
            this.w = this.img.getWidth();
            this.h = this.img.getHeight();
            Random rd = new Random();
            this.x = rd.nextInt(APP.WIDTH - this.w);
            this.y = -this.h;
            this.sp = rd.nextInt(8) + 1;
        } catch (IOException var2) {
            var2.printStackTrace();
        }

    }

    public void move() {
        this.y += this.sp;
    }

    public boolean hitByBullet(Bullet b) {
        int bx = b.x;
        int by = b.y;
        int bh = b.h;
        int bw = b.w;
        boolean hit = false;
        hit = this.x <= bx + bw && this.x >= bx - this.w && this.y >= by - this.h && this.y <= by + bh;
        return hit;
    }
}

package ui;

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

public class Missile {
    BufferedImage img;
    int x;
    int y;
    int w;
    int h;

    public Missile() {
        try {
            this.img = ImageIO.read(this.getClass().getResource("/img/ep15.png"));
            this.w = this.img.getWidth();
            this.h = this.img.getHeight() / 3;
            Random rd = new Random();
            this.x = rd.nextInt(APP.WIDTH - this.w);
            this.y = -this.h;
        } catch (IOException var2) {
            var2.printStackTrace();
        }

    }

    public void move() {
        this.y += 15;
    }

    public boolean hitByMissile(Hero hero) {
        int hx = hero.x;
        int hy = hero.y;
        int hh = hero.h;
        int hw = hero.w;
        boolean hit = false;
        hit = this.x <= hx + hw && this.x >= hx - this.w && this.y >= hy - this.h && this.y <= hy + hh;
        return hit;
    }
}

第五步,为了游戏看起来更加和谐就加一个bgm吧

还是在ui包下面创建一个Music类,然后在文件里面导入MP3

package ui;
import java.io.File;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.Sequence;
import javax.sound.midi.Sequencer;

public class Music {
    public Music() {
    }

    public void run() {
        try {
            Sequencer sequencer = MidiSystem.getSequencer();
            sequencer.open();
            File my = new File("1.mp3");
            Sequence mySeq = MidiSystem.getSequence(my);
            sequencer.setSequence(mySeq);
            sequencer.setLoopCount(-1);
            sequencer.start();
        } catch (Exception var4) {
            var4.printStackTrace();
        }

    }
}

第六步,把GamePanel里面我们创建的这些类对应的功能代码补齐写好(真的很长)

// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package ui;

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.ImageObserver;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JPanel;

public class GamePanel extends JPanel {
    Hero hero = new Hero();
    Boss1 b1 = new Boss1();
    List<Enmey> es = new ArrayList();
    List<HelpPlane> hps = new ArrayList();
    List<Bullet> bs = new ArrayList();
    List<Missile> ms = new ArrayList();
    int score = 0;
    boolean isGameOver = false;
    bg bg = new bg();
    int z = 0;
    int y = 0;
    int index = 0;
    int x = 0;

    public GamePanel() {
        this.setBackground(new Color(80, 60, 30));
        MouseAdapter adapter = new MouseAdapter() {
            public void mouseMoved(MouseEvent e) {
                int x = e.getX();
                int y = e.getY();
                if (!GamePanel.this.isGameOver) {
                    GamePanel.this.hero.moveTO(x, y);
                }

                GamePanel.this.repaint();
            }

            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                if (GamePanel.this.isGameOver) {
                    GamePanel.this.isGameOver = false;
                    GamePanel.this.hero = new Hero();
                    GamePanel.this.es.clear();
                    GamePanel.this.bs.clear();
                    GamePanel.this.hps.clear();
                    GamePanel.this.score = 0;
                    GamePanel.this.ms.clear();
                    GamePanel.this.b1 = new Boss1();
                }

            }
        };
        this.addMouseListener(adapter);
        this.addMouseMotionListener(adapter);
    }

    public void action() {
        (new Thread() {
            public void run() {
                while(true) {
                    if (!GamePanel.this.isGameOver) {
                        GamePanel.this.enmeyEnter();

                        int i;
                        Enmey e;
                        for(i = 0; i < GamePanel.this.es.size(); ++i) {
                            e = (Enmey)GamePanel.this.es.get(i);
                            e.move();
                        }

                        GamePanel.this.helpEnter();

                        for(i = 0; i < GamePanel.this.hps.size(); ++i) {
                            HelpPlane h = (HelpPlane)GamePanel.this.hps.get(i);
                            h.move();
                        }

                        GamePanel.this.missileEnter();

                        for(i = 0; i < GamePanel.this.ms.size(); ++i) {
                            Missile m = (Missile)GamePanel.this.ms.get(i);
                            m.move();
                        }

                        GamePanel.this.shootAction();

                        Bullet b;
                        for(i = 0; i < GamePanel.this.bs.size(); ++i) {
                            b = (Bullet)GamePanel.this.bs.get(i);
                            b.move();
                        }

                        if (GamePanel.this.score > 10000) {
                            GamePanel.this.boss1Enter();
                            GamePanel.this.b1hAction();
                            GamePanel.this.b1Action();
                            GamePanel.this.b1.move();
                            GamePanel.this.es.clear();
                        }

                        GamePanel.this.boomAction();
                        GamePanel.this.bangAction();
                        GamePanel.this.hpAction();
                        GamePanel.this.miAction();

                        for(i = 0; i < GamePanel.this.es.size(); ++i) {
                            e = (Enmey)GamePanel.this.es.get(i);
                            if (e.y > APP.HEIGHT) {
                                GamePanel.this.es.remove(e);
                            }

                            if (e.x > APP.WIDTH) {
                                GamePanel.this.es.remove(e);
                            }

                            if (e.x + e.w < 0) {
                                GamePanel.this.es.remove(e);
                            }
                        }

                        for(i = 0; i < GamePanel.this.bs.size(); ++i) {
                            b = (Bullet)GamePanel.this.bs.get(i);
                            if (b.y + b.h < 0) {
                                GamePanel.this.bs.remove(b);
                            }
                        }

                        try {
                            Thread.sleep(20L);
                            GamePanel.this.repaint();
                        } catch (InterruptedException var3) {
                            var3.printStackTrace();
                        }
                    }
                }
            }
        }).start();
    }

    protected void b1hAction() {
        if (this.b1.hitByHero(this.hero)) {
            this.hero.blood = 0;
            this.isGameOver = true;
        }

    }

    protected void b1Action() {
        for(int i = 0; i < this.bs.size(); ++i) {
            Bullet b = (Bullet)this.bs.get(i);
            if (b.hitByBoss1(this.b1)) {
                --this.b1.boold;
                this.bs.remove(i);
                this.score += 10;
                if (this.b1.boold <= 0) {
                    this.b1.boold = 0;
                    this.isGameOver = true;
                }
                break;
            }
        }

    }

    protected void miAction() {
        for(int i = 0; i < this.ms.size(); ++i) {
            Missile m = (Missile)this.ms.get(i);
            if (m.hitByMissile(this.hero)) {
                Hero var10000 = this.hero;
                var10000.blood -= 3;
                if (this.hero.blood <= 0) {
                    this.hero.blood = 0;
                    this.isGameOver = true;
                }
                break;
            }
        }

    }

    protected void missileEnter() {
        ++this.z;
        if (this.z % 500 == 0) {
            Missile m = new Missile();
            this.ms.add(m);
        }

    }

    protected void hpAction() {
        for(int i = 0; i < this.bs.size(); ++i) {
            Bullet b = (Bullet)this.bs.get(i);

            for(int j = 0; j < this.hps.size(); ++j) {
                HelpPlane h = (HelpPlane)this.hps.get(j);
                if (h.hitByBullet(b)) {
                    --h.boold;
                    if (h.boold <= 0) {
                        this.hps.remove(h);
                        if (this.hero.level <= 4) {
                            ++this.hero.level;
                        }

                        if (this.hero.level > 2 && this.hero.blood <= 2) {
                            ++this.hero.blood;
                        }
                    }

                    this.bs.remove(i);
                    this.score += 10;
                    break;
                }
            }
        }

    }

    protected void bangAction() {
        for(int i = 0; i < this.bs.size(); ++i) {
            Bullet b = (Bullet)this.bs.get(i);

            for(int j = 0; j < this.es.size(); ++j) {
                Enmey e = (Enmey)this.es.get(j);
                if (e.hitByBullet(b)) {
                    --e.boold;
                    if (e.boold <= 0) {
                        this.es.remove(e);
                    }

                    this.bs.remove(i);
                    this.score += 10;
                    if (this.score % 1000 == 0 && this.score > 1000) {
                        this.bg.changeImg();
                    }
                    break;
                }
            }
        }

    }

    protected void boomAction() {
        for(int i = 0; i < this.es.size(); ++i) {
            Enmey e = (Enmey)this.es.get(i);
            if (e.hitByHero(this.hero)) {
                this.es.remove(i);
                if (this.hero.level >= 2) {
                    --this.hero.level;
                }

                this.score += 1024;
                --this.hero.blood;
                if (this.hero.blood <= 0) {
                    this.hero.blood = 0;
                    this.isGameOver = true;
                }
                break;
            }
        }

    }

    protected void boss1Enter() {
    }

    protected void shootAction() {
        ++this.y;
        if (this.y % 15 == 0) {
            List<Bullet> b = this.hero.shoot();
            this.bs.addAll(b);
        }

    }

    protected void enmeyEnter() {
        ++this.index;
        if (this.index % 25 == 0) {
            Enmey e = new Enmey();
            this.es.add(e);
        }

    }

    protected void helpEnter() {
        ++this.x;
        if (this.x % 500 == 0) {
            HelpPlane h = new HelpPlane();
            this.hps.add(h);
        }

    }

    public void paint(Graphics g) {
        super.paint(g);
        g.drawImage(this.bg.img, this.bg.x, this.bg.y, this.bg.w, this.bg.h, (ImageObserver)null);
        g.drawImage(this.hero.img, this.hero.x, this.hero.y, this.hero.w, this.hero.h, (ImageObserver)null);

        int i;
        for(i = 0; i < this.es.size(); ++i) {
            Enmey e = (Enmey)this.es.get(i);
            g.drawImage(e.img, e.x, e.y, e.w, e.h, (ImageObserver)null);
        }

        for(i = 0; i < this.hps.size(); ++i) {
            HelpPlane h = (HelpPlane)this.hps.get(i);
            g.drawImage(h.img, h.x, h.y, h.w, h.h, (ImageObserver)null);
        }

        for(i = 0; i < this.bs.size(); ++i) {
            Bullet b = (Bullet)this.bs.get(i);
            g.drawImage(b.img, b.x, b.y, b.w, b.h, (ImageObserver)null);
        }

        for(i = 0; i < this.ms.size(); ++i) {
            Missile m = (Missile)this.ms.get(i);
            g.drawImage(m.img, m.x, m.y, m.h, m.w, (ImageObserver)null);
        }

        g.drawImage(this.b1.img, this.b1.x, this.b1.y, this.b1.w, this.b1.h, (ImageObserver)null);
        g.setColor(Color.white);
        Font font = new Font("楷体", 1, 24);
        g.setFont(font);
        g.drawString("血量:" + this.hero.blood, 20, 30);
        g.drawString("分数:" + this.score, 330, 30);
        g.setColor(Color.red);
        Font f = new Font("楷体", 1, 40);
        g.setFont(f);
        if (this.isGameOver) {
            g.drawString("Game Over!", 166, 360);
        }

    }
}

第七步,把GameFrame里面的主方法写出来并且补齐剩余部分

package ui;

import java.awt.Component;
import javax.swing.JFrame;

public class GameFrame extends JFrame {
    GamePanel panel = new GamePanel();

    public GameFrame() {
        this.add(this.panel);
        this.setTitle("全民飞机大战 by MT");
        this.setSize(APP.WIDTH, APP.HEIGHT);
        this.setLocationRelativeTo((Component)null);
        this.setDefaultCloseOperation(3);
        this.setResizable(false);
    }

    public static void main(String[] args) {
        GameFrame frame = new GameFrame();
        frame.panel.action();
        frame.setVisible(true);
    }
}

剩下的就是享受游戏了🤭。
这次整个项目给我的收获就是还是坚持贯彻面向对象的思想,把能够多余的抽离出来的部分都抽离出来做成对象,然后让程序调用我们创建的一个一个对象和里面包含的方法来执行功能,这样可以使思路清晰代码简洁且易于修复和复用。
一个不足之处就是在实现添加bgm的功能时我一直搞不懂这玩意究竟是怎么运作的,最后这段代码是直接在网上找了copy过来的。
第一次写博客语言颠倒混乱,望诸君见谅。
感谢看到此处,希望我们的java水平都能越来越高。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值