top of page

Level Up Your Snake, Part 4: Three Lives

  • Writer: Marcel Pflug
    Marcel Pflug
  • 7 hours ago
  • 3 min read

In the beginner game, a single clip of the wall was instantly fatal. That is honest but harsh. In this part we give the player a cushion: three lives. A crash now costs one life instead of the whole game, and only when the last life is spent does the run truly end. The lives count sits in the status bar we built in Part 3, ticking down as you go.

A Lives Counter

Lives are just another number we keep in memory, exactly like the score. At the start of a game we set it to three, and our draw_hud function from Part 3 already knows how to show it at the right of the status bar. So the only real work is deciding what happens when the snake crashes.

Losing a Life, Not the Game

The neat way to do this is with one loop wrapped around another. The outer loop runs once per life; the inner loop is the actual game, the same heartbeat we wrote in the beginner series.


Adding lives functionality to the Game Boy game
Get a second and a third chance

When the snake dies, we simply leave the inner loop, take away a life, and let the outer loop decide whether to hand out another.

lives = 3;
while (lives > 0) {              // keep going while lives remain
    draw_frame();
    new_round();                 // fresh board; score and lives kept
    timer = 0;
    while (1) {                  // one life's game loop
        wait_vbl_done();
        read_input();
        if (++timer >= 6) {      // move every 6th frame
            timer = 0;
            if (step_game()) break;   // crashed: leave this life
        }
    }
    lives--;                     // one life gone
    draw_hud();                  // update the LIVES number
    if (lives > 0) { waitpad(J_START); waitpadup(); }
}

Let us walk through it. lives = 3 gives the player three lives to begin with. while (lives > 0) is the outer loop: it means keep repeating everything inside as long as there is at least one life left. Inside, new_round sets up a fresh board without touching the score or lives, and then the inner while (1) is the ordinary game loop that moves the snake and, thanks to Part 3, updates the score. The important line is if (step_game()) break: step_game reports back whether the snake just died, and break means jump out of the inner loop, ending this life. Once we are out, lives-- takes one life away, draw_hud repaints the new count, and the final line pauses for the player to press START before the next life begins. When lives finally reaches zero, the outer loop stops on its own.

Starting a Fresh Round

Each new life needs a clean board, but the score and remaining lives must survive. We gather that setup into one tidy function:

void new_round(void) {
    clear_arena();   // wipe the play area (but not the status bar)
    draw_hud();      // (re)draw SCORE and LIVES
    spawn_snake();   // a fresh three-segment snake in the middle
    place_food();    // and one new piece of food
}

Each line is one clear job. clear_arena wipes only the inside of the walls, deliberately leaving the top status bar alone so the score does not flicker away. draw_hud repaints the SCORE and LIVES text. spawn_snake places a short new snake back in the middle, ready to go, and place_food drops a single piece of food for it to chase. Because none of this resets the score or lives, the player carries their points from one life straight into the next.

Coming Up in Part 5

The game now ends only when all three lives are gone, but that ending is still just the screen freezing. In Part 5 we give it the send-off it deserves: a proper GAME OVER screen that shows your final score and invites you to press START and try again.

The console behind all this is documented, with photographs, in the collection.


The Level Up Series

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page