Adding a bit of code

Adding a bit of code

Now that our game has been finished we can notice one thing - there is no sound!

There is a small buzzer on the device that is able to produce many different beeps, buzzes, and other crazy noises.

We could also use blocks to add it, but this way we'll explain how to do it by code.

This is the perfect time to copy the entire game code from the right side of the screen and to create a new project!

First, remember to save the project and name it accordingly.

random image

Then, copy the entire code by selecting it, pressing the right mouse button, and clicking 'copy' or by pressing Ctrl+C/Cmd+C.

random image

Go back to the main menu and open a new project.

Make sure to select Nibble and code for your project type.

random image

Your screen should look something like this.
random image

Delete the code that is inside and paste the copied code on an empty screen.

When you upload the code by clicking 'Run', your Nibble should have the exactly same game as it did before!

random image

Now, let's add the sound!

First thing we need to do is add a sound library.

The library we use is called Piezo and you should add the following line anywhere among the #include section.

random image

Now, let's add a bit of tone wheneve we press buttons A or B.

The function we're looking for is Piezo.tone() which has two numbers inside the brackets.

The first number determines the frequency of the tone, while the second number determines the length of the tone.

You can change the numbers up a bit to get different sounds!

random image

One final thing before finishing up - turning the sound on!

Find the main void setup() function and put the following line at the bottom of it.
random image

Now re-upload the game by pressing the red 'Run' button and here it is - you've just added some sounds to your game!

If you wish, you can add even more sounds to make the game even cooler!

Game ideas

On CircuitMess GitHub page you can find many more games for not only the Nibble, but for other consoles as well.

If you study these codes, you will learn how are these games written, which kind of methods are used for different actions, and most importantly - get some ideas for you own new games!

You can also scroll many different forums online, like our CircuitMess Community forum, and exchange knowledge and ideas with people like you all over the world!

Only the sky is the limit - now get to work and code some games!