Skip to content
Tutorials Center
My learning

Topics

Lesson 34 of 36 ยท Projects and Practice

Project: A Space Shooter

Build a vertical space shooter in Phaser with bullet pools, enemy waves, particle explosions, power-ups, a starfield, a HUD scene and rising difficulty.

A top-down space shooter is the classic second project. It practices exactly the things that the platformer did not: objects that come and go all the time (so pooling matters), systems that react to timers (waves and difficulty), screen-filling feedback (particles and screen shake), and a separate HUD scene fed by shared state. By the end you will have a game that gets harder the longer you survive.

As in the earlier project, all pictures are drawn in code, so everything runs on this page with no external files. Ship graphics, sounds and backgrounds are easy to swap in later.

Design

flowchart LR
  I["Input<br/>keys or pointer"] --> S["Player ship<br/>auto-fire"]
  S --> B["Bullet pool"]
  W["Wave timer<br/>(difficulty)"] --> E["Enemies<br/>(hp, wobble path)"]
  B -- overlap --> E
  E -- destroyed --> X["Explosion particles<br/>+ score + power-up chance"]
  E -- touches --> S
  EB["Enemy bullets"] -- overlap --> S
  X --> R["Registry: score, lives"]
  R --> H["HUD scene"]

Gameplay objects write to the registry; the HUD only reads it.

The rules are simple. The ship fires automatically, so the player concentrates on moving. Enemies fall in wobbly paths and some shoot back. Each kill is worth points, some enemies drop a green power-up that adds a life or a spread shot, and every 250 points the waves become faster. Three lives, no continues.

Building blocks

A bullet class for the pool. As in Custom Game Objects and Object Pools, bullets are created once and reused. fire() resets position and speed; preUpdate() disables the bullet when it leaves the screen.

An enemy class. An enemy has hit points, a base x and a sine wobble. Reusing enemies is the same pool pattern, so spawn() resets everything.

Particles. In Phaser 3.60 and later, and in Phaser 4, you create an emitter with this.add.particles(x, y, texture, config). A one-shot explosion is an emitter with emitting: false that you trigger with explode(count, x, y). One emitter can serve every explosion in the scene.

Scrolling background. A tileSprite the size of the screen has a tilePositionY property. Increasing it each frame scrolls the pattern endlessly. Two layers with different speeds give parallax.

Difficulty. A single number, level = 1 + floor(score / 250), controls the spawn delay, enemy speed and the chance that an enemy shoots. Tying everything to one number keeps the balance easy to adjust.

The complete game

Move with the arrow keys or WASD, or press and drag with the mouse or a finger: the ship follows the pointer. Click the canvas first so that it gets keyboard focus. The ship shoots on its own.

Star Defender: the complete shooter

Walking through the design

Everything short-lived is pooled. Bullets, enemy bullets and enemies come from groups with a maxSize. group.get() returns null when the pool is empty, and the code treats that as "skip this shot", which also limits the fire rate under extreme conditions. Dead objects are disabled with disableBody(true, true), never destroyed.

One number drives difficulty. level (from the score) feeds the spawn delay, wave size, enemy speed, enemy hit points and how often enemies fire. Change the 250 in one place, or the coefficients (90, 12, 80), to rebalance the whole game.

Feedback makes it feel good. An explosion is just 12 to 40 particles, a 80 ms screen shake and a short invulnerability flicker. None of this is gameplay logic, but players perceive it as the game being responsive.

State goes through the registry. The Game scene writes score and lives; the Hud redraws on changedata. When you add a shop, a pause screen or a second HUD element, nothing in the gameplay code changes. The HUD removes its listeners on shutdown, so restarting does not stack duplicates.

The pointer works like a joystick. While the pointer is down, the ship's velocity points toward it (offset upward so that your finger does not hide the ship). This gives touch devices a control scheme with no extra buttons.

Extending the game

  1. Boss fights: every N levels spawn a large enemy with 50 hit points that moves in a pattern and fires bursts. Use a state machine for its phases.
  2. Sound: a laser sound on shoot, a boom on explode, music that gets faster with the level.
  3. Weapons: homing missiles, a laser beam, and a shield power-up.
  4. Real sprites and animations from a texture atlas, with animated engine flames.
  5. High scores saved with the save manager and shown on a start screen.
  6. Game feel: hit pause (freeze time for 50 ms on a kill), screen flashes and slow motion when the player dies.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does the shooter use pooled groups with maxSize for bullets and enemies?

    Hundreds of bullets are created over a game. Recycling them avoids memory churn and frame drops.
  2. 2. How does one explosion emitter serve every explosion?

    A one-shot emitter can be triggered anywhere, so you never allocate a new one per explosion.
  3. 3. What drives the increasing difficulty?

    Tying all difficulty settings to one number makes the game easy to balance.
  4. 4. Why does the HUD scene remove its registry listener on shutdown?

    The registry outlives the HUD scene. Old listeners would keep firing after each restart.

Summary

  • A shooter is built from pooled bullets and enemies, a wave timer and a difficulty level.
  • Particles, screen shake and brief invulnerability give feedback at almost no cost.
  • The game scene owns the rules and writes to the registry; the HUD scene reads and draws.
  • One difficulty number drives spawn rate, speed, hit points and aggression.
  • A pointer that works like a joystick makes the game playable on touch screens.

Next: Project: A Memory Match Game