'Number Invaders' With Virtual 7 Segment Display

by Trevor Lee in Circuits > Arduino

175 Views, 0 Favorites, 0 Comments

'Number Invaders' With Virtual 7 Segment Display

numinvader.jpg

As a continuation of my previous post -- Button Click Counting with Virtual 7 Segment Display -- this post will show an extension of the setup, and develop a very simple good-old-days "caculator game" -- Number Invaders -- I imagined the name, specifically for this simple implementation :-)

Setup Arduino IDE

add_library.png

In order to be able to compile and run the sketch shown here, you will first need to install the DumbDisplay Arduino library.

Open your Arduino IDE; go to the menu item Tools | Manage Libraries, and type "dumbdisplay" in the search box there.

Connection Diagram

numinvader-connection.png
  • Connect A1 to one pin of the button on the left side; connect GND to the other pin of the button.
  • Connect A4 to one pin of the button on the right side; connect GND to the other pin of the button.
  • Connect A2 to the long leg of the LED on the left side; through a 330Ω resistor, connect the short leg of the LED to GND.
  • Connect A3 to the long lef of the LED on the right side; through a 330Ω resistor, connect the short leg of the LED to GND.
  • Note that the LEDs are optional, since they are in the setup just for "decoration".

How the Buttons and LEDs Connect

Simply, when you press the button on the left side, the LED on the left side will be turned on, until the button press is released.

Similarly, when you press the button on the right side, the LED on the right side will be turned on, until the button press is released.

The LEDs have nothing to do with the game; but the buttons have everthing to do with the game.

How the Buttons and the Game Connect

numinvader.gif
  • On the right side. The red digits are the invaders.
  • In the middle (close to the left side). The blue [on yellow] digit is your laser gun.
  • On the left side. The green digit is how many life you have left.
  • When you click the left button, the laser gun's "variant" changes, from 0 to 9.
  • You have to click the left button, until it matches the "variant" of the front-most invader.
  • Then you click the right button to fire your laser.
  • If when your laster gun fires, the laser "variant" matches the front-most invader, it will be eliminated.
  • If the front-most invader reaches your laser gun, you will loose a life.
  • Unlike cat, you will only have 3 lifes.

The Sketch

Although the setup shown here make use of Arduino Nano, I believe the sketch will work for other types of boards, of cause, with some adaptions.

Here it is.

#include "dumbdisplay.h"
#define BUTTON_LEFT PIN_A1
#define BUTTON_RIGHT PIN_A4
#define LED_LEFT PIN_A2
#define LED_RIGHT PIN_A3
#include "gameobjs.h" // for game objects
DumbDisplay dumbdisplay(new DDInputOutput(115200));
// game objects, defined in gameobjs.h
LaserGun laserGun;
Invaders invaders;
Controller controller(laserGun, invaders);
void setup() {
pinMode(BUTTON_LEFT, INPUT_PULLUP);
pinMode(BUTTON_RIGHT, INPUT_PULLUP);
pinMode(LED_LEFT, OUTPUT);
pinMode(LED_RIGHT, OUTPUT);
laserGun.life7Seg = dumbdisplay.create7SegmentRowLayer(1);
laserGun.life7Seg->segmentColor("green");
laserGun.life7Seg->border(5, "green", "round");
laserGun.life7Seg->padding(5);
laserGun.laserGun7Seg = dumbdisplay.create7SegmentRowLayer(1);
laserGun.laserGun7Seg->segmentColor("blue");
laserGun.laserGun7Seg->backgroundColor("yellow");
laserGun.laserGun7Seg->border(5, "blue", "round");
laserGun.laserGun7Seg->padding(5);
invaders.invader7Seg = dumbdisplay.create7SegmentRowLayer(MAX_INVADER_COUNT);
invaders.invader7Seg->segmentColor("red");
invaders.invader7Seg->border(5, "red", "round");
invaders.invader7Seg->padding(5);
// 'pin' the layers (7 segment displays) one by one horizontally
// note that tomtommap is not visible initially
dumbdisplay.configAutoPin(DD_AP_HORI);
controller.initialize();
}
void loop() {
controller.loop();
}

This simple part should show how to setup the buttons as well as the LEDs, and also how the virtual 7 segment displays are layout with DumbDisplay.

It looks simple enough. However, in fact, the whole software logic part is quite lengthy, and not as easy. The main software logic is buried in the INCLUDE file "gamesobjs.h".

  • gamesobjs.h -- it defines LaserGun, Invaders, and Controller classes for controller the game logic
  • tracker.h -- it defines ButtonPressTracker class for tracking when buttun is clicked
  • sound.h -- it define some functions for making simple sound effects with your phone

Note: Arduino IDE requires that the sketch (numinvader.ino), as well as all other INCLUDE files (gamesobjs.h, tracker.h and sound.h) be in a folder with the same name as the sketch (numinvader).

Upload the Sketch and Run It

Other than making the needed connections to your Arduino Nano, you will need to prepare two addition things:

  • You will need to be able to attach your Arduino Nano to your Android phone. To do this, you will need an OTG adaptor, which allows you to plug your Arduino Nano to your phone via the usual USB cable.
  • You will certainly need to install the DumbDisplay Android app.

After uploading the sketch, re-plug the USB cable to your phone via an OTG adaptor. Then, open the DumbDisplay Android app on your phone, and make connection.

Enjoy!

There will be a follow up to this post as well. Next post I will be showing how to config and attched HC-06 module to this setup, and try to make it connects to Arduino DumbDisplay app wirelessly, via Bluetooth. Until then, enjoy!

Peace be with you. Jesus loves you. May God bless you!