Java implementation of Airplane Wars 2.0 game (open source)

Foreword:

The game is based on [Shangxuetang] Aircraft Battle 2.0 at station B, the main frame remains unchanged, and the code is slightly changed.

Java technology: java variables, java data types, judgment statements, loop structures, arrays, Java collections, simple window creation, graphics and picture drawing, double buffering technology, events-mouse and keyboard events, object collision detection.

?

The main framework of the game:

Game code:

(1) BgObj class

By changing the coordinates of the background image, the background image can be cyclically scrolled.

import java.awt.*;

public class BgObj extends GameObj{

    @Override
    public void painSelf(Graphics gImage) {
        super. painSelf(gImage);
        y + = speed;
        if (y >= 0) {
            y = -2000;
        }
    }

    public BgObj() {
        super();
    }

    public BgObj(Image img, int x, int y, double speed) {
        super(img, x, y, speed);
    }
}

(2) BossObj class

import java.awt.*;

public class BossObj extends GameObj{

    int life = 15;

    @Override
    public void painSelf(Graphics gImage) {
        super. painSelf(gImage);
        if (x > 550 || x <- 50) {
            speed = -speed;
        }
        x + = speed;
        for (ShellObj shellObj : GameUtils. shellObjList) {
            if (this. getRec(). intersects(shellObj. getRec())) {
                shellObj.setX(-100);
                shellObj.setY(100);
                GameUtils. removeList. add(shellObj);
                life--;
            }
            if (life <= 0) {
                GameWin. state = 4;
            }
        }
        //White background of the blood bar
        gImage.setColor(Color.white);
        gImage.fillRect(20,40,100,10);
        //drawing the blood bar
        gImage.setColor(Color.red);
        gImage.fillRect(20,40,life* 100 / 20,10);
    }

    @Override
    public Rectangle getRec() {
        return super. getRec();
    }

    public BossObj(Image img, int x, int y, int width, int height, double speed, GameWin frame) {
        super(img, x, y, width, height, speed, frame);
    }
}

(3) BulletObj class

import java.awt.*;

public class BulletObj extends GameObj{

    @Override
    public void painSelf(Graphics gImage) {
        super. painSelf(gImage);
        y + = speed;
        // track my machine
// if (this.x > PlaneObj.getX)
        //Collision detection between enemy bullets and our aircraft
        if (this. getRec(). intersects(this. frame. planeObj. getRec())) {
            GameWin. state = 3;
        }
        //The enemy's bullet crosses the boundary and disappears Condition y > 600 The changed coordinates (-300, 300)
        if (y > 600) {
            this.x = -300;
            this.y = 300;
            GameUtils. removeList. add(this);
        }
    }

    @Override
    public Rectangle getRec() {
        return super. getRec();
    }

    public BulletObj(Image img, int x , int y, int width, int height, double speed, GameWin frame) {
        super(img, x, y, width, height, speed, frame);
    }
}

(4) EnemyObj class

import java.awt.*;

public class EnemyObj extends GameObj{
    @Override
    public void painSelf(Graphics gImage) {
        super. painSelf(gImage);
        y + = speed;
        / / enemy aircraft collision detection
        if (this. getRec(). intersects(this. frame. planeObj. getRec())) {
            GameWin. state = 3;
        }
        //The enemy's cross-border disappears Judgment condition y > 600 The changed coordinates (-200, -200)
        if (y > 600) {
            this.x = -200;
            this.y = 200;
            GameUtils. removeList. add(this);
        }
        //Move to (-200, -200) before the enemy disappears. Our bullets (-100, 100)
        for(ShellObj shellObj : GameUtils. shellObjList) {
            if (this. getRec(). intersects(shellObj. getRec())) {
                ExplodeObj explodeObj = new ExplodeObj(x,y);
                GameUtils.explodeObjList.add(explodeObj);
                GameUtils. removeList. add(explodeObj);
                System.out.println("Collision");
                shellObj.setX(-100);
                shellObj.setY(100);
                this.x = -200;
                this.y = -200;
                GameUtils. removeList. add(shellObj);
                GameUtils. removeList. add(this);
                GameWin. score++;
            }
        }
    }

    @Override
    public Rectangle getRec() {
        return new Rectangle(x,y,width,height + 5);
    }

    public EnemyObj() {
        super();
    }

    public EnemyObj(Image img, int x, int y, int width, int height, double speed, GameWin frame) {
        super(img, x, y, width, height, speed, frame);
    }
}

(5) ExplodeObj class

import java.awt.*;

public class ExplodeObj extends GameObj{

    static Image[] pic = new Image[16];

    int explodeCount = 0;

    static {
        for (int i = 0; i < pic.length; i ++ ) {
            pic[i] = Toolkit.getDefaultToolkit().getImage("src/imgs/explode/e" + (i + 1) + ".gif");
        }
    }

    public ExplodeObj(int x, int y) {
        super(x, y);
    }

