top of page

Level Up Your Snake, Part 6: Sound and Pause

  • Writer: Marcel Pflug
    Marcel Pflug
  • 6 hours ago
  • 4 min read

Our Snake is complete from title screen to game over. To finish the series we add the two touches that turn a working game into an enjoyable one: sound, so eating and dying actually make a noise, and a pause function so you can stop for a moment. Then the whole leveled-up game is yours to download.

Turning On the Sound

The Game Boy makes sound through a small chip we control by writing numbers into a handful of special memory slots called registers. Think of each register as a labelled switch or dial on the sound hardware. Before we can make any noise, we flip a few of them on:

void sound_on(void) {
    NR52_REG = 0x80;   // switch the sound hardware on
    NR51_REG = 0xFF;   // send every channel to both speakers
    NR50_REG = 0x77;   // full volume, left and right
}

Each line sets one of those dials. NR52_REG = 0x80 is the master power switch for all sound; nothing is audible until this is on. NR51_REG = 0xFF tells the chip to play every sound channel through both the left and right speaker. NR50_REG = 0x77 sets the volume to full on each side. We call sound_on once when the game starts, and the console is ready to make noise.

A Blip When You Eat, a Tone When You Die

A sound effect is just another set of numbers written to the registers of one sound channel. The values choose the pitch, how long the note lasts, and how loud it is. You do not need to memorise them; here are two ready-made effects, a short high blip for eating and a lower falling tone for dying:

void sfx_eat(void) {   // a short, bright blip
    NR10_REG = 0x00; NR11_REG = 0x81; NR12_REG = 0xA7;
    NR13_REG = 0x00; NR14_REG = 0x86;
}
void sfx_die(void) {   // a low, falling tone
    NR10_REG = 0x36; NR11_REG = 0x80; NR12_REG = 0xF7;
    NR13_REG = 0x00; NR14_REG = 0x83;
}

Each function writes five numbers to the first sound channel: roughly, one sets a pitch sweep, one the length and tone, one the volume, and the last two the frequency of the note before triggering it. The exact values were found by ear, and tweaking them is half the fun. Using them is the easy part: we add a single line sfx_eat(); in the food branch of our game step, right where the score goes up, and a line sfx_die(); at the moment the snake crashes. Suddenly the game has a voice, and every meal and every mistake is something you hear as well as see.

Adding Pause

Finally, a pause. We want START to freeze the game mid-play and START again to resume. A small function, checked once every frame, does the whole job:

void check_pause(void) {
    uint8_t i;
    if (joypad() & J_START) {        // START pressed mid-game?
        waitpadup();                 // wait for the button to be released
        draw_text(8, 8, "PAUSE");     // show the word
        waitpad(J_START);            // freeze here until START again
        waitpadup();
        for (i = 0; i < 5; i++) set_bkg_tile_xy(8 + i, 8, T_EMPTY);
        for (i = 0; i < length; i++) set_bkg_tile_xy(snake_x[i], snake_y[i], T_BODY);
        set_bkg_tile_xy(food_x, food_y, T_FOOD);
    }
}

Reading it through: if (joypad() & J_START) checks whether the player has pressed START during play. If so, waitpadup() waits for them to let go (so one press is not read twice), draw_text writes PAUSE across the middle, and waitpad(J_START) freezes the game right there until START is pressed again. When it is, we tidy up: the first loop erases the five squares the word PAUSE was covering, and the next two lines repaint the snake and the food from memory, so the board looks exactly as it did before we interrupted it. We call check_pause() once each frame inside the game loop, and the game gains a clean pause with only a dozen lines.


Develop your own Game Boy Game - the Pause button for the snake like game
Let's add some convenience - the pause button

Download the Game Files

That completes the leveled-up Snake: a title screen, a hand-drawn font, a live score, three lives, a real game-over screen, sound and pause. You can grab the whole finished project below, the complete snake.c source ready to build. As in the beginner series, turn it into a playable ROM with a single command, adjusting the path to the lcc tool inside your GBDK folder.

lcc -o snake.gb snake.c

Open the resulting snake.gb in an emulator such as Emulicious, BGB or SameBoy, or copy it onto a flash cartridge to play your creation on a genuine DMG-01.




 

The End of the Level

You started with a bare moving snake and, one small feature at a time, turned it into a rounded little game with its own look, sound and feel. Every technique here, drawing tiles, writing text, tracking numbers, structuring the game with loops, and driving the sound hardware, is a real building block used in far larger Game Boy games. If this has whetted your appetite, the beginner series Build Your Own Game Boy Game is where the whole story begins, and the machines it all runs on are waiting in the collection.

The Level Up Series

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page