Spielerando

Lesson 2 of 24One rule for one pixel7 minFree

Where Am I? Stripes by Pixel Position in a Pixel Art Shader

In the last lesson the rule looked at one thing: the number at its own place. Now it also knows where that place is, as a column and a row counted in whole pixels from the top left. With the row alone it lays stripes across a whole picture, and those stripes come out even only if the rule counts rows, not shares of the screen.

A pixel art shed at noon: a shelf of honey jars, a workbench with a straw hive, a loft with more straw hives, and a ladder up to it. Horizontal stripes lie across the whole picture, every second one a shade darker. The beekeeper climbs the ladder up and down through them, and the stripes change from thick to thin to thicker.

The rule knows only the row it is asked for. Every stripe has the same number of rows, so the stripes stay where they are while she climbs through them: her hat is light in one stripe and darker in the next.

Also in this lesson 3 things to read up on 3 questions to check yourself

The idea

A pixel's place is two whole numbers. The column, x, starts at 0 on the left. The row, y, starts at 0 at the top. The picture above is 240 pixels wide and 135 tall, so its top left pixel is at 0, 0 and its bottom right one at 239, 134. The rule is asked for every one of them and is told which one it is.

Some effects belong to a place rather than to a thing: bands of shade where the sun falls through the slats of a shutter, a striped look over a whole room, the lines of a ruled page. This lesson's rule lays such stripes. Each stripe is the same number of rows tall, and every second one is a step darker.

The stripes belong to the place: the beekeeper climbs her ladder through them, and they stay where they are.

How it works

The rule takes its row and divides it by the stripe's height, keeping only the whole part. With stripes of 8 rows, rows 0 to 7 are stripe 0, rows 8 to 15 are stripe 1, and so on down. If the stripe's number is odd, the pixel becomes one step darker; if it is even, the pixel stays as it came. The column plays no part, so a whole row always lies in one stripe.

"One step darker" comes from a table that names, for every colour of the palette, the next darker one: cream becomes sand, sand becomes wood, honey becomes amber. So the answer is still a number of the palette, and an empty pixel stays empty, because the table's entry for nothing is nothing.

The slider sets how many rows tall a stripe is, from 2 to 16. It starts at 8. The shed's wall is made of boards 4 rows tall, so at 4, 8, 12 or 16 rows every board lies wholly in one stripe. That makes it easy to see whether the stripes are even.

What goes wrong

Most shader examples do not give a pixel's place in pixels. They give it as a share of the picture: 0 at the top, 1 at the bottom, 0.5 in the middle. An AI that learned from them works the stripe out from that share. It asks how many stripes of 8 rows fit into the height, gets 16, and cuts the share into 16 equal parts. But 135 rows in 16 parts is 8.44 rows a part, and a pixel cannot be 0.44 of a row. So some stripes come out 9 rows tall and others 8, and the further down you look, the further they drift from where they belong.

A share is not even exact inside a computer. Stripes of 3 rows fit 135 rows exactly, 45 of them, and still the version with the share put rows 39 to 41 into two different stripes.

Switch the picture to "With the mistake" and look at the wall. With the rule, every board is either light or dark. With the mistake, boards are cut in two: a light sliver on a dark board, a dark one on a light board.

Three tests catch it: every stripe must be exactly as many rows tall as asked, all the way down; 45 stripes of 3 rows must fit 135 rows; and the stripes must not depend on how tall the picture is. The last one shows why shares go wrong in a game: put the same effect on a smaller picture, a window inside the game, and with shares its stripes change size.

The mistake, line by line

Lines with - are the rule with the mistake, lines with + the rule as it should be.

- /* Stripes by place, with the mistake: the rule does not count the rows. It takes its place as a share
-    of the picture's height, 0 at the top and nearly 1 at the bottom, the way most shaders are written,
-    and cuts that share into as many stripes as would fit. A share of a height that the stripes do not
-    divide does not come out in whole rows, so some stripes are a row taller than the others, and
-    further down the dark ones lie where light ones should be.
+ /* Stripes by place: the rule is told where its pixel is, as a column and a row in whole pixels counted
+    from the top left, and lays stripes across the whole picture by the row alone. Every stripe is
+    `rows` rows tall, and every second one is one step darker: the shade of the slats of a shutter on a
+    sunny wall, bands across a room, a striped look for a whole screen.

+    The pixel art way: a picture is a grid of whole numbers, 0 where nothing is and 1 to 25 for the
+    palette's colours. The rule is asked for every pixel and answers with a number.
+
     From the course "Shaders for pixel art" on spielerando.com.
     SPDX-License-Identifier: MIT */