    @Override
    public void painSelf(Graphics gImage) {

        if (explodeCount < 16) {
            super.img = pic[explodeCount];
            super. painSelf(gImage);
            explodeCount++;
        }
    }
}

(6) GameObj class

import java.awt.*;


public class GameObj {
    Image img;
    int x;
    int y;
    int width;
    int height;
    double speed;
    GameWin frame;

    public void painSelf(Graphics gImage) {
        gImage.drawImage(img,x,y,null);
    }
    public Rectangle getRec() {
        return new Rectangle(x,y,width,height);
    }

    public GameObj() {
    }

    public GameObj(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public GameObj(Image img, int x, int y, double speed) {
        this.img = img;
        this.x = x;
        this.y = y;
        this.speed = speed;
    }

    public GameObj(Image img, int x, int y, int width, int height, double speed, GameWin frame) {
        this.img = img;
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        this.speed = speed;
        this. frame = frame;
    }

    public Image getImg() {
        return img;
    }

    public void setImg(Image img) {
        this.img = img;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public double getSpeed() {
        return speed;
    }

    public void setSpeed(double speed) {
        this.speed = speed;
    }

    public GameWin getFrame() {
        return frame;
    }

    public void setFrame(GameWin frame) {
        this. frame = frame;
    }
}

(7) GameUtils class

import java.awt.*;
import java.util.List;
import java.util.ArrayList;

public class GameUtils {
    //Background picture
    public static Image bgImg = Toolkit.getDefaultToolkit().getImage("src/imgs/bg.jpg");
    //boss picture
    public static Image bossImg = Toolkit.getDefaultToolkit().getImage("src/imgs/boss.png");
    //explosion image
    public static Image explodeImg = Toolkit.getDefaultToolkit().getImage("src/imgs/explode/e6.gif");
    //Picture of our fighter
    public static Image planeImg = Toolkit.getDefaultToolkit().getImage("src/imgs/plane.png");
    //Enemy fighter picture
    public static Image enemyImg = Toolkit.getDefaultToolkit().getImage("src/imgs/enemy.png");
    //Our bullet image
    public static Image shellImg = Toolkit.getDefaultToolkit().getImage("src/imgs/shell.png");
    //enemy bullet image
    public static Image bulletImg = Toolkit.getDefaultToolkit().getImage("src/imgs/bullet.png");
    //Collection of elements to delete
    public static List<GameObj> removeList = new ArrayList<>();
    //Collection of all game objects
    public static List<GameObj> gameObjList = new ArrayList<>();
    //Collection of our bullets
    public static List<ShellObj> shellObjList = new ArrayList<>();
    //Collection of enemy bullets
    public static List<BulletObj> bulletObjList = new ArrayList<>();
    //Collection of enemy aircraft
    public static List<EnemyObj> enemyObjList = new ArrayList<>();
    // collection of explosions
    public static List<ExplodeObj> explodeObjList = new ArrayList<>();

    // Tool class for drawing strings
    public static void drawWord(Graphics gImage,String str,Color color,int size,int x ,int y) {
        gImage.setColor(color);
        gImage.setFont(new Font("做宋",Font.BOLD,size));
        gImage.drawString(str,x,y);
    }
}

(8) GameWin class

import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class GameWin extends JFrame {
    //Game status 0 Not started 1 Game in progress 2 Paused 3 Clearance failed 4 Cleared successfully
    public static int state = 0;
    //game score
    public static int score =0;
    //Double cached image
    Image offScreenImage = null;
    //window width
    int width = 600;
    int height = 600;
    //Background image object
    BgObj bgobj = new BgObj(GameUtils.bgImg,0,-2000,2);
    //Our aircraft object
    PlaneObj planeObj = new PlaneObj(GameUtils. planeImg,290,550,20,30,0,this);
    //Enemy boss object
    public BossObj bossObj = null;
    //The number of times the game is redrawn
    int count = 1;
    //Number of enemies
    int enemyCount = 0;

    public void launch() {
        //Set whether the window is visible
        this. setVisible(true);
        //Set window size
        this.setSize(width,height);
        //Close the window and terminate the program immediately
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //Set the window position
        this.setLocationRelativeTo(null);
        //Set window title
        this.setTitle("Plane War");

        GameUtils.gameObjList.add(bgobj);
        GameUtils.gameObjList.add(planeObj);

        this. addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getButton() == 1 & amp; & amp; state == 0){
                    state = 1;
                    repaint();
                }
            }
        });

        //pause
        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if (e. getKeyCode() == 32) {
                    switch (state) {
                        case 1:
                            state = 2;
                            break;
                        case 2:
                            state = 1;
                            break;
                        default:
                    }
                }
            }
        });

        //repeat drawing
        while (true) {
            if(state == 1) {
                creatObj();
                repaint();
            }
            try {
                Thread. sleep(25);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void paint(Graphics g) {
        if(offScreenImage == null) {
            offScreenImage = createImage(width,height);
        }
        Graphics gImage = offScreenImage. getGraphics();
        gImage.fillRect(0,0,width,height);
        if (state == 0) {
            gImage.drawImage(GameUtils.bgImg,0,0,null);
            gImage.drawImage(GameUtils.bossImg,220,120,null);
            gImage.drawImage(GameUtils.explodeImg,270,350,null);
            GameUtils.drawWord(gImage,"Click to start the game",Color.pink,40,180,300);
        }
        if (state == 1) {
            GameUtils.gameObjList.addAll(GameUtils.explodeObjList);
            //running
            for (int i = 0; i < GameUtils. gameObjList. size(); i ++ ) {
                GameUtils.gameObjList.get(i).painSelf(gImage);
                System.out.println(GameUtils.gameObjList.size());
            }
            GameUtils.gameObjList.removeAll(GameUtils.removeList);
        }
        if (state == 3) {
            //fail
            gImage.drawImage(GameUtils.explodeImg, planeObj.getX() + 15, planeObj.getY() + 10, null);
            GameUtils.drawWord(gImage,"GAME OVER!",Color.RED,50,180,300);
        }
        if (state == 4) {
            //Clearance
            gImage.drawImage(GameUtils.explodeImg, bossObj.getX() + 30, bossObj.getY(), null);
            GameUtils.drawWord(gImage,"Game clearance!",Color.pink,50,190,300);
        }
        GameUtils.drawWord(gImage,score + " points",Color.green,40,30,100);
        g. drawImage(offScreenImage,0,0,null);
        count + + ;
    }

    void creatObj() {
        if (count % 15 == 0) {
            GameUtils.shellObjList.add(new ShellObj(GameUtils.shellImg,planeObj.getX() + 13, planeObj.getY() + 8,14,29,5,this));
            GameUtils.gameObjList.add(GameUtils.shellObjList.get(GameUtils.shellObjList.size() - 1));
        }

        if (count % 15 == 0) {
            GameUtils.enemyObjList.add(new EnemyObj(GameUtils.enemyImg,(int)(Math.random()*12)*50,0,49,36,5,this));
            GameUtils.gameObjList.add(GameUtils.enemyObjList.get(GameUtils.enemyObjList.size() - 1));
            enemyCount++;

        }

        if (count % 20 == 0 & amp; & amp; bossObj != null) {
            GameUtils.bulletObjList.add(new BulletObj(GameUtils.bulletImg, bossObj.getX() + 83, bossObj.getY() + 83,15,25,5,this));
            GameUtils.gameObjList.add(GameUtils.bulletObjList.get(GameUtils.bulletObjList.size() - 1));
        }

        if (enemyCount > 100 & amp; & amp; bossObj == null) {
            bossObj = new BossObj(GameUtils.bossImg,250,35,155,100,4,this);
            GameUtils.gameObjList.add(bossObj);
        }
    }

    public static void main(String[] args) throws FileNotFoundException, JavaLayerException {
        GameWin gameWin = new GameWin();
        gameWin. launch();
    }
}

(9) PlaneObj class

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class PlaneObj extends GameObj{
    @Override
    public void painSelf(Graphics gImage) {
        super. painSelf(gImage);
        //Collision detection between our aircraft and the enemy boss
        if (this.frame.bossObj != null & amp; & amp; this.getRec().intersects(this.frame.bossObj.getRec())){
            GameWin. state = 3;
        }
    }


    @Override
    public Rectangle getRec()
    {
        return new Rectangle(x + 28, y + 10, width + 5, height + 5);
    }


// @Override
// public Rectangle getRec() {
// return super. getRec();
// }

    public PlaneObj() {
        super();
    }

    public PlaneObj(Image img, int x, int y, int width, int height, double speed, GameWin frame) {
        super(img,x,y,width,height,speed,frame);
        this.frame.addMouseMotionListener(new MouseAdapter() {
            @Override
            public void mouseMoved(MouseEvent e) {
                PlaneObj. super. x = e. getX() -42;
                PlaneObj. super. y = e. getY() -35;
            }
        });
    }

    @Override
    public Image getImg() {
        return super. getImg();
    }
}

(10) ShellObj class

import java.awt.*;

public class ShellObj extends GameObj {
    @Override
    public void painSelf(Graphics gImage) {
        super. painSelf(gImage);
        y -= speed;
        if (y < 0) {
            this.x = -100;
            this.y = 100;
            GameUtils. removeList. add(this);
        }
    }

    @Override
    public Rectangle getRec() {
        return new Rectangle(x + 4, y, width + 4, height);
    }

    public ShellObj() {
        super();
    }

    public ShellObj(Image img, int x, int y, int width, int height, double speed, GameWin frame) {
        super(img, x, y, width, height, speed, frame);
    }

    @Override
    public Image getImg() {
        return super. getImg();
    }
}

Conclusion: