Uncategorised

How to Create Your Own Game on the KeyChainino 144

Alessandro Matera 9 min read

Rewritten in October 2026. The first version of this tutorial asked you to install the KeyChainino library. That library doesn't exist any more: every sketch now carries all its files in its own folder.

Hi Guys,

the first KeyChainino, back in 2015, had one game: a brick breaker.

Today the KeyChainino 144 has seventeen. And there’s room for one more.

Yours 🙂

In the meantime the code inside the KeyChainino changed completely: a new LED driver, a new game engine, and nothing to install. So I rewrote this tutorial too. At the end of it you’ll have your own game in the KeyChainino menu, right after the other seventeen.

What you need

  • A KeyChainino 144.
  • A programmer: a USBasp, or an Arduino UNO you already have.
  • The Arduino IDE with MiniCore.

If you’ve never uploaded a sketch to your KeyChainino, start from How to program the KeyChainino. Come back when the seventeen games are on it: from here on, I’ll take that part for granted.

And if you don’t have a KeyChainino yet, you can still follow everything with the simulator. More on that later.

How the KeyChainino draws

144 LEDs, and only 13 pins of the microcontroller to drive them. How?

With Charlieplexing. Every pin can be HIGH, LOW or switched off (an input), so every pair of pins can light two different LEDs, one for each direction of the current. With 13 pins you get 13 × 12 = 156 LEDs. We use 144 of them, a 12 × 12 matrix.

Three microcontroller pins driving six LEDs with Charlieplexing

The trick is that only one line of LEDs is on at any moment. The lines take turns so fast, about 100 times per second, that your eyes see the whole picture. I explained it step by step in this post about Charlieplexing.

But the good news is: you don’t have to think about any of that.

For your game, the screen is just a sheet of squared paper, 12 squares by 12. You color the squares, and the KeyChainino shows the sheet. Every square can be off, dim or bright.

Get the code

  1. On GitHub press Code → Download ZIP, and unzip it.
  2. Open the folder Sketch/KeyChainino_v4_144_LEDs/KeyChainino_v144_multigame.
  3. Open KeyChainino_v144_multigame.ino. All the other files open as tabs next to it.

That’s the firmware every KeyChainino 144 ships with. Nothing to install: everything it needs is in that folder.

Many tabs, I know. But you only need a few of them:

  • game_*.cpp: one file per game. game_catch.cpp is the shortest real one, 117 lines.
  • games.h and registry.cpp: the list of the games in the menu.
  • icons.cpp: the menu pictures.
  • gfx.h and input.h: drawing and buttons.

Everything else (the LED driver, the sleep, the scrolling text, the high scores) works for you in the background.

A game is two functions

Here’s the nice part. A game, on the KeyChainino 144, is just this:

void reset();     // get a new round ready
uint8_t frame();  // move everything by one step and draw it

The engine calls reset() once at the start of every round. Then it calls frame() 50 times a second, every 20 ms, like the pages of a flip book.

In frame() you read the buttons, move your things, draw them, and tell the engine how it’s going:

  • GAME_ON: keep going;
  • GAME_LOSE or GAME_WIN: the round is over.

And that’s all you write.

The engine does everything around it: the face at the start, the score scrolling at the end, the “RECORD!” saved in the memory, holding A+B to go back to the menu, and the sleep when nobody plays. Your game puts its points in engine::score and doesn’t care about the rest.

The tools you have

The squares go from (0, 0), top left, to (11, 11), bottom right. x goes right, y goes down, and the buttons are below the bottom row: A on the left, B on the right.

gfx::clear();                   // all off
gfx::px(x, y, BR_MAX);          // one square: BR_OFF, BR_DIM or BR_MAX
gfx::line(x0, y0, x1, y1, BR_MAX);
gfx::rect(x, y, w, h, BR_DIM);  // also fillRect, circle, hline, vline

input::downA();      // true while A is held (downB() for B)
input::tapA();       // true once for each press (tapB() for B)
input::chordTap();   // A and B pressed together
input::chordHeld();  // A and B held: the player is leaving the game

engine::score++;     // one point

A tip about brightness. Use BR_MAX for the player and for what moves, BR_DIM for the scenery: walls, bricks, the road. It’s how all the seventeen games do it, and it keeps the screen easy to read.

Let’s build a game: Dodge

The rules are simple. A dot falls from the top, in a random column. You’re at the bottom, and you move left and right with A and B. If the dot lands on you, game over. If it falls past you, one point, and the next dot is a bit faster.

Step 1: the game

In the IDE, open the menu at the right end of the tabs (⋯ in the IDE 2, a small arrow in the older 1.8), choose New Tab and call it game_dodge.cpp. Then write this:

/*
 * DODGE: a dot falls from the top. Move left and right so it doesn't
 * land on you. Every dot you dodge is a point, and the next one is faster.
 */
#include "games.h"
#include "gfx.h"
#include "input.h"