…
  }

- /** The rule, wrong: the stripe from a share of the picture's height, cut into as many stripes as fit. */
+ /** The rule: the stripe a pixel lies in comes from its own row, counted from the top in whole rows.
+     Rows 0 to rows - 1 are stripe 0, the next `rows` rows stripe 1, and so on. Every second stripe is
+     one step darker. */
  export function shade(x: number, y: number, _tick: number, src: Frame, p: Params = DEFAULTS): number {
    const n = at(src, x, y);
-   const share = y / src.height;
-   const stripes = Math.floor(src.height / p.rows);
-   const stripe = Math.floor(share * stripes);
+   const stripe = Math.floor(y / p.rows);
    return stripe % 2 === 1 ? DARKER[n] : n;
  }

What the test says about the mistake

✔ the top stripe keeps its colours
✔ the next stripe is one step darker: cream turns sand, honey turns amber
✖ every stripe is exactly as many rows tall as asked, all the way down
    stripes 3 rows tall came out as 4, 3, 3, 4, 3, 3
    got 4, expected 3
✖ stripes that fit the picture exactly come out exactly: 45 stripes of 3 rows in 135 rows
    rows 39 to 41 are not one stripe
    got 2, expected 1
✖ the stripes do not depend on how tall the picture is
    row 3
    got 7, expected 6
✔ the column does not matter: a whole row lies in one stripe
✔ nothing stays nothing, in a light stripe and in a dark one
✔ a place outside the picture is nothing
8 tests: 5 passed, 3 failed

Tell your AI

Write a shader for my game that lays horizontal stripes across the whole picture: every stripe is exactly [8] pixels tall, and every second stripe is one step darker.

