Pls help in this code =
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Define LCD parameters
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set I2C address, columns, rows
// Game variables
const int buttonPin = 2; // Button connected to digital pin 2
int buttonState = 0;
int lastButtonState = 0;
int dinoY = 0; // Dinosaur's Y-position (0 is ground, higher values are higher jumps)
int jumpHeight = 2; // Maximum jump height
int jumpVelocity = jumpHeight; // Initial jump velocity
int gravity = 1; // Gravity constant
int obstacleX = 16;
int obstacleSpeed = 1;
int score = 0;
bool gameOver = false;
void setup() {
pinMode(buttonPin, INPUT_PULLUP); // Use internal pull-up resistor
// Initialize LCD
lcd.init();
lcd.backlight();
}
void loop() {
// Read button state
buttonState = digitalRead(buttonPin);
// Detect button press
if (buttonState == LOW && lastButtonState == HIGH) {
// Jump
jumpVelocity = jumpHeight;
}
// Update dinosaur position (gravity)
if (dinoY < jumpHeight) {
dinoY += jumpVelocity;
jumpVelocity -= gravity;
} else {
dinoY -= gravity;
}
dinoY = constrain(dinoY, 0, jumpHeight); // Keep dinoY within bounds
// Move obstacle
obstacleX -= obstacleSpeed;
// Check for collision
if (obstacleX <= 0 && dinoY == 0) {
gameOver = true;
}
// Check for game over
if (gameOver) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Game Over!");
lcd.setCursor(0, 1);
lcd.print("Score: ");
lcd.print(score);
delay(2000); // Delay before restarting
resetGame();
}
// Update score
if (obstacleX <= 0) {
obstacleX = 16;
score++;
obstacleSpeed = constrain(obstacleSpeed + 1, 1, 5); // Increase speed gradually
}
// Draw game on LCD
lcd.clear();
lcd.setCursor(0, dinoY); // Use dinoY to control vertical position
lcd.print("D");
lcd.setCursor(obstacleX, 0);
lcd.print("O");
lcd.setCursor(0, 1);
lcd.print("Score: ");
lcd.print(score);
lastButtonState = buttonState;
delay(100);
}
void resetGame() {
dinoY = 0;
obstacleX = 16;
obstacleSpeed = 1;
score = 0;
gameOver = false;
}