•
5 min read
Moving the Whole Tetromino, and Rendering It as SVG
How Elixir Translates Tetromino Points and Renders Them With Kino

At the end of Blog #19, one field on the tetromino was still waiting to do its job:

%Tetromino{name: :z, location: {3, 1}, rotation: 90, color: :red}

to_group/1 could choose the shape and rotate its points, but those points still lived inside their own 4x4 grid. The tetromino knew its board location. Its points did not.

That leaves a visible mismatch. The shape is right, but it appears in the wrong place. To put it on the board, we need to move every point by the offset in location.

This change finishes the movement layer and opens the next problem. A correctly positioned list of points is useful data, but it is not yet something we can see.

Follow the Compiler Down a Layer

The group owns the list of points, so movement starts there:

def move_to(points, location) do
  Enum.map(points, &Point.move_to(&1, location))
end

Enum.map/2 sends each point to Point.move_to/2. The capture keeps location fixed while &1 changes for every point in the group.

Compile that code and Elixir reports that Point.move_to/2 does not exist. The error tells us which layer is incomplete. Group knows how to apply an operation across a shape, while Point needs to know how one coordinate moves.

At the point layer, movement is coordinate addition:

def move_to({from_row, from_col}, {to_row, to_col}) do
  {from_row + to_row, from_col + to_col}
end

A point at {1, 2} moved by {3, 1} becomes {4, 3}. That tiny function can be tested without constructing a board or opening Livebook. Layered code gives us more named pieces, but each piece carries less knowledge.

🎯 Join Groxio's Newsletter

Weekly lessons on Elixir, system design, and AI-assisted development -- plus stories from our training and mentoring sessions.

We respect your privacy. No spam, unsubscribe anytime.

Put the Location Into the Pipeline

Here is the practical change in to_group/1. Before, the pipeline stopped after rotation:

def to_group(tetro) do
  points = case tetro.name do
    # the seven shapes
  end

  Group.rotate(points, tetro.rotation)
end

After, the rotated group moves to the tetromino’s board location:

def to_group(tetro) do
  points = case tetro.name do
    # the seven shapes
  end

  points
  |> Group.rotate(tetro.rotation)
  |> Group.move_to(tetro.location)
end

The order matters. Rotation happens in the shape’s local grid, then translation places the finished shape on the board. If those operations were reversed, the rotation would also turn the board offset and move the piece somewhere else. The rule is simple: transform locally first, position globally second.

Now the reducers can produce a sequence of complete tetromino values:

Tetromino.new(:z)
|> Tetromino.rotate()
|> Tetromino.fall()
|> Tetromino.right()

Each call returns the next value, so the same pipeline can stand in for game input. Bruce describes the result in the live book:

“What this allows us to do is build the Tetris game frame by frame, using functions as if somebody clicked a joystick, one unit to the left, or one unit to the right, or hit the button to rotate an individual piece.”

— Bruce Tate

A keyboard or game loop can call these reducers later. The movement code does not care where the command came from.

Turn Points Into an Image

To see those values move, we need a renderer. Bruce searches hex.pm for Livebook rendering, finds Kino, and follows the documentation to Kino.Image. This is a useful development habit: begin with the capability you need, then read enough of the package documentation to find the API that fits.

Kino can render SVG, and SVG is text with a visual meaning. A rectangle is an element with coordinates, dimensions, and a fill color:

svg = """
<svg width="200" height="200">
  <rect x="60" y="20" width="20" height="20" fill="purple" />
</svg>
"""

Kino.Image.new(svg, :svg)

That changes the rendering task into a conversion. For each {row, col} point, build a <rect> tag, using col for x and row for y, scaled by the block size. Join those rectangles inside one <svg> element and pass the string to Kino.

This boundary is useful because the game model stays independent of SVG. Tetromino functions return coordinates, and the canvas decides how those coordinates look. A different renderer could consume the same points without changing movement at all.

The homework is to replace the hand-written canvas dependency with Kino, then implement Canvas.new/1 for the full board and Canvas.tetro/1 for a piece’s 4x4 grid. Both functions receive points and produce SVG that Kino can render.

From Coordinates to Characters

Once you treat SVG as a text format, drawing stops being mysterious. Your reducers decide where the piece belongs, and the canvas translates that final position into characters a renderer understands.

The next pressure appears when a board requires many rectangle strings on every frame. Blog #23 follows that pressure into IO lists, where Elixir gives us a better way to assemble large amounts of text.


📚 Build Elixir Systems From Clear Mental Models

This comes from our Learning Elixir course, where Bruce teaches production architecture decisions through real-world scenarios. Learn how layered transformations and clean rendering boundaries keep game logic easy to reason about as systems grow. Available via monthly subscription -- try it for one month.

— Paulo & Bruce

Bruce Tate's avatar
Bruce Tate
System architecture expert and author of 10+ Elixir books.
Paulo Valim's avatar
Paulo Valim
Full-stack Elixir developer and educator teaching modern Elixir and AI-assisted development.