This portable Arduino-based Game Boy lets you play an Endless Runner game βlike popular mobile games such as Temple Run, Jetpack Joyride or Canabalt, or other infinite platform games like Flappy Bird β with Background Music & Sound Effects! Choose from Harry Potter, Game of Thrones or Legend of Zelda themes, or program in your own!
This game builds on Joshua Brooks' Endless Runner code (see attribution below for link to original code) - it adds background music, sound effects, tracking high scores and being able to toggle between different background music. It also updates the sprites to make the theme more forest-y with trees, and a boy with a wand as the runner (my amateur attempt at creating Harry Potter with a 5x8 character! π). Additionally, everything is in a portable enclosure so you can indulge in some easy game play any time you like, while being able to program and experiment to your heart's content! By making your prototype more robust and "product-like", it makes it more useful and usable!
The music playing happens in the background using the ProtoStax_MmlMusicPlayer Arduino library, allowing you to do other tasks like handling button presses, advancing the game, etc, in the main loop. The ProtoStax_MmlMusicPlayer extends the MmlMusic library to use Arduino Uno's timers to play an entire musical score in the background (not just individual notes).
This project is a followup to the "Multi-Track Music Player" project that debuted the ProtoStax_MmlMusicPlayer library. Interestingly though, the use-case that spurred me to create the ProtoStax_MmlMusicPlayer library in the first place was this game - I needed an easy way to be able to play full score music and sound effects for the game, simultaneously and in the background, allowing the main loop to deal with game progress.
The music is specified in MML notation as strings that can either be PROGMEM or SRAM (regular memory). You can therefore replace the music with your own favorite easily, or add to the list! There are also many libraries of MML scores available, so you can use them (or use them for inspiration as a starting point before adding your own touch!).
You can also modify the sprites to create your own game themes. Read on below for more details.
This portable Arduino Gaming platform with a 16x2 LCD screen, two buttons and two piezo speakers can be used to develop other games as well. But let's first take a look at what we have!
Here is a demo first, so you get an idea of what the final product is like:
Let's now get started with how to make it!
Step 1 : Prepare the HardwareThe video below shows the steps putting together the game box:
I'm using the ProtoStax Enclosure system here to make the Game Boy portable and contain everything within - the Arduino Uno, the LCD screen, buttons and piezo speakers. ProtoStax is a modular enclosure system and you can add functionality and peripherals to it by swapping out/in pieces.
I started with the ProtoStax Enclosure for Arduino, and swapped out the Top Plate with the ProtoStax LCD Kit V2 (which has cut outs and mounting holes for an I2C LCD module (included)). I then swapped out one of the Short-Side walls with a ProtoStax Kit for Momentary Push Button Switches (which includes two panel-mount push button switches).
I used some jumper wires to wire the circuit according to the circuit diagram below. I soldered some jumper wires to the momentary push button switches. I also put the two piezo speakers on a small piece of proto board, so they'd have a little bit of heft to hold the shape, as the wire leads are quite flexible. The ground pins are also shared between the two piezos (and between the two buttons) to reduce the number of connections to the Arduino.
Before you close everything up, you'll first want to adjust the potentiometer on the LCD module to adjust the contrast for best viewing.
You can use the example sketch from the GitHub page (link below), which also has instructions on how to get it running.
The sketch utilizes the following libraries:
- ProtoStax_MmlMusicPlayer
- MmlMusic ( ProtoStax_MmlMusicPlayer extends the MmlMusic library, so we need the base library)
- JC_Button (used for button presses)
- LiquidCrystal_I2C (for the LCD display)
You'll need to follow the instructions to install the above libraries as well.
Step 3: Running itCompile and upload the sketch to your Arduino Game Boy. Press Button1 (the button closer to you, and connected to pin 2 on the Arduino) to start the game. When the game is in progress, Button1 is used to make your runner jump (Harry Potter with his wand) to avoid the oncoming obstacles (trees in a forest!).
While the game is in progress, pressing Button2 (the button further away from you, and connected to pin 3 on the Arduino) will toggle the music between Harry Potter, Game of Thrones or Legend of Zelda!
You also have sound effects when the Runner jumps, or when the Runner hits an obstacle and the game ends.
The game will continue until the runner hits an obstacle. Points are awarded based on distance. The game tracks and displays the high score at the start. The background music will loop over, so it will continue playing as long as you're in the game, racking up high scores! π
You will need to first adjust the potentiometer on your LCD module to get the best contrast for viewing. You should have done this in step 1: Preparing the Hardware. If you have not done so, don't worry. You can open up the top plate of the ProtoStax Enclosure to get access the LCD module and potentiometer - make the adjustments as you see fit, and then close everything up again!
Deeper Dive:Music Macro Language - MML and ProtoStax_MmlMusicPlayerThis project uses the ProtoStax_MmlMusicPlayer library, which makes it easy for you to play sounds/music on the Arduino.
You can specify an entire score to play as a string written in MML (Music Macro Language) syntax. For example, here's the score for Harry Potter theme music:
const char harryPotter[] PROGMEM = {"T144 O6 r2 D4 G.4 A#8 A4 G2 >D4< >C.2< A.2 G.4 A#8 A4 F2 G#4 D.1 D4 G.4 A#8 A4 G2 >D4< >F2< >E4< >D#2< B4 >D#.4< >D8< >C#4< C#2 B4 G.1 A#4 >D2< A#4 >D2< A#4 >D#2< >D4< >C#2< A4 A#.4 >D8< >C#4< C#2 D4 >D.1< r4 A#4 >D2< A#4 >D2< A#4 >F2< >E4< >D#2< B4 >D#.4< >D8< >C#4< C#2 A#4 G.1 r r r r r r r r"};
music2.play_P(harryPotter); // will play whole score in the background without blocking
The T144 specifies the tempo, and the O6 specifies the Octave. The notes are then presented as CDEFGAB etc and with the note duration as C8 (eighth note), D4 (quarter note) etc. Rests are specified as R/r2/R4 etc. Dotted notes are specified as C.2 (a dotted half note). ">" and "<" are used to go up/down an octave - "C4 > C4 <" plays a quarter note of C in the 6th octave followed by a quarter note of C in the 7th octave (the last < brings the octave back down to 6 for anything that follows).
You can create multiple instances of ProtoStax_MmlMusicPlayer to play multiple scores - the number is only limited by the number of available Timers you have. The entire score(s) is(are) played in the background, allowing you to do other activities in your main loop. In our Arduino Game Boy, we use two instances, as the Arduino Uno has two timers available to use. The first instance plays the background music. The second instance is used to play our sound effects - jumping sound for the Runner, or an uh-oh sound when hitting an obstacle!
Modifications to Joshua Brooks' Endless Runner Game EngineWhile I have kept the basic game engine from the original code intact, I have made a bunch of modifications.
I updated the sprites used for the Runner and the Terrain. I utilized the LCD Custom Character Generator for this purpose.
There are 7 different sprites used - Run Position 1, Run Position 2, Jump, Jump Lower, Obstacle, Obstacle Left and Obstacle Right. I tried to make the Runner hold a little wand, like Harry Potter! π Pardon my amateur attempt! For the obstacles, I made trees, like in the Forbidden Forest!
You can unleash your own artistic abilities to create your own game themes by modifying the sprites as you see fit! Combine that with your own theme music and sound FX, and you have your own fun game to play!
I added background music to be played when the game is in play. There are 3 choices - Harry Potter, Game of Thrones or Legend of Zelda. I keep track of Button 2 presses and toggle between the three background music choices whenever Button 2 is pressed during play.
I also added sound effects at various points, such as when the game starts, the Runner jumps, or when the Runner hits an obstacle.
Additionally, I keep track of the high score, and display the high score on the screen when the play ends.
[Fun fact - if you want to cheat and go into "God Mode", just set the PIN_AUTOPLAY to enable auto play!]
Going ForwardAs always, I like to end by talking about how you can take this project forward with your own additions! Once you get comfortable playing around with the code sample and understanding the code, it is always nice to try to extend your learning by doing more.
Here are a few suggestions on how you can take this project forward:
- Create your own sprites using the LCD Custom Character Generator to create your own variation of the game.
- Create your own background music and sound effects by creating your own MML strings that be played.
- Add sound effects when the player has hit a high score - this could either be during game play, or when the game ends! When during game play, you may want to consider a short sound effect so as not to distract the player into crashing into an obstacle! π You could add a short sound effect during game play, and a longer more congratulatory music after the game ends!
- When the player has hit a high score and the game ends, show a congratulatory banner along with the congratulatory music.
- Allow the player to enter his/her name when they get a high score. Have a list of high scores and player names.
- Use the Button2 to toggle a settings/high-score menu screen before game starts - the user can use this settings screen to choose the background music and sound FX settings, for example.
- Create a different game like Flappy Bird that uses the two buttons to control the movement (up or down).
- Use Button 2 (or a series of Button 2 presses) to enable "God Mode" to enable auto play and boost up your score! π
Can you think of other ways to extend this project? Share it with us below! π Also do share photos/videos of your creation on your social media platform of choice - just tag it #ProtoStaxGameBoy #ProtoStax so we can find you!
Happy Making! π
Sridhar
Comments