Skip to main content

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
  }
}

Comments

Popular posts from this blog

How to Make Automatic Room Light Controller Without Microcontroller

You must have noticed in some offices or hotels, when nobody is in gallery or washroom, the light remains OFF but when somebody enters the place, light switches ON automatically. In this post I am going to teach you how to make this circuit. Before going ahead I would like to tell you that this is VERY EASY circuit. For this circuit the material we need is… PIR Motion sensor General Purpose PCB - 5x5 cm. Transistor 2222N – 1 No. Relay 5V – 1 No. 1K/0.250W – 2 Nos. 10K/0.250W – 1 No. IN4007 – 2 Nos. LED 3mm – 1 No. Connector – 4 Nos. Few wires. Relay Circuit Concept : We can use any relay of 12V, 24V, 5V etc. but we have to consider power supply or battery we will use. Since 5V power supply is easily available and 9V battery can also be used for 5V output (after using 7805 regulator if needed). So I am using 5V relay. PIR sensor has three terminals, One for 5Vdc Second for Gnd (0V). Third for

How to drive high voltage/current load by small voltage signal from a microcontroller?

Sometimes we need to control or drive a high voltage and heavy current load by a small voltage signal. For example, if you want to control water motor with your microcontroller output. We know that microcontroller gives only 5v output which is not sufficient to drive a heavy motor. This circuit, about which this post is, is very-very useful for electronics engineer and hobbyist. So pay attention! For this circuit the material we need is… General Purpose PCB - 5x5 cm. Transistor KN 2222A (TO-92) - 1 No. Relay 5V – 1 No. 1K/0.250W – 2 Nos. 10K/0.250W – 1 No. IN4007 – 2 Nos. LED 3mm – 1 No. Connector – 4 Nos. Few wires. Tools. Concept: Weak signal triggers the transistor and transistor acts as a switch for the relay. You can use any relay of 12V, 24V, 5V etc. but we have to consider power supply or battery we will use. Since 5V power supply is easily available and 9V battery can also be used for 5V output (after using 7805 regulator if needed).

How to Read Analog Input & Use PWM pin as Analog Output

  Analog Input: An analog signal can take on any number of values. To measure the value of analog signals, Arduino has a built-in analog-to-digital converter (ADC). The ADC turns the analog voltage into a digital value. There is an inbuilt function to read Analog value; analogRead(pin_number). This function converts the value of the voltage on the analog input pin and returns a digital value ranges from 0 to 1023, relative to the reference value. The default reference voltage is 5 V (for 5 V Arduino boards) or 3.3 V (for 3.3 V Arduino boards). This function has only one parameter, which is the pin number.     Analog Output: The Arduino does not have any built-in digital-to-analog converter (DAC), but it can do pulse-width modulation (PWM); a digital signal to achieve some of the functions of an analog output. The function analogWrite(pin, value) is used to output a PWM signal. In the function ‘pin’ is the pin number used for the PWM output. ‘value’ is a number proportiona