Arduino Game: Escape Lane

Hi Friends, this is a Game, Escape Lane, made with Arduino. You can name it as per your wish😊


In this game an obstacle comes from the right-hand side towards the character, character has to avoid the obstacle to remain in the game.

In this game I tried to give a human like shape to the character, you can try something else as per your wish

As the game advances, score increases accordingly.

When the obstacle hits the character, game is over, and it asks to start the game by pressing Pin-12


Material Required

1. Arduino UNO: 1 No.

2. 16x2 LCD Shield: 1 No.

3. Toggle Switches or Push Button: 2 Nos.

4. Some Wires

5. USB Cable to Program


Game Working

1. LCD shield is plugged in to the Arduino UNO

2. Input-12 is used to start the game

3. As game is started, obstacle starts coming from the right-hand side toward the character

4. Input-2 is used to control our character, up and down

5. There are two rows, with the help of this switch we can control the position of the character

6. When switch is OFF then character will remain in top row, when switch is ON then it will jump to the bottom row to avoid the obstacle coming towards it

7. On the right-hand side, there is the score counter


Code

/*
 * Project: Escape Lane Game in Arduino
 * Author: Deepak Sharma
 * Date: 11 Aug 2024
 * Description: This program uses an Arduino Uno with an LCD shield to make a simple game, obstacle avoidance; name: Escape Lane.
 * Version: 1.0
 */


#include <LiquidCrystal.h>

// Initialize the LCD with the pins connected to the LCD shield
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

// Define the button pins
const int rowControlPin = 2; // Pin to control human row (INPUT_PULLUP)
const int startButtonPin = 12; // Pin to start and restart the game

// Custom characters for the human figure
byte head[8] = {
  B01110,
  B10001,
  B11011,
  B10001,
  B01110,
  B11011,
  B01010,
  B01010
};

// Game variables
int humanRow = 1; // Initial row of the human figure (0 or 1)
int gameStarted = 0; // Game start state (0 = not started, 1 = started)
unsigned long previousMillis = 0;
unsigned long scoreMillis = 0;
const long interval =100; // Speed of the game (ms)
const long scoreInterval = 500; // Scoring interval (ms)
int obstaclePosition = 12; // Initial position of the obstacle
int obstacleRow = 1; // Row of the obstacle (0 or 1)
int score = 0;

void setup() {
  // Set up the LCD and buttons
  lcd.begin(16, 2);
  pinMode(rowControlPin, INPUT_PULLUP);
  pinMode(startButtonPin, INPUT_PULLUP);

  // Create custom characters
  lcd.createChar(0, head);
  
  
  lcd.setCursor(0, 0);
  lcd.print("   Welcome To  ");
  lcd.setCursor(0, 1);
  lcd.print("ElectroMech Lab");
  delay(2000);
  lcd.clear();


  lcd.setCursor(0, 0);
  lcd.print("Welcome To Game");
  lcd.setCursor(0, 1);
  lcd.print("  Escape Lane  ");
  delay(2000);
  lcd.clear();


  
  lcd.setCursor(0, 0);
  lcd.print("Press Pin 12 to");
  lcd.setCursor(0, 1);
  lcd.print("   Start Game");
}

void loop() {
  //  if the start button is pressed
  if (digitalRead(startButtonPin) == LOW) {
    if (gameStarted == 0) {
      startGame();
    }
  }
  
  // If the game has started, proceed with game logic
  if (gameStarted == 1) {
    unsigned long currentMillis = millis();
    
    // Update the game state
    if (currentMillis - previousMillis >= interval) {
      previousMillis = currentMillis;
      
      // Move the obstacle
      obstaclePosition--;
      if (obstaclePosition < 0) {
        obstaclePosition = 12; // Reset the obstacle position
        obstacleRow = random(2); // Randomly change obstacle row
        score += 1; // Increase score when obstacle passes
      }
      
      // Display the game state
      displayGame();
      
      //  for collision
      if (obstaclePosition == 0 && humanRow == obstacleRow) {
        gameOver();
      }
    }
    
    // Update the score
    if (currentMillis - scoreMillis >= scoreInterval) {
      scoreMillis = currentMillis;
      score++;
    }
  }
}

void displayGame() {
  lcd.clear();
  RowControl();
  
  // Display the Human figure
  lcd.setCursor(0, humanRow);
  lcd.write(byte(0)); // Head
  
  // Display the obstacle
  lcd.setCursor(obstaclePosition, obstacleRow);
  lcd.print("<");
  
  // Display the score
  lcd.setCursor(11, 0);
  lcd.print("Score:");
  lcd.setCursor(13, 1);
  lcd.print(score);
}

void gameOver() {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("   Game Over  ");
  lcd.setCursor(0, 1);
  lcd.print("Score: ");
  lcd.print(score);
  delay(3000);
  resetGame();
}

void resetGame() {
  // Reset game variables
  obstaclePosition = 12;
  obstacleRow = 1;
  humanRow = 1;
  score = 0;
  gameStarted = 0; // Game is not started
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Press Pin 12 to");
  lcd.setCursor(0, 1);
  lcd.print("   Start Game");
}

void startGame() {
  // Initialize game variables
  gameStarted = 1; // Set game to started
  randomSeed(analogRead(A0)); // Initialize random seed
  lcd.clear();
}

void RowControl() {
  // Control the row of the human figure based on pin 2
  if (digitalRead(rowControlPin) == LOW) {
    humanRow = 1; // Row 0
  } else {
    humanRow = 0; // Row 1
  }
}