Arduino LCD गेम में स्कोरिंग सिस्टम जोड़ना

शुरुआत: 🕹️ सरल Arduino एलसीडी गेम: "कूदो और आगे बढ़ो"

अद्यतित Arduino स्केच

जब भी खिलाड़ी किसी बाधा से कूदकर बच जाएगा, हम उसका स्कोर बढ़ा देंगे।pinइस पर क्लिक करें। स्कोर एलसीडी स्क्रीन पर प्रदर्शित होगा।

#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));
}

खेल यांत्रिकी

  • स्कोर ट्रैकिंग: जब भी खिलाड़ी स्क्रीन के बाईं ओर से आगे बढ़कर किसी बाधा से बचता है तो स्कोर में वृद्धि होती है।
  • प्रदर्शन स्कोर: स्कोर एलसीडी की शीर्ष पंक्ति पर लगातार प्रदर्शित होता रहता है।
  • खेल खत्म परिदृश्य: जब कोई टक्कर होती है, तो खेल अंतिम स्कोर प्रदर्शित करता है और फिर रीसेट हो जाता है।

स्कोरिंग प्रणाली जोड़ने से न केवल खेल अधिक चुनौतीपूर्ण हो जाता है, बल्कि खिलाड़ियों को उनके कौशल या प्रगति का मात्रात्मक माप भी मिलता है।

📡 सिग्नल प्रसारित करें - कनेक्शन को बढ़ाएँ।

एक जवाब लिखें