Flipper TD: Building a Tower Defense Game for the Flipper Zero

Flipper TD: Building a Tower Defense Game for the Flipper Zero

Tower Defense Game for the Flipper Zero

If you've ever held a Flipper Zero, you know the magic it holds. It's a powerful, pocket-sized multi-tool for hackers, makers, and tinkerers. But beyond its primary functions, it's also a fantastic platform for creating homebrew games. There's a certain charm in building something for its crisp 128x64 monochrome screen, a throwback to the golden age of handheld gaming. Full code available on Github

It's with that spirit that I began work on Flipper TD, a classic tower defense game built from the ground up to run on the Flipper Zero. This project is a labor of love and a deep dive into the Flipper's Furi SDK.

Please note, Flipper TD is very much a work in progress! This blog post is a look behind the curtain at what the game is, how it works, and where it's headed.

What is Flipper Tower Defense? The Gameplay

The goal of Flipper TD is simple and should be familiar to any fan of the genre: stop waves of enemies from crossing the screen.

You start with a set amount of lives and gold. Enemies spawn from a corner of the screen and will try to make their way to the opposite corner. If an enemy makes it to the exit, you lose a life. If you run out of lives, it's game over.

To stop them, you must strategically place defensive towers on the game grid. Each enemy you defeat rewards you with gold, which you can then use to build more towers. With each passing wave, the enemies get tougher, faster, and more numerous.

The current version features four distinct tower types, each with a unique role:

  • (N) Normal Tower: Your standard, reliable damage dealer.
  • (R) Range Tower: Sacrifices some power for the ability to shoot enemies from further away.
  • (S) Splash Tower: Deals damage in a small area, perfect for handling clustered groups of enemies.
  • (F) Freeze Tower: Doesn't deal much damage, but its projectiles temporarily freeze enemies in place, making them easy targets for your other towers.

How It Works: A Peek Under the Hood

Building a game, even a simple one, involves solving some interesting technical challenges. Flipper TD is built entirely in C using the Flipper's Furi SDK. Let's explore some of the core components.


The Central Game State

At the heart of the application is a massive C struct called GameState. This structure is the single source of truth for everything happening in the game at any given moment. It holds:

  • The player's current lives, gold, and wave number.
  • A 2D array representing the grid, storing the type of tower at each position.
  • The cursor's current position on the grid.
  • Arrays to hold all the active enemies and projectiles on the screen, along with their individual properties like HP, position, and velocity.
  • Timers to manage the delay between waves and the spawning of individual enemies.
struct GameState {
    int lives;
    int gold;
    int wave;
    TowerType grid[GRID_WIDTH][GRID_HEIGHT];
    Coord cursor;
    Enemy enemies[MAX_ENEMIES];
    Projectile projectiles[MAX_PROJECTILES];
    int pre_wave_timer;
    int wave_spawn_timer;
    int wave_spawn_index;
};

Pathfinding: The Brains of the Operation

The most critical challenge in any tower defense game is pathfinding. Enemies need to be smart enough to navigate around the towers you place. If you build a wall, they need to find a way around it.

Flipper TD Pathfinding

Flipper TD accomplishes this using a Breadth-First Search (BFS) algorithm.

Here is a conceptual look at what the screen might look like, and the path the BFS algorithm finds:

+--------------------------------+
| Lives:10 Gold:100 Wave:1       |
+--------------------------------+
| E -> E -> v . . . . . . . . . .|
| . . . . E v . . . . . . . . . .|
| . T . . E v . T . . . T . . . .|
| . . . . E v . . . . . . . . . .|
| . . . . E . . . . . . . . . . .|
| . . . . ^ < < < < < < < < . X .|
+--------------------------------+

(A mockup of the game screen showing an enemy path (E) around towers (T) towards the exit (X))

The find_path() function builds a "map" of the grid and explores it step-by-step from the start point, until it finds the end point. This ensures the enemies always take the shortest possible route.

Crucially, the game uses this same function to protect the player from themselves. When you try to place a tower, the game temporarily places it and runs the BFS algorithm. If no path can be found, the placement is rejected, and your tower isn't built. This prevents you from accidentally creating an impossible-to-win scenario by completely walling off the path.

// Logic for placing a tower
if(game->grid[game->cursor.x][game->cursor.y] == TOWER_NONE) {
    if(game->gold >= 10) {
        // Temporarily place the tower
        game->grid[game->cursor.x][game->cursor.y] = TOWER_NORMAL;

        // Check if a path still exists
        if(find_path(game, start, end, test_path, &test_path_length)) {
            // Path exists, so the placement is valid. Finalize it.
            game->gold -= 10;
        } else {
            // No path! Revert the placement.
            game->grid[game->cursor.x][game->cursor.y] = TOWER_NONE;
        }
    }
}

The Game Loop

The entire application runs inside the flipper_td_app function. It's a continuous loop that does three main things, over and over:

  1. Process Input: It checks a message queue for any events, like a button press from the Flipper's D-pad. If you press a button, the game updates the cursor position or tries to build/sell a tower.
  2. Update State: This is where all the game logic happens. The update_enemies(), update_tower_firing(), and update_projectiles() functions are called. Enemies move, towers check for targets, projectiles fly, and collisions are detected.
  3. Render: After all the logic is updated, the draw_game() function is called. It clears the screen and redraws everything in its new position—the status bar, the grid, the towers, enemies, and projectiles. This happens so fast that it creates the illusion of smooth motion.

The Road Ahead

As mentioned, Flipper TD is still early in development. The core mechanics are in place, but there is a long list of features I'm excited to work on next:

  • Real Graphics: Replacing the simple letter characters ('N', 'R', 'S', 'F') with actual pixel art for the towers and enemies.
  • Game Economy: A more robust economy with varying costs for towers and different gold rewards for different enemy types.
  • Tower Upgrades: The ability to spend gold to upgrade existing towers, improving their damage, range, or special abilities.
  • UI/UX Polish: A proper main menu, a clear "Game Over" screen, and better visual feedback for game events.
  • Sound! Adding simple sound effects for firing, explosions, and enemy deaths would bring the game to life.

This project has been a fantastic learning experience and a fun way to engage with the Flipper Zero on a deeper level. If you're interested in contributing, reporting a bug, or just tracking the progress, head over to the Flipper TD GitHub repository. Stay tuned for more updates!

← Back to Blog