Milestone 7: Make It Pretty

At the beginning of this chapter, I said we'd focus on functionality, and wait until the end to make the game pretty. We didn't exactly follow that guidance. The HUD, the end scenes, the flickering fireflies, and the background are mostly just visuals, not core functionality.

In this last milestone, we'll focus exclusively on visuals, to make the game more appealing. The art in this game will still only be shapes, but we'll chose colors carefully and add a bit more style. Even a little bit of effort will have a big impact!

Hedges, Trees, and the Hero

The hedges are the easiest change. The gray box and its label become a dark green box with a slightly lighter outline.

Choosing Colors

JetLag wants colors as hex strings, like "#1d4a2a", but nobody picks colors by guessing hex digits. Instead, use an HTML color picker, like the one at W3Schools. Click around until you find a color you like, and then copy the six-digit code it shows you (along with the #) into your code. If you want a see-through color, add two more digits at the end.

The colors for the garden follow a few rules of thumb:

  • Start with the background, then pick everything else to look good against it
  • Each thing gets a base color, and a slightly lighter version of the same color for its outline. That gives the hedges an edge without making them look like they were drawn with a marker.
  • Things the player has to notice, like the fireflies and the jar, are the brightest things on the screen.

The most important rule is this: Whatever colors you choose, make sure the game is still easy to understand at a glance.

For the hedges, all we need to do is make changes to the colors of the fill and line:

  // Draw the hedges as boxes
  for (const hedge of HEDGES) {
    new Actor({
      appearance: new FilledBox({ width: hedge.r - hedge.l, height: hedge.b - hedge.t, fillColor: "#1d4a2a", lineWidth: 0.04, lineColor: "#2f6b3d" }),
      rigidBody: new BoxBody({ cx: (hedge.l + hedge.r) / 2, cy: (hedge.t + hedge.b) / 2, width: hedge.r - hedge.l, height: hedge.b - hedge.t }),
      behaviors: new SolidBehavior(),
    });
  }

Next, we'll do the trees. They'll have a dark green canopy and a small brown circle on top for the trunk:

  // Draw the trees as circles
  for (const tree of TREES) {
    new Actor({
      appearance: [new FilledCircle({ radius: tree.radius, fillColor: "#173d22", lineWidth: 0.05, lineColor: "#2f6b3d" }), new FilledCircle({ radius: tree.radius * 0.3, fillColor: "#4a3321" })],
      rigidBody: new CircleBody({ cx: tree.cx, cy: tree.cy, radius: tree.radius }),
      behaviors: new SolidBehavior(),
    });
  }

The trunk's radius is radius * 0.3, so a bigger tree gets a bigger trunk, without any new numbers to keep track of. And the rigid body is untouched. It's still a circle the size of the whole canopy, so the hero still bumps into the edge of the tree, not the trunk.

The hero will be a pale circle:

    appearance: new FilledCircle({ radius: 0.35, fillColor: "#e9dcc0", lineWidth: 0.05, lineColor: "#8a7a5c" }),

The Jar

The jar is a see-through, rounded box with a light blue outline, plus a brown lid:

    appearance: [new FilledRoundedBox({ width: JAR.width, height: JAR.height, radius: 0.2, fillColor: "#9fd8e833", lineWidth: 0.06, lineColor: "#bfe9f5" }), new FilledBox({ width: 0.7, height: 0.18, fillColor: "#8a6a3c", offset: { dx: 0, dy: -0.6 } })],

The glass is see-through because of the 33 at the end of its fill color. The lid is a second FilledBox, and the offset moves it 0.6 meters up from the center of the actor, so it sits on top of the jar instead of in the middle of it. Every appearance in an array can have its own offset, which lets you build a picture out of several shapes that aren't all stacked in the same spot.

Fireflies That Glow

The fireflies are the heart of the game, so they get the most attention. A glow is hard to draw as one shape, but it's easy to fake with three. (In the code listing below, not a lot changed, but you'll see the whole function, instead of just the changes, so that it's easier to follow along.)

/**
 * Make one firefly at (cx, cy).  It has a CollectableBehavior, so when the hero touches it, it disappears, and the
 * "fireflies" counter goes up by one.  It doesn't wait around forever: it blinks out on its own after `lifetime` seconds.  For the
 * last `flickerTime` seconds of its life it flickers, so the player gets a warning that it's about to go.
 *
 * @param cx          The X coordinate of the firefly's center
 * @param cy          The Y coordinate of the firefly's center
 * @param counter     The name of the counter to bump when it's caught ("fireflies" or "golden")
 * @param color       The firefly's color, as "#rrggbb" (like "#d4ff4a")
 * @param lifetime    How many seconds before it blinks out on its own
 * @param flickerTime How many seconds, at the end of its life, it flickers
 */
function makeFirefly(cx: number, cy: number, counter: string, color: string, lifetime: number, flickerTime: number) {
  // The art is three circles, stacked on top of each other: a big, faint glow,
  // a smaller halo, and a small, bright core.  They're the same color, with
  // different transparency.  Remember that these are base-16 numbers, so "00"
  // is transparent, "ff" is solid color.
  const layers = [
    new FilledCircle({ radius: 0.45, fillColor: color + "22" }), // glow (as big as GLOW_RADIUS)
    new FilledCircle({ radius: 0.26, fillColor: color + "66" }), // halo
    new FilledCircle({ radius: 0.12, fillColor: color }),        // core
  ];
  // Remember the transparency/opacity values, because we change them to make
  // the firefly flicker, so we need to know how to put them back.
  const opacity = ["22", "66", ""];

  const firefly = new Actor({
    // The body (radius 0.3) is smaller than the outer glow (radius 0.45), so just brushing the faint glow doesn't catch the firefly
    appearance: layers,
    rigidBody: new CircleBody({ cx, cy, radius: 0.3 }),
    behaviors: new CollectableBehavior({ onCollect: counter }),
  });

  // For the very last half second, the flicker gets twice as fast
  const fastFlickerTime = 0.5;

  // Every tenth of a second, this timer adds 0.1 to the firefly's age, and
  // decides what the firefly should look like right now.
  let age = 0;
  const ticker = new TimedEvent(0.1, true, () => {
    age += 0.1;
    if (age >= lifetime) {
      // Its time is up: remove the firefly, and stop this timer.  (If the
      // hero already caught it, removing it again does no harm.)
      firefly.remove();
      ticker.cancelled = true;
    } else if (age >= lifetime - flickerTime) {
      // It's almost out of time, so flicker.  `Math.floor(age * 5) % 2` flips
      // between 0 and 1 every 0.2 seconds, and for the last `fastFlickerTime`
      // seconds, `* 10` makes it flip twice as fast.  When `on` is true, each
      // layer gets its lit color back.  When it's false, every layer turns dim
      // by becoming mostly see-through ("30").
      const on = Math.floor(age * (age >= lifetime - fastFlickerTime ? 10 : 5)) % 2 == 0;
      layers.forEach((l, i) => (l.fillColor = on ? color + opacity[i] : color + "30"));
    }
  });
  stage.world.timer.addEvent(ticker);
}

We're drawing a big, faint circle, then a smaller, brighter one, then a small, solid core, all the same color. Stacked on top of each other, they look like a single point of light fading out into the dark. The color itself is now a parameter to makeFirefly: a yellow-green for ordinary fireflies, and a warm orange for golden ones.

This also changed the arguments, so that the color comes in instead of text for a label. All three calls to makeFirefly pass a color: one for the ordinary fireflies that blink on over time, one for the golden ones, and one for the firefly that is already lit when the game starts:

  // A new firefly blinks on every so often, somewhere in the open
  stage.world.timer.addEvent(new TimedEvent(SPAWN_FREQ, true, () => {
    const spot = randomOpenSpot(GLOW_RADIUS);
    makeFirefly(spot.cx, spot.cy, "fireflies", "#d4ff4a", FIREFLY_LIFETIME, FLICKER_TIME);
  }));

  // Golden fireflies are rarer, hide in corners, and blink out faster
  stage.world.timer.addEvent(new TimedEvent(GOLDEN_SPAWN_FREQ, true, () => {
    const spot = GOLDEN_SPOTS[Math.floor(Math.random() * GOLDEN_SPOTS.length)];
    makeFirefly(spot.cx, spot.cy, "golden", "#ffb020", GOLDEN_FIREFLY_LIFETIME, FLICKER_TIME);
  }));

  // One firefly is already lit when the game starts
  makeFirefly(5.5, 4.5, "fireflies", "#d4ff4a", FIREFLY_LIFETIME, FLICKER_TIME);

Be sure to press b so you can see how the firefly's body matches the middle circle. The inner circle is just decoration, and the outer circle doesn't count as a collision, because each has a radius that is different than the radius of the rigidBody. That's on purpose: the glow tells you where a firefly is, and the core tells you where to go.

The flicker code shows one change. Until now, the ticker only had one box to fade. Now there are three circles, and each has its own amount of see-through-ness. The opacity variable remembers each layer's original transparency, so that when the flicker turns the firefly back on, each layer goes back to the way it was:

      layers.forEach((l, i) => (l.fillColor = on ? color + opacity[i] : color + "30"));

forEach runs its code once for each layer, with l as the layer and i as its position in the array. When on is false, every layer turns into the same dim, mostly see-through color. When it's true, layer i gets opacity[i] added back to the end of its color.

The Finished Game

The code for the finished game is here. Below, you'll find a playable link. Be sure to press b to see the hit boxes. You should also compare this version of the game to the one from the end of the last milestone. I think you'll agree that one of them is a lot more fun to play.