Introduction: Color Invaders


This is a fun little project that I was recently inspired to try out by Hamster's own Colour Invaders project.

As the name suggests, it is similar in design to the classic Space Invaders game or (more similarly) the Casio's Number Invaders on the calculator. The idea behind the game is that different colored invaders start marching down the LED strip and you have to fire missiles that match the color of the oncoming invader.

Naturally, as you successfully destroy more of the invaders, the faster they come towards your base. Luckily, we do get a nice bonus if we successfully destroy enough invaders in a row... :)

Can you survive the onslaught against the color invaders?

Step 1: What I Used

Realistically, a number of these components can be swapped out with other parts, but these are what I had on hand that worked perfectly for what I needed.

  • strip of 30 WS2812 LEDs - the weatherproof sheathing also works nicely as a way to diffuse each of these individually addressable LEDs
  • Digilent's chipKIT uC32 - this PIC32 microcontroller has more than enough capability to run this strip of 30 LEDs... or easily run 900 of them. It also had a library for the WS2812 LEDs which was a plus.
  • a PmodENC - this is a rotary encoder to allow users to cycle between colors for their missile and also doubles as a pushbutton so that they can fire their missile.
  • breadboard jumper wires - so I can connect everything together

I'll go through how I wired it all up next.

Step 2: Wiring It Up

For this particular circuit, there isn't a whole lot that we need to wire up.

The WS2812 strips need to have the red wire connected to a 5V line, the white wire to a ground (0V) line, and the green wire connected to whichever pin you are sending data out on; in my case this is digital pin 3. In terms of power, as long as you are powering the microcontroller/WS2812 strip from a standard USB 2.0 port, you will have enough current to power the entire strip for this particular game. (see the first picture)

The PmodENC needs to be connected to three data pins (for the two internal buttons that the encoder uses and the pushbutton that is present on the encoder itself) and a 3.3V power supply so that appropriate logic levels for the PIC32 microcontroller can be achieved. I attached the button pin (labeled "BTN" on the PmodENC) to digital pin 30 on the uC32, and pins "A" and "B" to digital pins 12 and 13, respectively. (see the second and third pictures)

Pins 12 and 13 where chosen because these are two "change notice" pins in the PIC32 hardware that are placed in close proximity to each other and are externally accessible. A change notice pin is one that provides an interrupt when its current state (high or low) is changed.

Step 3: Game Variables

The code I created for this game is available in both an MPIDE and text file format at the bottom of this step.

To make the game work, I created several functions that checked for some potential conditions (in order as listed)

  1. If there is a missile on the strip, but it is not in flight, update it's current color via the current encoder value.
  2. If there is not a missile on the strip (i.e. it has exploded/fizzled out), read the new color value via the encoder, but do not actually update anything on the LED strip.
  3. If no missile exists, generate a new missile and start it's charging time.
  4. If the fire button is pressed, the missile is charged, and the missile is not currently in flight, fire the missile.
  5. If the missile is in flight and it's time for it to move along the strip, do so.
  6. If the missile is in flight and its current location matches the location of the foremost invader, indicate contact has been made. If contact has been made, check to see if the missile color and the invader color match. If so, explode the invader; if not, have the missile vanish and the invaders continue their assault.
  7. If it is time to move the invaders down the strip, do so. A new invader of a random color is also placed at the end of strip.
  8. After we have moved the invaders, check for contact again to see if the invaders moved into a missile that is currently in flight.
  9. If the invaders (or at least the one at the front of the line) have made it all the way down to the very first LED on the strip, you lose the game.
  10. Refresh the LED strip with the new colors, missiles, and invader locations before looping through this list again.

Various global variables and #define's were also used to help keep track of consistent variables such as the current color of the missile in flight, what colors the invaders are, as well as timing delays for missile and invader movement.

The mentioned "random" color is random in the fact that it chooses from one of the six predefined colors in based off how recently the player destroyed an invader compared to the internal system clock.

Step 4: Parting Thoughts

Naturally, this program can be adapted to change some of the settings, such as not having a missile recharge time, allowing for multiple missiles on the screen, a missile continuing to go past the invader stream until it finds one of a matching color, or even a different game entirely.

Feel free to post any questions or comments that you may have!