Sprites as strings

Madlands is a free bullet hell MMO that runs in the browser. It has no sprite sheets and no image files for its characters. Every hero, monster and bullet is a list of strings in the TypeScript source, and the game bakes them into textures when it loads.

Here are the game's 17 classes, drawn on this page just now from the game's own art code:

Yes, a list of strings with a palette is a bitmap. It's an indexed-color image in a text format I made up. The format isn't the interesting part. What matters is that the art lives in the code, so code can read it, change it and build more of it.

A hero is 16 strings

Every actor is drawn on one grid: 16 pixels to a tile. A one-tile hero is 16 rows of 16 characters. Each letter is a material, and . is empty. This is the wizard, facing the camera:


          

    Some letters mean the same thing on every hero. h is the player's dye, H, j and l are its shadow, deep shadow and highlight, s is skin, e eyes and y gold. Each class adds its own letters: the wizard's w is the beard, r the robe, t the staff.

    Lighting is a function

    I draw each material flat, in one color, and let code light it. A pixel whose edges face the upper left take the material's light shade; one facing the lower right takes its dark shade. Every piece of a hero reads as its own beveled part, and I never shade by hand.

    // ramps: each material's light and dark key, e.g. h: "lH" (dye: light l, dark H)
    function lit(rows: string[], ramps: Record<string, string>): string[] {
      const at = (x: number, y: number) => rows[y]?.[x] ?? ".";
      return rows.map((row, y) => [...row].map((k, x) => {
        const ramp = ramps[k];
        if (!ramp) return k;
        const edge = (n: string) => +(n !== k);
        const up = edge(at(x - 1, y)) + edge(at(x, y - 1));
        const down = edge(at(x + 1, y)) + edge(at(x, y + 1));
        return up > down ? ramp[0] : down > up ? ramp[1] : k;
      }).join(""));
    }

    The output is more strings, so the next step doesn't care whether a pixel was typed or computed. Then the baker traces an ink outline around the whole shape:

    Dyes are a palette swap

    Players dye their heroes. Because every hero is drawn in the same few dye letters, a dye is just four colors worked out from one:

    h: dye, H: shade(dye, -0.3), j: shade(dye, -0.55), l: shade(dye, 0.35)

    Code draws half the frames

    Each hero has a front, back and side view, and each view has stand, step and attack frames. I draw the stand and the attack. The step is the stand with its bottom two rows replaced:

    side: view(archSide, archSideAttack, legs("....BB...BB..W..", "...kkk....kkk..."))

    The rest comes from code. otherStep() finds the gap between the legs in each row and mirrors the moved leg across it, so a hero walks on both feet though I only drew one. attackStep() takes every pixel the step changed and lays it on the attack frame, so a hero firing on the move keeps walking. Doing that on PNGs would mean image processing. On strings, it's a loop over characters.

    Big monsters are painted, then become strings

    Typing a 64-pixel boss letter by letter is slow. Some monster families are built with shape helpers instead: rectangles, polygons and ovals that rasterize onto the same grid and put out the same strings. Everything after that, lighting, outline and baking, works the same.

    const p = pixelPainter(24, 18, { g: "#5ec85a", G: "#a6f28e", d: "#2c7438", e: "#1a1420", w: "#ffffff" });
    p.oval(12, 11, 11, 7, "g");                       // body
    p.rect(7, 8, 2, 3, "e"); p.rect(15, 8, 2, 3, "e"); // eyes
    p.dot(7, 8, "w"); p.dot(15, 8, "w");               // a glint in each
    const slime = lit(p.art().rows, { g: "Gd" });

    Try it

    This is the wizard's frame from above. Change any letter and the wizard is drawn again, lit and outlined the way the game does it. Try h for dye, y for gold, or . to erase.

    From strings to the screen

    At load, and again when the zoom changes, each frame is baked once onto a canvas at a whole multiple of the screen's pixels, so nothing is ever resampled or blurred. The ink outline comes from stamping a small disc around every filled pixel, so it also outlines holes inside a sprite and leaves no gaps at diagonal corners. A soft dark glow goes behind that, and the canvas becomes a nearest-neighbor texture. The last three zoom levels stay baked, so zooming back costs nothing.

    Why bother?

    Partly because it's fun. But the art shows up in git diff like any other change, I can grep it, and a lot of what would be more drawing is a function instead. The catch is there's no paint tool: I see my art by running the game or showing it on a page like this one.

    The game is free in the browser, no sign-up: playmadlands.com.