Back to projects
Jan 01, 2025
3 min read

Space Invaders Game

I recreated the classic Space Invaders game using Java Swing to build a dynamic and interactive game environment. The game features classic elements like a player-controlled spaceship that shoots projectiles at advancing alien enemies. I implemented key mechanics, such as collision detection, enemy movement patterns, and scoring. Using Java Swing’s GUI components, I also designed a simple interface with game controls, scoring display, and a clear indication of game over. This project helped me further develop my skills in Java and object-oriented programming, while also giving me hands-on experience in game design.

Here’s the result:

Game Screenshot

Class Structure

I designed the game with an object-oriented approach. The class diagram shows how different game elements work together:

Class Diagram

Game Components

Game Objects

The foundation of the game is the GameObject class, which defines how all game elements behave.

Here’s an abstract of this class that also show the collision detection system:

abstract class GameObject {
    protected int x, y;
    protected int width, height;

    public boolean collidesWith(GameObject other) {
        return x < other.x + other.width &&
                x + width > other.x &&
                y < other.y + other.height &&
                y + height > other.y;
    }

    public abstract void draw(Graphics2D g);
    public abstract void update();
}

Aliens

The alien enemies move together in a group, sliding left and right across the game area. When they hit the edge of the screen, they automatically change direction and move down a bit.

Here’s how their movement works:

class Alien extends GameObject {
    private static final int SPEED = 2;
    private int direction = 1;

    public void update() {
        x += SPEED * direction;
    }

    public boolean needsToChangeDirection() {
        return x <= 0 || x >= 770;
    }

    public void changeDirection() {
        direction *= -1;
        y += 20;
    }
}

Player Spaceship

The player’s spaceship moves left and right using arrow keys and shoots missiles with the spacebar.

This abstract of the code shows how I implemented the movement:

class Player extends GameObject {
    private boolean movingLeft;
    private boolean movingRight;
    private static final int SPEED = 5;

    public void update() {
        if (movingLeft && x > 0) x -= SPEED;
        if (movingRight && x < 770) x += SPEED;
    }
}

Projectiles and Bunkers

Missiles fly up and down the screen. Players have bunkers that protect them from alien shots. When a missile hits a bunker, it loses some of its health and gradually breaks down.

Here’s how projectiles work:

class Projectile extends GameObject {
    private final int velocity;

    public Projectile(int x, int y, int velocity) {
        super(x, y, 3, 15);
        this.velocity = velocity;
    }

    public void update() {
        y += velocity;
    }

    public boolean isOutOfBounds() {
        return y < 0 || y > 600;
    }
}

The bunkers lose health when hit, gradually breaking down:

class Bunker extends GameObject {
    private int health;

    public Bunker(int x, int y) {
        super(x, y, 60, 40);
        health = 4;
    }

    public void damage() {
        health--;
    }

    public boolean isDestroyed() {
        return health <= 0;
    }
}

Here’s how I implemented bunker graphics:

    public void draw(Graphics2D g) {
        g.setColor(new Color(128, 128, 128, 255 * health / 4));
        int[] xPoints = {x, x + width, x + width - 10, x + 10};
        int[] yPoints = {y + height, y + height, y, y};
        g.fillPolygon(xPoints, yPoints, 4);
    }

Future Improvements

Potential additions to the game:

  • Multiple difficulty levels
  • Sound effects
  • High score system
  • Power-up mechanics