Introduction: Arduino Space Rocks Game
Whether they are played on a computer, on a phone, on a game console, or on a standalone box, a lot of video games include an element of obstacle avoidance. Sure, there may be points awarded for collecting tokens or finding your way through a maze, but rest assured that there is probably something in the game whose sole purpose is to prevent you from doing that. The first video game was Pong, but after that the most popular games were things like “Asteroids” or “Pac-Man”. A more recent variation would be the simple but addictive game of “Flappy Birds”.
Recently I saw that someone had made a simple two-level version of “Flappy Bird” that was played on a common 1602 LCD. I thought that would be something the grandkids might like so I decided to do my own variation from scratch. The 1602 version only has two levels so I decided to use a 2004 LCD (20x4) instead to slightly increase the difficulty of play. I also opted to make it more like “Asteroids” by having the player guide a “ship” through a maze of “space rocks”. Even if you aren’t interested in building the game, there might be some elements of the software that you can use in one of your own projects.
Step 1: Hardware
The hardware can be based on pretty much any Arduino version. I did the prototyping using a Nano and then burned the code into an ATMega328 chip. That is the same chip used in the Nano but using it by itself allows for a more compact build and less power consumption. As you can see, I built the circuit on a small breadboard that piggybacks on the LCD module. The other aspect that is different is that the Nano runs at 16-MHz using an external crystal but I chose to use the built-in 8-MHz oscillator for the ATMega328 chip. That saves parts and power.
The 2004 LCD interfaces to the Arduino the same way as a 1602 LCD. An interesting difference is in the addressing of the display locations. Obviously there is a difference because there are four lines instead of two but, in the 2004, the third line is an extension of the first line and the fourth line is an extension of the second line. In other words, if you had a test program that just sent out a string of characters to the LCD, the 21st character would show up at the start of the third line and the 41st character wraps back around to the start of the first line. I use that characteristic in the software to effectively double the length of the maze.
I decided to make my version battery powered so I used a common 18650 Li-ion, 3.6-volt battery. That required that I add a small board to allow for USB recharging and another small board to boost the battery voltage to 5 volts for the LCD and the ATMega chip. The pictures show the modules I used but there are also all-in-one modules that do both functions.
Step 2: Software
The software is the same for both the Nano and the ATMega328 chip. The only difference is in the programming method. I use my own barebones version of 1602 LCD software and the LCD software in this project is based on that. I did need to add capabilities to address the extra lines of the 2004 display and also added routines for shifting the display. The display shift provides the movement effect of the “rocks” past the “ship”.
As mentioned earlier, lines 1 and 3 form a circular queue and lines 2 and 4 do as well. That means that after 20 shifts, lines 1 and 3 are swapped and lines 2 and 4 are swapped. After 40 shifts the lines are back in their original positions. Because of this behavior, the original 20-character maze becomes completely different when the lines swap. That made life interesting when I tried to form a maze. I finally just opened up an Excel spreadsheet so I could chart the path without having to constantly change the software. The software provided here has two versions of the maze (one is commented out) so you can choose which one you want or make up your own.
I originally wanted this to be simple enough that the young grandkids could play it, but I also wanted it to have some extra challenge if they (or someone else) got too good at it. The game starts with the shift rate set at 1 second. The internal tic rate is 50ms so that means that there are 20 intervals during which the up/down buttons can be pressed. In actuality, a pressed button consumes 2 tics because a 50ms interval is used to detect the press and another 50ms interval is used to wait for the release. With the default maze the maximum number of presses required before the next shift is three. The simple way to increase the game difficulty is to shorten the time between shifts so a couple of lines of code do just that as the score increases. The shift rate is set to speed up by 50ms every 20 shifts, with the minimum rate limited to 500ms. It’s easy to change these parameters.
Other than altering the shift rate, the primary logic in the software is to move the “ship” and to determine if the “ship” has collided with a “rock”. These functions take advantage of the defined “rock/space” array and also the array that defines the memory locations in the display. The shift count corresponds to the line length of the LCD (0-19) and is used as an index into these arrays. The logic is somewhat complicated by the fact that the lines swap every 20 shift counts. Similar logic is used to determine the position of the “ship” which can be on any of the four lines.
The score for each play is simply the count of the number of shifts that occurred and the high score is saved in the microcontroller internal EEROM. The EEPROM library is used to do the reads and writes to this memory. The routines available allow single byte reads/writes and reads/writes of floating point values. A value of 0xA5 is stored in the first EEROM location to indicate that a high score has been saved. If that value is present on power up, then the floating point value for the high score is read and displayed. If the 0xA5 value is not present, then a routine is called to initialize the high score to a value of 1. That same routine is called if a reset of the high score is desired. The high score is set back to a value of 1 by holding down one of the up/down buttons and then momentarily pressing the reset button.
Attachments
Step 3: Playing the Game
When power is applied, the current high score is displayed. After the high score is displayed, the maze of “rocks” and the “ship” are displayed then the game begins a few seconds later. When the “ship” hits a “rock” the message “CRASH AND BURN” flashes a few times before displaying the score for the game. If a new high score is made, then that message is also displayed. A new game is started by pressing the reset button.