A one-dimensional dungeon adventure Game built using an LED strip, an accelerometer/gyro sensor and a Raspberry Pi 3 running Android Things.
Why we built it
Liverpool MakeFest is a family-friendly event celebrating low level technologies, hardware, hacking and making. We participated with a stand at the 2018 event and built the Dungeon Crawler game to engage with those attending and learn more about Redux, Firebase and Android Things for ourselves.
Dungeon Crawler uses an LED strip as a display, where the player, enemies, and other game entities are represented as different LED colours. The player can be controlled with a joystick that’s built using a spring from a doorstop, which includes a gyro & accelerometer sensors to determine the movement speed or if the user is attacking.
We ported all the game logic to Java/Kotlin running on Android Things and once it was working with the hardware, we decided to take it one step further to explore how we could make the experience even more engaging. We considered multiple options, but one that really caught our attention was having a replay feature. After someone plays a game, the game stores the replay as JSON in Firebase in order to replay it later on. Using the Redux architecture enables this feature as we store a list of game states (one for each frame) as the game progresses, and at the end of the game, we store the list of states in a Firebase Database. Additionally, we built a companion app, which can run on a regular Android device. The companion app observes for new game replays in Firebase and when there’s a new one, it pulls it and replays it on the separate tablets screen.
See the full blog post here.
How we built it
First of all we converted the code from the TWANG game to Java and then we modified the game loop so that we could tie it to Android’s Activity Lifecycle, as we wanted to make sure we could get the game logic working and compiling before proceeding to the hardware side of things.
Once we had the game working on an Android device, it was time to tackle the hardware side of this project. We started with the joystick. We built the first version of the joystick by screwing the door stop to a piece of wood and the MPU6050 sensor to the head of the door stop. Then wired it to a breadboard and to the Raspberry Pi. Next, we decided to convert the Arduino code to communicate with the sensor to Android Things. After some struggling with the configuration and with maths, we realised that we were writing to the SPI bus incorrectly, and managed to fix it when we found about this MPU6050 driver for Android Things (we ended up leaving our implementation with the fixes from that repo). Finally, we were able to read data from the sensor and wobble it as a player would do when playing the game!
Next up was the display. In order to power the whole LED strip (5 meters long, so 300 LEDs), we had to use a separate power supply to make sure the LEDs would be bright enough. The product details mention that the LED strip uses 18W/meter, so since we wanted to use the whole LED strip, it would be using 90W in total. The working voltage of the LEDs is 5V, so the maths are as follows:
I = P / V = (18W * 5m) / 5V = 90W / 5V = 18A
So we had to use a power supply that could output 18A at 5V. We decided to get this one, and it worked great! We decided that it’d make sense to use the same power supply for the Raspberry Pi as well, since it also works at 5V, so we cut the end of a USB cable and used it to connect the Pi to the PSU.
Getting the Apa102 LED strip working with Android Things was rather simple, since we already had some experience with working with LED strips in Android Things and there’s already a driver for the Apa102 that works out of the box. No major issues here, we just had to convert the Twang code that was using Arduino’s FastLED library to work with the Apa102 Android Things driver, and everything worked.
At this point we had the joystick and the display working! It was a matter of putting it all together. Xavi’s dad designed a 3D model of a case for this project and 3D-printed it. It came out pretty nicely!
We also 3D-printed the head (we took the same design that is provided in the Twang project):
Inside the head we put the MP6050 sensor, and we used some glue to make sure the head would stay in place attached to the door stop. The wires to the sensor would go through the spring of the door stop and connected to the Pi directly.
At the top of the case, we mounted the door stop and the joystick was complete:
Now, we just had to close the case and connect the LED strip (which comes out of the small hole at the end of the case:
Next, we decided to implement the code to store replays (using Firebase). The idea was that we would store in memory the current game state for every frame of the game and then, when the game is over (or if the player completes the game), it would store in Firebase the whole list of game frames.
Read More Detail :Android Things Dungeon Adventure Game