Mirko Pavleski
Published © GPL3+

DIY Arduino Laser Pointer Shooting Game

The object of the game is to shoot the LDR (target) under the active LED with a light beam from a laser pointer.

BeginnerFull instructions provided6,617
DIY Arduino Laser Pointer Shooting Game

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
RGB Backlight LCD - 16x2
Adafruit RGB Backlight LCD - 16x2
×1
LDR, 1 Mohm
LDR, 1 Mohm
×5
LED (generic)
LED (generic)
×5
Buzzer
Buzzer
×1
Resistor 1k ohm
Resistor 1k ohm
×5
Through Hole Resistor, 820 ohm
Through Hole Resistor, 820 ohm
×5
Pushbutton Switch, Momentary
Pushbutton Switch, Momentary
×1

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
Solder Wire, Lead Free
Solder Wire, Lead Free

Story

Read more

Schematics

Schematic

Code

Arduino Code

C/C++
/**
 * How to play:
 * Goal of the game is to shoot as many alien spaceships as possible.
 * At game start you have 5 shots. One of 5 red LEDs ("spaceship") lit randomly for a short
 * time and you have to shoot this spaceship as fast as possible. If you do not hit the
 * spaceship fast enough you loose and the game is over. After you hit the spaceship the
 * LED will go off. If you run out of shots the game is over, but for every successfully
 * hit spaceship you gain one more shot.
 */
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

#define PIN_PIEZO 6

#define AMOUNT_SPACESHIPS 5
#define AMOUNT_SHOTS_INIT 5

#define MAX_SPACESHIP_DELAY  2000 // in ms
#define MIN_SPACESHIP_DELAY   250 // in ms
#define SPACESHIP_DELAY_STEP  100 // in ms

#define TONE_SHOT  1
#define TONE_END_1 2
#define TONE_END_2 3

LiquidCrystal_I2C lcd(0x27,16,2);

bool gameRunning;
byte pinLEDs[AMOUNT_SPACESHIPS] = {9, 10, 11, 12, 13};
byte pinLDRs[AMOUNT_SPACESHIPS] = {2,  3,  4,  5, 7};
int activeSpaceship;
byte shots;
unsigned int spaceshipDelay;
unsigned int score;
unsigned long shipAppearance;

void setup()
{
  Serial.begin(9600);

  lcd.begin();
  lcd.setCursor(1,0);
  lcd.print(" Laser Shooter");
  lcd.setCursor(1,1);
  lcd.print("    mircemk");
  
  randomSeed(analogRead(0));
  pinMode(PIN_PIEZO, OUTPUT);
  for (byte i = 0; i < AMOUNT_SPACESHIPS; i++) {
    pinMode(pinLEDs[i], OUTPUT);
    pinMode(pinLDRs[i], INPUT);
    digitalWrite(pinLEDs[i], LOW);
  }

  spaceshipDelay = MAX_SPACESHIP_DELAY;
  gameRunning = true;
  activeSpaceship = -1;
  score = 0;
  shots = AMOUNT_SHOTS_INIT;
  initAnimation();
}

void loop()
{
  if (gameRunning) {
    updateSpaceships();
    checkShoot();
  }
}

void updateSpaceships()
{
  if (activeSpaceship > -1) {
    if ((millis() - shipAppearance) > spaceshipDelay) {
      endGame();
    }
    return;
  }

  // delay between two spaceships
  delay(random(500, 3000));

  activeSpaceship = random(0, AMOUNT_SPACESHIPS);
  digitalWrite(pinLEDs[activeSpaceship], HIGH);
  shipAppearance = millis();
  if (spaceshipDelay > (MIN_SPACESHIP_DELAY + SPACESHIP_DELAY_STEP)) {
    spaceshipDelay -= SPACESHIP_DELAY_STEP;
  }
}

void checkShoot()
{
  if (activeSpaceship == -1) {
    return;
  }
  for (byte i = 0; i < AMOUNT_SPACESHIPS; i++) {
    if (digitalRead(pinLDRs[i]) == HIGH) {
      continue;
    }

    shots--;
    if (activeSpaceship == i) {
      // player hit spaceship
      digitalWrite(pinLEDs[activeSpaceship], LOW);
      activeSpaceship = -1;
      score++;
      shots++;
      tone(PIN_PIEZO, 400, 50);
      return;
    }
    if (shots <= 0) {
      endGame();
      return;
    }
  }
}

void initAnimation()
{
  Serial.println("Incoming alien ships...");
  lcd.setCursor(1,0);
  lcd.print("Incoming ships  ");
  lcd.setCursor(1,1);
  lcd.print("  * * * * * *   ");
  for (byte i = 0; i < 9; i++) {
    for (byte j = 0; j < AMOUNT_SPACESHIPS; j++) {
      digitalWrite(pinLEDs[j], i%2);
    }
    delay(250);
     tone(PIN_PIEZO, 1220 - (i%2 * 50));
    delay(300);
    noTone(PIN_PIEZO);
  }
}

void endGame()
{
  gameRunning = false;
  Serial.println("GAME OVER");
    lcd.setCursor(1,0);
  lcd.print("  GAME OVER    ");
  lcd.setCursor(1,1);
  lcd.print("Final score:");
  lcd.print(score);
  Serial.println("Final score: " + String(score));
  
  for (byte i = 0; i < 6; i++) {
    for (byte j = 0; j < AMOUNT_SPACESHIPS; j++) {
      digitalWrite(pinLEDs[j], i%2);
    }
    tone(PIN_PIEZO, 660 - (i%2 * 50));
    delay(300);
    noTone(PIN_PIEZO);
  }
  for (byte i = 0; i < AMOUNT_SPACESHIPS; i++) {
    digitalWrite(pinLEDs[i], LOW);
  }
}

Credits

Mirko Pavleski
172 projects • 1388 followers
Contact

Comments

Please log in or sign up to comment.