- Work in the game's own pixels, counted as whole numbers from the top left: column 0 is at the left, row 0 at the top. My game's picture is [width] by [height] pixels and is shown [how many] times larger on the screen, so work the row out in game pixels, not in screen pixels.
- Do not work with a share of the screen's height and a number of stripes. The stripe is the row divided by the stripe's height, rounded down.
- "One step darker" comes from a table of my palette: for each colour, the next darker colour. [My palette's colours, or the table itself.] Never blend colours: the answer is always a colour of my palette.

Before you write the shader, write the same rule as a small plain function. It takes a grid of whole numbers (0 = empty, 1 to N = the colours of my palette), a column and a row, and returns the number for that one pixel. Test it: every stripe is exactly [8] rows tall all the way down, also in a picture whose height the stripes do not divide; a picture of another height gives the same stripes in the same rows; empty pixels stay empty. Show me the test's output. Then write the shader so that it does exactly what the function does.

The code

Three times the same rule. The first is the one we test; the two shaders are that rule in a shader language, which is what your game runs.

The rule, tested: shader.ts

TypeScript, small, and it needs nothing else. Your AI can read it and write the same rule for whatever your game is built with.

/* Stripes by place: the rule is told where its pixel is, as a column and a row in whole pixels counted
   from the top left, and lays stripes across the whole picture by the row alone. Every stripe is
   `rows` rows tall, and every second one is one step darker: the shade of the slats of a shutter on a
   sunny wall, bands across a room, a striped look for a whole screen.

   The pixel art way: a picture is a grid of whole numbers, 0 where nothing is and 1 to 25 for the
   palette's colours. The rule is asked for every pixel and answers with a number.

   From the course "Shaders for pixel art" on spielerando.com.
   SPDX-License-Identifier: MIT */

export interface Frame {
  width: number;
  height: number;
  index: Uint8Array;
}

export const DEFAULTS = {
  /** How many rows tall one stripe is. */
  rows: 8,
};
export type Params = typeof DEFAULTS;

/** The next darker colour of the palette for each number. [0] is nothing, and nothing stays nothing. */
const DARKER = [0, 24, 25, 2, 3, 15, 7, 20, 9, 20, 21, 10, 13, 25, 12, 16, 14, 25, 17, 18, 21, 24, 25, 22, 24, 24];

/** The number at a place of the picture; outside the picture nothing is. */
export function at(src: Frame, x: number, y: number): number {
  if (x < 0 || y < 0 || x >= src.width || y >= src.height) return 0;
  return src.index[y * src.width + x];
}

/** The rule: the stripe a pixel lies in comes from its own row, counted from the top in whole rows.
    Rows 0 to rows - 1 are stripe 0, the next `rows` rows stripe 1, and so on. Every second stripe is
    one step darker. */
export function shade(x: number, y: number, _tick: number, src: Frame, p: Params = DEFAULTS): number {
  const n = at(src, x, y);
  const stripe = Math.floor(y / p.rows);
  return stripe % 2 === 1 ? DARKER[n] : n;
}

The same rule as a shader: GLSL

For WebGL 2 and OpenGL ES 3.0. Only the part marked as the rule belongs to this lesson; the rest is the same in every lesson.

#version 300 es
// A shader the pixel art way: a pixel is a whole number, 0 where nothing is, 1 to 25 for the
// palette's colours. The rule is shade(): it is asked for every pixel and answers with a number.
// Everything outside the marked part is the same for every rule. Put your own palette in PALETTE.
precision highp float;
precision highp int;

uniform sampler2D SRC;   // the picture, sampled without smoothing (nearest)
uniform sampler2D OTHER; // a second picture, where the rule uses one
uniform int TICK;        // the time in ticks, 60 a second, counted by the game

const vec3 PALETTE[26] = vec3[26](
  vec3(0.0),
  vec3(31.0, 33.0, 66.0) / 255.0, // 1: k
  vec3(75.0, 78.0, 112.0) / 255.0, // 2: n
  vec3(120.0, 123.0, 152.0) / 255.0, // 3: m
  vec3(198.0, 199.0, 227.0) / 255.0, // 4: e
  vec3(255.0, 255.0, 255.0) / 255.0, // 5: w
  vec3(246.0, 241.0, 233.0) / 255.0, // 6: c
  vec3(214.0, 205.0, 189.0) / 255.0, // 7: d
  vec3(242.0, 193.0, 78.0) / 255.0, // 8: y
  vec3(224.0, 138.0, 46.0) / 255.0, // 9: o
  vec3(194.0, 65.0, 45.0) / 255.0, // 10: r
  vec3(244.0, 163.0, 160.0) / 255.0, // 11: p
  vec3(47.0, 98.0, 240.0) / 255.0, // 12: b
  vec3(35.0, 83.0, 214.0) / 255.0, // 13: u
  vec3(157.0, 184.0, 255.0) / 255.0, // 14: s
  vec3(234.0, 240.0, 255.0) / 255.0, // 15: a
  vec3(207.0, 224.0, 255.0) / 255.0, // 16: i
  vec3(15.0, 138.0, 76.0) / 255.0, // 17: g
  vec3(124.0, 207.0, 154.0) / 255.0, // 18: l
  vec3(226.0, 244.0, 234.0) / 255.0, // 19: f
  vec3(138.0, 90.0, 60.0) / 255.0, // 20: t
  vec3(92.0, 59.0, 40.0) / 255.0, // 21: h
  vec3(109.0, 76.0, 230.0) / 255.0, // 22: v
  vec3(201.0, 187.0, 255.0) / 255.0, // 23: q
  vec3(18.0, 20.0, 43.0) / 255.0, // 24: x
  vec3(39.0, 43.0, 82.0) / 255.0 // 25: z
);

// Which colour of the palette is this? 0 where nothing is drawn.
int number(vec4 c) {
  if (c.a < 0.5) return 0;
  int best = 1;
  float least = 9.0;
  for (int i = 1; i <= 25; i++) {
    vec3 d = c.rgb - PALETTE[i];
    float away = dot(d, d);
    if (away < least) {
      least = away;
      best = i;
    }
  }
  return best;
}

// The number at a place of the picture; outside the picture nothing is.
int at(ivec2 p) {
  ivec2 size = textureSize(SRC, 0);
  if (p.x < 0 || p.y < 0 || p.x >= size.x || p.y >= size.y) return 0;
  return number(texelFetch(SRC, p, 0));
}

// The same for the second picture.
int otherAt(ivec2 p) {
  ivec2 size = textureSize(OTHER, 0);
  if (p.x < 0 || p.y < 0 || p.x >= size.x || p.y >= size.y) return 0;
  return number(texelFetch(OTHER, p, 0));
}

// a wrapped into 0 .. n-1, also for a below zero (the % sign is not safe there)
int wrap(int a, int n) {
  return a - n * int(floor(float(a) / float(n)));
}

// ---- the rule ----
uniform int rows; // how many rows tall one stripe is

// the next darker colour of the palette for each number; [0] is nothing, and nothing stays nothing
const int DARKER[26] = int[26](0, 24, 25, 2, 3, 15, 7, 20, 9, 20, 21, 10, 13, 25, 12, 16, 14, 25, 17, 18, 21, 24, 25, 22, 24, 24);

// the stripe a pixel lies in comes from its own row, counted from the top in whole rows:
// rows 0 to rows - 1 are stripe 0, the next rows rows stripe 1, and so on.
// Every second stripe is one step darker.
int shade(ivec2 p) {
  int n = at(p);
  int stripe = p.y / rows;
  return stripe % 2 == 1 ? DARKER[n] : n;
}
// ---- end of the rule ----

out vec4 FRAGMENT;

void main() {
  // the pixel's place, counted from the top left as in the picture
  ivec2 p = ivec2(int(gl_FragCoord.x), textureSize(SRC, 0).y - 1 - int(gl_FragCoord.y));
  int n = shade(p);
  FRAGMENT = n == 0 ? vec4(0.0) : vec4(PALETTE[n], 1.0);
}

The same rule for Godot 4

The checked shader with Godot's names around it. We have not run this one: Godot is not part of our checks yet, so let your AI compare its picture with the one on this page.

shader_type canvas_item;
// A shader the pixel art way: a pixel is a whole number, 0 where nothing is, 1 to 25 for the
// palette's colours. The rule is shade(): it is asked for every pixel and answers with a number.
// Everything outside the marked part is the same for every rule. Put your own palette in PALETTE.
//
// Put this on a ColorRect that covers the screen, above everything it should change.
// The rule counts pixels as this shader sees them. Let the game draw at the size of its art and
// enlarge the finished picture (the project's stretch mode "viewport"), or it counts window pixels.
uniform sampler2D SRC : hint_screen_texture, filter_nearest; // what is on the screen behind it
uniform sampler2D OTHER : filter_nearest; // a second picture, where the rule uses one
// The time in ticks, 60 a second. The game counts it itself: add 1 in the _physics_process of a
// node that pauses with the game, and set it there, so a paused game pauses its shaders:
//   tick += 1
//   material.set_shader_parameter("TICK", tick)
uniform int TICK;

const vec3 PALETTE[26] = vec3[26](
  vec3(0.0),
  vec3(31.0, 33.0, 66.0) / 255.0, // 1: k
  vec3(75.0, 78.0, 112.0) / 255.0, // 2: n
  vec3(120.0, 123.0, 152.0) / 255.0, // 3: m
  vec3(198.0, 199.0, 227.0) / 255.0, // 4: e
  vec3(255.0, 255.0, 255.0) / 255.0, // 5: w
  vec3(246.0, 241.0, 233.0) / 255.0, // 6: c
  vec3(214.0, 205.0, 189.0) / 255.0, // 7: d
  vec3(242.0, 193.0, 78.0) / 255.0, // 8: y
  vec3(224.0, 138.0, 46.0) / 255.0, // 9: o
  vec3(194.0, 65.0, 45.0) / 255.0, // 10: r
  vec3(244.0, 163.0, 160.0) / 255.0, // 11: p
  vec3(47.0, 98.0, 240.0) / 255.0, // 12: b
  vec3(35.0, 83.0, 214.0) / 255.0, // 13: u
  vec3(157.0, 184.0, 255.0) / 255.0, // 14: s
  vec3(234.0, 240.0, 255.0) / 255.0, // 15: a
  vec3(207.0, 224.0, 255.0) / 255.0, // 16: i
  vec3(15.0, 138.0, 76.0) / 255.0, // 17: g
  vec3(124.0, 207.0, 154.0) / 255.0, // 18: l
  vec3(226.0, 244.0, 234.0) / 255.0, // 19: f
  vec3(138.0, 90.0, 60.0) / 255.0, // 20: t
  vec3(92.0, 59.0, 40.0) / 255.0, // 21: h
  vec3(109.0, 76.0, 230.0) / 255.0, // 22: v
  vec3(201.0, 187.0, 255.0) / 255.0, // 23: q
  vec3(18.0, 20.0, 43.0) / 255.0, // 24: x
  vec3(39.0, 43.0, 82.0) / 255.0 // 25: z
);

// Which colour of the palette is this? 0 where nothing is drawn.
int number(vec4 c) {
  if (c.a < 0.5) return 0;
  int best = 1;
  float least = 9.0;
  for (int i = 1; i <= 25; i++) {
    vec3 d = c.rgb - PALETTE[i];
    float away = dot(d, d);
    if (away < least) {
      least = away;
      best = i;
    }
  }
  return best;
}

// The number at a place of the picture; outside the picture nothing is.
int at(ivec2 p) {
  ivec2 size = textureSize(SRC, 0);
  if (p.x < 0 || p.y < 0 || p.x >= size.x || p.y >= size.y) return 0;
  return number(texelFetch(SRC, p, 0));
}

// The same for the second picture.
int otherAt(ivec2 p) {
  ivec2 size = textureSize(OTHER, 0);
  if (p.x < 0 || p.y < 0 || p.x >= size.x || p.y >= size.y) return 0;
  return number(texelFetch(OTHER, p, 0));
}

// a wrapped into 0 .. n-1, also for a below zero (the % sign is not safe there)
int wrap(int a, int n) {
  return a - n * int(floor(float(a) / float(n)));
}

// ---- the rule ----
uniform int rows; // how many rows tall one stripe is

// the next darker colour of the palette for each number; [0] is nothing, and nothing stays nothing
const int DARKER[26] = int[26](0, 24, 25, 2, 3, 15, 7, 20, 9, 20, 21, 10, 13, 25, 12, 16, 14, 25, 17, 18, 21, 24, 25, 22, 24, 24);

// the stripe a pixel lies in comes from its own row, counted from the top in whole rows:
// rows 0 to rows - 1 are stripe 0, the next rows rows stripe 1, and so on.
// Every second stripe is one step darker.
int shade(ivec2 p) {
  int n = at(p);
  int stripe = p.y / rows;
  return stripe % 2 == 1 ? DARKER[n] : n;
}
// ---- end of the rule ----

void fragment() {
  ivec2 p = ivec2(SCREEN_UV * vec2(textureSize(SRC, 0)));
  int n = shade(p);
  COLOR = n == 0 ? vec4(0.0) : vec4(PALETTE[n], 1.0);
}
The test: shader.test.ts
import assert from "node:assert/strict";
import { test } from "node:test";
import { type Frame, shade } from "./shader.ts";

/** A tiny picture from rows of digits: 0 is nothing, 1 to 9 are colours. */
function picture(rows: string[]): Frame {
  return { width: rows[0].length, height: rows.length, index: Uint8Array.from(rows.join(""), Number) };
}

/** A strip of cream wall (6), one pixel wide and `height` rows tall. */
const strip = (height: number) => picture(Array.from({ length: height }, () => "6"));

/** A shelf on a cream wall: a honey jar (8) with a dark lid (1), and an empty corner (0). */
const SHELF = picture([
  "6116",
  "6886",
  "6886",
  "6660",
]);

/** The rule's answers down one column, from the top. */
const column = (src: Frame, x: number, rows: number) => Array.from({ length: src.height }, (_, y) => shade(x, y, 0, src, { rows }));

test("the top stripe keeps its colours", () => {
  // with stripes 2 rows tall, rows 0 and 1 are the top stripe
  assert.deepEqual([0, 1, 2, 3].map((x) => shade(x, 0, 0, SHELF, { rows: 2 })), [6, 1, 1, 6]);
  assert.deepEqual([0, 1, 2, 3].map((x) => shade(x, 1, 0, SHELF, { rows: 2 })), [6, 8, 8, 6]);
});

test("the next stripe is one step darker: cream turns sand, honey turns amber", () => {
  // rows 2 and 3 are the second stripe: cream (6) becomes sand (7), honey (8) becomes amber (9)
  assert.deepEqual([0, 1, 2, 3].map((x) => shade(x, 2, 0, SHELF, { rows: 2 })), [7, 9, 9, 7]);
  // and with stripes 1 row tall, row 1 is already the second stripe
  assert.deepEqual([0, 1, 2, 3].map((x) => shade(x, 1, 0, SHELF, { rows: 1 })), [7, 9, 9, 7]);
});

test("every stripe is exactly as many rows tall as asked, all the way down", () => {
  for (const rows of [3, 4, 6]) {
    const answers = column(strip(20), 0, rows);
    // cut the column into runs of the same answer: every run but the last one at the bottom is `rows` long
    const runs: number[] = [];
    for (let y = 0; y < answers.length; y++) {
      if (y > 0 && answers[y] === answers[y - 1]) runs[runs.length - 1]++;
      else runs.push(1);
    }
    for (const run of runs.slice(0, -1)) assert.equal(run, rows, `stripes ${rows} rows tall came out as ${runs.join(", ")}`);
    assert.ok(runs[runs.length - 1] <= rows);
  }
});

test("stripes that fit the picture exactly come out exactly: 45 stripes of 3 rows in 135 rows", () => {
  const answers = column(strip(135), 0, 3);
  for (let y = 0; y < 135; y += 3) {
    assert.equal(new Set(answers.slice(y, y + 3)).size, 1, `rows ${y} to ${y + 2} are not one stripe`);
    if (y > 0) assert.notEqual(answers[y], answers[y - 1], `row ${y} does not start a new stripe`);
  }
});

test("the stripes do not depend on how tall the picture is", () => {
  // each of the top 10 rows is in the same stripe in a picture of 10 rows as in one of 17 or of 135
  for (let y = 0; y < 10; y++) {
    const short = shade(0, y, 0, strip(10), { rows: 3 });
    assert.equal(shade(0, y, 0, strip(17), { rows: 3 }), short, `row ${y}`);
    assert.equal(shade(0, y, 0, strip(135), { rows: 3 }), short, `row ${y}`);
  }
});

test("the column does not matter: a whole row lies in one stripe", () => {
  const wall = picture(["666666666"]);
  for (const rows of [1, 2, 5]) {
    const answers = [...Array(9).keys()].map((x) => shade(x, 0, 0, wall, { rows }));
    assert.equal(new Set(answers).size, 1);
  }
});

test("nothing stays nothing, in a light stripe and in a dark one", () => {
  // the empty corner is in the top stripe when stripes are 4 rows tall, and in a dark one when they are 2
  assert.equal(shade(3, 3, 0, SHELF, { rows: 4 }), 0);
  assert.equal(shade(3, 3, 0, SHELF, { rows: 2 }), 0);
});

test("a place outside the picture is nothing", () => {
  assert.equal(shade(0, -1, 0, SHELF, { rows: 2 }), 0);
  assert.equal(shade(4, 2, 0, SHELF, { rows: 2 }), 0);
  assert.equal(shade(0, 4, 0, SHELF, { rows: 2 }), 0);
});
What we checked
  • 8 tests pass on the rule.
  • With the mistake put in, 3 of them fail: "every stripe is exactly as many rows tall as asked, all the way down", "stripes that fit the picture exactly come out exactly: 45 stripes of 3 rows in 135 rows", "the stripes do not depend on how tall the picture is".
  • The mistake shows in the picture above: switch to "With the mistake".
  • The shader ran in a browser (Chrome/158.0.8127.37) on 10 pictures and gave the rule's picture in 324,000 of 324,000 pixels.

Run by pnpm courses:check on 2026-11-27 with Node v24.15.0.

Try it

Read up

Where to go deeper: the pages of ours this lesson builds on, and what others have written about its point.

Check yourself: three questions

Each answer is checked and explained at once. The questions are part of the quiz, so they count for your Game Coder IQ like any other.

Question 1 of 3Easy

The stripe rule of this lesson is told its pixel's column and its row. Which of the two decides whether the pixel lies in a dark stripe?

All questions of this course · The quiz and the Game Coder IQ

Next lesson Time in Ticks: an Even Conveyor Belt Shader for Pixel Art

Give the whole lesson to your AI

The button copies what to tell it together with the tested rule, its test and both shaders, so your AI has everything to build this in your game and to show you that it works.

How useful was this page?

Nobody has rated this page yet.

One click, no account. It tells us which pages to make better next.

Last updated: 2026-11-27