namespace dodge {

#define PLAYER_Y (MATRIX_H - 1)  // the player lives on the bottom row
#define MOVE_MS 90               // one step every 90 ms while you hold a button
#define FALL_START_MS 200        // the dot falls one row every 200 ms...
#define FALL_MIN_MS 60           // ...and never faster than every 60 ms

static int8_t playerX;
static int8_t dotX, dotY;
static uint16_t fallMs;            // how long the dot stays on each row
static uint16_t fallAcc, moveAcc;  // milliseconds since the last step

static void newDot() {
  dotX = random(0, MATRIX_W);
  dotY = 0;
}

void reset() {
  playerX = MATRIX_W / 2;
  fallMs = FALL_START_MS;
  fallAcc = 0;
  moveAcc = 0;
  newDot();
}

uint8_t frame() {
  // 1. Move: A goes left, B goes right.
  moveAcc += FRAME_MS;
  if (moveAcc >= MOVE_MS) {
    if (input::downA() && !input::downB() && playerX > 0) {
      playerX--;
      moveAcc = 0;
    } else if (input::downB() && !input::downA() && playerX < MATRIX_W - 1) {
      playerX++;
      moveAcc = 0;
    }
  }

  // 2. Fall. The dot stops while you hold A+B to leave the game.
  if (!input::chordHeld()) fallAcc += FRAME_MS;
  if (fallAcc >= fallMs) {
    fallAcc = 0;
    dotY++;
    if (dotY > PLAYER_Y) {  // it fell past you: one point, and faster
      engine::score++;
      if (fallMs > FALL_MIN_MS) fallMs -= 5;
      newDot();
    }
  }

  // 3. Hit?
  if (dotY == PLAYER_Y && dotX == playerX) return GAME_LOSE;

  // 4. Draw. The engine shows it for you.
  gfx::clear();
  gfx::px(dotX, dotY, BR_MAX);
  gfx::px(playerX, PLAYER_Y, BR_MAX);
  return GAME_ON;
}

}  // namespace dodge

Only one thing in there needs a few words: fallAcc and moveAcc.

frame() runs every 20 ms, but the dot has to fall every 200 ms. So we count. Every frame adds 20 to fallAcc: 20, 40, 60… and when it reaches 200, the dot falls one row and the count starts again.

200 / 20 = 10 frames for each row. And when the dot gets faster, at 60 ms, it’s 60 / 20 = 3 frames. Easy-peasy.

It’s like the kitchen timer of the KeyChainino: it ticks every 20 ms, and every game in the folder uses it the same way.

Step 2: put it in the menu

Now we tell the sketch that Dodge exists. Four small changes.

In games.h, add one line after GAME_MODULE(steroids):

GAME_MODULE(dodge)

In icons.h, add one line after the one for ICON_STEROIDS:

extern const uint16_t ICON_DODGE[12] PROGMEM;

At the end of icons.cpp, draw the icon:

/* Dodge: three dots falling, you at the bottom */
const uint16_t ICON_DODGE[12] PROGMEM = {
  0x000, 0x020, 0x000, 0x000, 0x400, 0x000,
  0x000, 0x002, 0x000, 0x000, 0x040, 0x000
};

Every number is one row, from the top. Every bit is one square: bit 0 is the column on the left. So 0x020 is bit 5, the sixth square from the left, lit. Leave the last row empty: the menu needs it.

In registry.cpp, add the name next to the others:

static const char NAME_DODGE[] PROGMEM = "DODGE";

and your game as the last row of GAME_TABLE, after Steroids:

  { dodge::reset, dodge::frame, NAME_DODGE, LBL_SCORE, ICON_DODGE, 0 },

Always the last row. The position in this table is also the place where the KeyChainino keeps that game’s high score. Put your game in the middle, and every game after it would get the record of its neighbour.

Step 3: room for one more record

In save.cpp, change this line:

#define EE_MAGIC 0x53

into

#define EE_MAGIC 0x54

When the KeyChainino finds a different number there, it clears the records and starts again with room for all eighteen games.

I know, it hurts. Your records on the other games go back to zero.

But without it, Dodge would start with a record that nobody has ever written, most likely 65535. And nobody could ever beat it 🙂

Step 4: play it

You can try it before you upload anything:

  1. In the IDE choose Sketch → Export Compiled Binary. A build folder appears inside the sketch folder, with a .hex file in it.
  2. In the ZIP you downloaded there’s simulator/index.html. Open it in your browser.
  3. Drag the .hex file onto the page.

It’s a KeyChainino 144 in your browser, running your firmware. Hold A+B to turn it on (on the keyboard, ← and → together), press A or B to go through the menu, and press both together to start a game.

Dodge is the last game in the menu, right after Steroids.

When you like it, upload it to the real KeyChainino, as in the programming guide.

What next?

Dodge is small on purpose. Here’s how to make it yours:

  • Two dots at once. Look at game_catch.cpp: it does exactly that, with an array of drops.
  • A real floor. Draw the bottom row with BR_DIM and the player on top of it.
  • Lives. Three hits instead of one, with the lives shown as dim dots in a corner.
  • One step per press. Use input::tapA() instead of input::downA(), and see how it changes the game.

And when you’re stuck, open the other games. They’re all built the same way, with the same two functions. Reading them is the fastest way to learn.

Ah, almost forgot! Would you rather have a KeyChainino with only your game on it? Look at KeyChainino_v144_snake: it’s the same engine with a single game inside.

If you make a game, write to me at info@keychainino.com or open a pull request on GitHub. I really want to play it.

Enjoy!

Leave a Reply

Your email address will not be published. Required fields are marked *

Keep reading

{"cart_token":"","hash":"","cart_data":""}