PVG
2. Fractal Code Cycle — Program Video Games

2. Fractal Code Cycle

You probably already know this pattern: Input -> Processing -> Output. It's one of the first things taught in a computer class.

What you may not have been told, as I certainly wasn't, is that **this simple model is the most important fundamental idea in programming.

What's Really Going On

Whether you're looking at a whole game, a single system, or one tiny function, *they all follow this structure.

The purpose of a system is what it does.

Programs (systems) do one thing: transform data.

It’s "fractal" because the pattern repeats at every scale - from game loops to CPU instructions.

A Closer Look

Here are some examples in descending fractal order:

The Game Loop

Input: Keyboard/Mouse/Gamepad
Process: Update game state, run physics, AI, etc
Output: Render frame, play sounds, etc

Character Controller

Input: Directional input, player speed, delta time
Process: Apply velocity, check collisions
Output: New position, animation state

A Procedure

add :: proc(a, b: int) -> int {
    return a + b
}

Input: Two integers, a and b
Process: The procedure body (between { and })
Output: An integer, the sum of a and b

The CPU (roughly)

Inputs: Two registers
Process: ALU runs the add circuit
Output: One of the registers now contains the result

Zooming Out

At the top level, your game:

  1. Takes input: e.g. joystick movement
  2. Processes it: e.g. movement logic, collisions
  3. Produces output: e.g. player character's new position

But, the same is true inside each function. One function might take two numbers, add them, and return a result.

The same is true inside your CPU. There are physical slots called registers. Data is "put into" these slots (input parameters), and some circuit runs (processing) to produce a result.

Understanding this helps you design clear systems and write code that plugs together cleanly.

Try It Yourself

Try this:

Pick any piece of a programming project (game or otherwise).

Ask:

  • What are the inputs? (What data comes in?)
  • What is the process? (What happens to it?)
  • What are the outputs? (What comes out the other side? And/Or, what side-effects are created?)

Start zoomed out - perhaps your combat system. Then, zoom in - an individual skill or status effect.

The Big Idea

The Fractal Code Cycle is a way of thinking about software as a fractal pattern of Input -> Process -> Output.

When you start programming this way, considering that all you are doing is transforming data from one state into another, everything becomes easier to reason about.

It's not a new idea. But it's often neglected in favour of complicated methods like SOLID.