Adding a Scoring System to the Arduino LCD Game

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

Updated Arduino Sketch

We will increment the score each time the player avoids an obstacle by jumping over it. The score will be displayed on the LCD screen.

#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 score = 0; // Initialize score
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
  displayScore();
}

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("*");
    displayScore();
    delay(200);
  }
}

void moveRight() {
  if (positionX < 15) {
    lcd.clear();
    lcd.setCursor(++positionX, 1);
    lcd.print("*");
    displayScore();
    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
    displayScore();  
    }
  
  } else {
    score++; // Increment score when an obstacle is avoided
    obstaclePosition = 15; // Reset the obstacle to the start
    displayScore();
  }
}

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("*");
    score = 0; // Reset score
  displayScore();
  }
}
void displayScore() {
  lcd.setCursor(0, 0);
  lcd.print(String(score));
}

Game Mechanics

  • Score Tracking: The score increments every time the player avoids an obstacle by allowing it to move past the left side of the screen.
  • Display Score: The score is displayed continuously on the top row of the LCD.
  • Game Over Scenario: When a collision occurs, the game displays the final score and then resets.

Adding a scoring system not only makes the game more challenging but also provides players with a quantitative measure of their skill or progress.

📡Broadcast the signal — amplify the connection.

Leave a Reply