Adding Obstacles to the Arduino LCD Game

Beginning: 🕹️ Simple Arduino LCD Game: “Jump & Move”

Updated Arduino Sketch

We’ll introduce random obstacles that appear on the right side of the LCD and move toward the left. The player must jump over them to avoid a collision.

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <stdlib.h> // Include the standard library for random number functions

LiquidCrystal_I2C lcd(0x27, 16, 2);

const int buttonLeftPin = 2;
const int buttonJumpPin = 3;
const int buttonRightPin = 4;
int fly = 0;
int positionX = 7; // Start in the middle of the screen
int obstaclePosition = 15; // Start obstacle off-screen
bool inAir = false; // Is the character in the air?

void setup() {
  pinMode(buttonLeftPin, INPUT_PULLUP);
  pinMode(buttonJumpPin, INPUT_PULLUP);
  pinMode(buttonRightPin, INPUT_PULLUP);
  lcd.init();
  lcd.backlight();
  lcd.setCursor(positionX, 1);
  lcd.print("*"); // Character representation
  randomSeed(analogRead(0)); // Initialize random seed
}

void loop() {
  if (digitalRead(buttonLeftPin) == LOW) {
    moveLeft();
  }
  if (digitalRead(buttonRightPin) == LOW) {
    moveRight();
  }
  if (digitalRead(buttonJumpPin) == LOW && !inAir) {
   
    jump();
  }
  if (millis() % 20 == 0) { // Every 20 milliseconds, update the obstacle
    moveObstacle();

  }
  checkCollision();
}

void moveLeft() {
  if (positionX > 0) {
    lcd.clear();
    lcd.setCursor(--positionX, 1);
    lcd.print("*");
    delay(200);
  }
}

void moveRight() {
  if (positionX < 15) {
    lcd.clear();
    lcd.setCursor(++positionX, 1);
    lcd.print("*");
    delay(200);
    
  }
}

void jump() {
  
  lcd.clear();
  inAir = true;
  lcd.setCursor(positionX, 0);
  lcd.print("*");
  delay(200);
  fly = obstaclePosition - 3;
  
    
}

void moveObstacle() {
  delay(200);
  lcd.setCursor(obstaclePosition, 1);
  lcd.print(" "); // Clear the old position
  
  if (obstaclePosition > 0) {
    lcd.setCursor(--obstaclePosition, 1);
    lcd.print("#"); // Display the new position
    if (fly == obstaclePosition){

  lcd.clear();
  lcd.setCursor(positionX, 1);
  lcd.print("*");
  inAir = false; // Return to ground
      
    }
  
  } else {
    obstaclePosition = 15; // Reset the obstacle to the start
  }
}

void checkCollision() {
  if (positionX == obstaclePosition && !inAir) {
    // Handle collision - game over or reduce life
lcd.clear();
    lcd.setCursor(positionX, 1);
    lcd.print("0");
    delay(1000);
    lcd.clear();
    lcd.setCursor(positionX - 1, 1);
     lcd.print(">0<");
    delay(1000);
    lcd.clear();
    lcd.print("Game Over!");
    delay(2000);
lcd.clear();
    lcd.print("Restarting...");
    delay(2000);
lcd.clear();
    positionX = 7; // Reset position
    obstaclePosition = 15; // Reset obstacle
    lcd.setCursor(positionX, 1);
    lcd.print("*");
  }
}

Game Mechanics

  • Obstacles: Obstacles (#) appear randomly and move from the right to the left of the screen.
  • Jumping: The player must jump over obstacles to avoid collisions.
  • Collision Detection: If the player is not in the air and collides with an obstacle, the game displays “Game Over!” and then restarts.

This update introduces basic obstacle mechanics and increases the complexity of the game. You can further expand this by adding multiple obstacle types, varying speeds, or even scores based on how many obstacles are successfully avoided.

📡Broadcast the signal — amplify the connection.

Leave a Reply