top of page

Level Up Your Snake, Part 3: Keeping Score

  • Writer: Marcel Pflug
    Marcel Pflug
  • 56 minutes ago
  • 3 min read

Our snake now has a proper title screen to greet the player, but once the game starts it still says nothing about how well you are doing. Time to fix that. In this part we add a score: a number that starts at zero, climbs by one every time the snake eats, and is shown live at the top of the screen using the little font we designed in Part 1. As always, we will read every line in plain English.

A Home for the Score

First the score needs somewhere to live where it will not clash with the game. We keep the very top row of the screen as a status line and, importantly, start the wall one row lower so the playing field sits neatly beneath the score.


Game Boy Create A Game - Add a Score Counter
Adding a Score to the Game

Then a small function writes the labels and numbers onto that top row:

void draw_hud(void) {
    draw_text(0, 0, "SCORE");        // label on the left
    draw_num3(6, 0, score);         // the score number beside it
    draw_text(13, 0, "LIVES");      // label on the right
    set_bkg_tile_xy(19, 0, tile_for('0' + lives));
}

This function, draw_hud (short for heads-up display, the name games give to that status strip), simply writes four things onto row 0, the top row. draw_text(0, 0, "SCORE") puts the word SCORE at the far left. draw_num3(6, 0, score) prints the score itself, as three digits, just after it; we will build draw_num3 in a moment. draw_text(13, 0, "LIVES") writes the second label further along, and the last line draws the number of lives as a single digit at the right edge. Because all of this sits on row 0 and the wall now begins on row 1, the status line and the arena never overlap.

Counting Up

The score is just a number we keep in memory, called score. Back in the beginner series, the moment the snake ate food lived inside our game step. We add three small lines right there:

if (hit == T_FOOD) {          // the head landed on food
    // ... the snake grows, as before ...
    if (score < 999) score++; // add one point (stop at 999)
    draw_num3(6, 0, score);   // redraw the number so it updates
    place_food();             // drop a new piece of food
}

Reading it plainly: the line if (hit == T_FOOD) means only do the following when the square the head moved into was a piece of food. When that happens, score++ adds one to the score (the little guard if (score < 999) just stops the number growing past three digits). We then call draw_num3 again to repaint the number so the player sees it tick upward, and place_food drops a fresh piece somewhere new. Eat, add one, redraw, repeat.

Drawing a Number

There is one puzzle left: our font holds separate tiles for the digits 0 to 9, so to show a score like 42 we must split it into its individual digits and draw each one.


Game Boy Developement: Showing the Score Logic
Showing the Score Logic


That is what draw_num3 does:

void draw_num3(uint8_t col, uint8_t row, uint16_t n) {
    set_bkg_tile_xy(col,     row, tile_for('0' + (n / 100) % 10)); // hundreds
    set_bkg_tile_xy(col + 1, row, tile_for('0' + (n / 10)  % 10)); // tens
    set_bkg_tile_xy(col + 2, row, tile_for('0' +  n        % 10)); // units
}

The trick relies on two everyday-maths symbols. The slash / means divide and throw away any remainder, so 42 / 10 is 4, not 4.2. The percent sign % means the opposite: keep only the remainder, so 42 % 10 is 2 (what is left after taking away the whole tens). Put them together and each digit falls out: n / 100 % 10 is the hundreds digit, n / 10 % 10 is the tens, and n % 10 is the units. The little '0' + in front turns a plain number like 4 into the character '4' so the font can look up the right tile. For a score of 42 that gives a 0, a 4 and a 2, drawn side by side, exactly as the diagram shows.

Coming Up in Part 4

You can now watch your score climb, which raises the stakes nicely, but a single mistake still ends everything at once. In Part 4 we soften that with lives: the snake will start with three, lose one on each crash, and only reach the end when the last one is gone, with the count shown live in the status bar we just built.

The Game Boy whose screen we are filling is documented, with photographs, in the collection.

The Level Up Series

Part 1: Drawing Graphics the Game Boy Way · Part 2: A Title Screen for Your Snake · Part 3: Keeping Score (you are here) · Part 4: Three Lives · Part 5: A Proper Game Over · Part 6: Sound and Pause

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page