Game Development 16 min read

Java Shooting Game Example Demonstrating OOP Concepts

This article presents a complete Java shooting‑game source code, explaining how each class—Airplane, Bee, Bullet, Hero, and the main game loop—illustrates object‑oriented programming principles such as inheritance, interfaces, encapsulation, and event‑driven design for a simple desktop game.

Java Captain
Java Captain
Java Captain
Java Shooting Game Example Demonstrating OOP Concepts

The author shares a full Java desktop shooting‑game project to help readers understand object‑oriented programming (OOP) through practical code examples.

Airplane (enemy plane) class:

import java.util.Random;

public class Airplane extends FlyingObject implements Enemy {
    private int speed = 3;  //移动步骤

    /** 初始化数据 */
    public Airplane(){
        this.image = ShootGame.airplane;
        width = image.getWidth();
        height = image.getHeight();
        y = -height;            
        Random rand = new Random();
        x = rand.nextInt(ShootGame.WIDTH - width);
    }

    @Override
    public int getScore() {   
        return 5;
    }

    @Override
    public boolean outOfBounds() {   
        return y>ShootGame.HEIGHT;
    }

    @Override
    public void step() {   
        y += speed;
    }
}

Award interface (score rewards):

/** 
 * 奖励 
 */  
public interface Award {  
    int DOUBLE_FIRE = 0;  //双倍火力  
    int LIFE = 1;   //1条命  
    /** 获得奖励类型(上面的0或1) */  
    int getType();  
}

Bee (reward object) class:

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(){   
        this.image = ShootGame.bee;   
        width = image.getWidth();   
        height = image.getHeight();   
        y = -height;   
        Random rand = new Random();   
        x = rand.nextInt(ShootGame.WIDTH - width);   
        awardType = rand.nextInt(2);   //初始化时给奖励   
    }   

    public int getType(){   
        return awardType;   
    }   

    @Override   
    public boolean outOfBounds() {   
        return y>ShootGame.HEIGHT;   
    }   

    @Override   
    public void step() {        
        x += xSpeed;   
        y += ySpeed;   
        if(x > ShootGame.WIDTH-width){    
            xSpeed = -1;   
        }   
        if(x < 0){   
            xSpeed = 1;   
        }   
    }   
}

Bullet class (projectile):

/** 
 * 子弹类:是飞行物 
 */  
public class Bullet extends FlyingObject {  
    private int speed = 3;  //移动的速度   

    /** 初始化数据 */   
    public Bullet(int x,int y){   
        this.x = x;   
        this.y = y;   
        this.image = ShootGame.bullet;   
    }   

    @Override   
    public void step(){      
        y-=speed;   
    }   

    @Override   
    public boolean outOfBounds() {   
        return y<-height;   
    }   
}

Enemy interface (scorable enemies):

/** 
 * 敌人,可以有分数 
 */  
public interface Enemy {  
    /** 敌人的分数  */  
    int getScore();  
}

FlyingObject abstract base class (common properties and collision logic):

import java.awt.image.BufferedImage;  

/** 
 * 飞行物(敌机,蜜蜂,子弹,英雄机) 
 */  
public abstract class FlyingObject {  
    protected int x;    //x坐标 
    protected int y;    //y坐标 
    protected int width;    //宽 
    protected int height;    //高 
    protected BufferedImage image;    //图片 

    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 BufferedImage getImage() { return image; }
    public void setImage(BufferedImage image) { this.image = image; }

    public abstract boolean outOfBounds();
    public abstract void step();

    public boolean shootBy(Bullet bullet){   
        int x = bullet.x;  //子弹横坐标   
        int y = bullet.y;  //子弹纵坐标   
        return this.x<x && x<this.x+width && this.y<y && y<this.y+height;   
    }
}

Hero class (player’s aircraft with double fire and life management):

import java.awt.image.BufferedImage;  

/** 
 * 英雄机:是飞行物
 */  
public class Hero extends FlyingObject{  
    private BufferedImage[] images = {};  //英雄机图片   
    private int index = 0;                 //英雄机图片切换索引   
    private int doubleFire;   //双倍火力   
    private int life;   //命   

    public Hero(){   
        life = 3;   //初始3条命   
        doubleFire = 0;   //初始火力为0   
        images = new BufferedImage[]{ShootGame.hero0, ShootGame.hero1}; //英雄机图片数组   
        image = ShootGame.hero0;   //初始为hero0图片   
        width = image.getWidth();   
        height = image.getHeight();   
        x = 150;   
        y = 400;   
    }
    // getters/setters for doubleFire and life omitted for brevity
    @Override
    public boolean outOfBounds() { return false; }
    public Bullet[] shoot(){ 
        int xStep = width/4; 
        int yStep = 20; 
        if(doubleFire>0){ 
            Bullet[] bullets = new Bullet[2];
            bullets[0] = new Bullet(x+xStep,y-yStep);
            bullets[1] = new Bullet(x+3*xStep,y-yStep);
            return bullets;
        }else{ 
            Bullet[] bullets = new Bullet[1];
            bullets[0] = new Bullet(x+2*xStep,y-yStep);
            return bullets;
        }
    }
    @Override
    public void step() { 
        if(images.length>0){ 
            image = images[index++/10%images.length];
        }
    }
    public boolean hit(FlyingObject other){ 
        int x1 = other.x - this.width/2; 
        int x2 = other.x + this.width/2 + other.width; 
        int y1 = other.y - this.height/2; 
        int y2 = other.y + this.height/2 + other.height; 
        int herox = this.x + this.width/2; 
        int heroy = this.y + this.height/2; 
        return herox>x1 && herox<x2 && heroy>y1 && heroy<y2; 
    }
}

Main game class (Swing panel handling rendering, input, timer, and game logic):

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.*;

public class ShootGame extends JPanel {
    public static final int WIDTH = 400; // 面板宽
    public static final int HEIGHT = 654; // 面板高
    private int state;
    private static final int START = 0, RUNNING = 1, PAUSE = 2, GAME_OVER = 3;
    private int score = 0;
    private Timer timer;
    private int intervel = 1000 / 100;
    public static BufferedImage background, start, airplane, bee, bullet, hero0, hero1, pause, gameover;
    private FlyingObject[] flyings = {};
    private Bullet[] bullets = {};
    private Hero hero = new Hero();
    static { /* load images */ }
    @Override public void paint(Graphics g) { /* draw background, hero, bullets, objects, score, state */ }
    // ... many helper methods: paintHero, paintBullets, paintFlyingObjects, paintScore, paintState
    public static void main(String[] args){ JFrame frame = new JFrame("Fly"); ShootGame game = new ShootGame(); frame.add(game); frame.setSize(WIDTH, HEIGHT); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); game.action(); }
    public void action(){ /* mouse listeners, timer task that calls enterAction, stepAction, shootAction, bangAction, outOfBoundsAction, checkGameOverAction, repaint */ }
    // ... implementations of enterAction, stepAction, shootAction, bangAction, outOfBoundsAction, checkGameOverAction, isGameOver, bang, nextOne
}

In the concluding section the author notes that image resources are omitted from the post and invites readers to request them, while also providing a mind‑map to further illustrate OOP concepts used in the game.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Game Developmentexample-codeOOPShooting Game
Java Captain
Written by

Java Captain

Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.