As part of my Computer Hardware and Operating Systems module, we were given the task “build something interesting with the Arduino kit”. I wanted to do something that reflected my own course, so I decided to have a crack at making a really basic space invaders clone. This was a 10 hour project.
- Arduino Mega 2560, Sainsmart model.
- 128×64 Adafruit SSD1306 OLED screen.
- Basic rotary encoder with left/right turn and button.
- TM1638 LED display module with 7 segment display & LEDs.
- Male-to-male & male-to-female jumper pins.
- Arduino IDE.
- Visual Studio & SFML for writing the Space Invaders library.
- Adafruit SSD1306 drivers.
- TM1638 LED display module drivers.
- My own platform agnostic space invaders library, C++.
- Rotary encoder reader lib.
Decoupling and writing the Simple Invaders lib
As the Arduino doesn’t have a set of debugging/testing tools, so I decided the best choice would be to create the game in a game framework first (SFML) while keeping the Simple Invaders Lib completely decoupled from the SFML and Arduino platform. This would allow me to test features and bugs inside of the visual studio environment (thank god for breakpoints), making development much easier than testing directly on the Arduino.
To keep the library decoupled, we defined three abstract objects that would be inherited from to allow the game to run on either chosen platform. These objects are: “GraphicsDevice”, “InputReader” and “GameInterface”. These sit as global objects inside of the “Platform.h” file.
The graphics device has basic virtual methods for drawing text and rectangles. On the Arduino, this just called the necessary methods for interfacing with the Adafruit OLED screen.
The input reader has basic virtual methods that define if the player wants to move left/right or has fired. On the Arduino, this just checked the encoder to see if the player has twisted left/right or pressed in the rotary button.
The game interface has basic virtual methods that display the remaining count of the invaders and the players lives. On the Arduino, this just set the LEDs and the 7 segment displays values appropiately.
You can see me inheriting these objects and setting their global instances inside of the ‘invadersSketch.ino’ file in the simple invaders git.
The game is missing a lot of the original space invaders features, but time (and the fact that I’d get no marks for implementing them) didn’t permit me to make a complete clone.
All in all, a really fun project. The arduino is fun to work with – and working with only 8K SRAM is an interesting experience that teaches you a lot. Including respect for the people who originally made this game on a 2MHz CPU.
Photos of the final product