#include #include LiquidCrystal_I2C lcd(0x20, 16, 2); // change to 0x7C if needed const int buttonPin = D7; bool lastButtonState = HIGH; unsigned long lastDebounceTime = 0; const unsigned long debounceDelay = 30; int playerRow = 1; // 1 = ground, 0 = jump bool isJumping = false; unsigned long jumpStartTime = 0; const unsigned long jumpDuration = 500; int obstacleCol = 15; int score = 0; bool gameOver = false; unsigned long lastStepTime = 0; int gameSpeed = 250; // smaller = faster void setup() { pinMode(buttonPin, INPUT_PULLUP); Wire.begin(D4, D5); lcd.init(); lcd.backlight(); showStartScreen(); } void loop() { if (gameOver) { if (buttonPressed()) { resetGame(); } return; } if (buttonPressed() && !isJumping) { isJumping = true; jumpStartTime = millis(); playerRow = 0; } if (isJumping && millis() - jumpStartTime > jumpDuration) { isJumping = false; playerRow = 1; } if (millis() - lastStepTime > gameSpeed) { lastStepTime = millis(); updateGame(); drawGame(); } } bool buttonPressed() { bool reading = digitalRead(buttonPin); if (reading != lastButtonState) { lastDebounceTime = millis(); lastButtonState = reading; } if ((millis() - lastDebounceTime) > debounceDelay) { static bool stableState = HIGH; if (reading != stableState) { stableState = reading; if (stableState == LOW) { return true; } } } return false; } void showStartScreen() { lcd.clear(); lcd.setCursor(0, 0); lcd.print("Mini Jump Game"); lcd.setCursor(0, 1); lcd.print("Press btn start"); while (!buttonPressed()) { delay(10); } resetGame(); } void resetGame() { playerRow = 1; isJumping = false; obstacleCol = 15; score = 0; gameOver = false; gameSpeed = 250; lcd.clear(); drawGame(); } void updateGame() { obstacleCol--; if (obstacleCol < 0) { obstacleCol = 15; score++; if (gameSpeed > 120) { gameSpeed -= 10; // gradually increase difficulty } } if (obstacleCol == 1 && playerRow == 1) { gameOver = true; showGameOver(); } } void drawGame() { lcd.clear(); // player lcd.setCursor(1, playerRow); lcd.print("P"); // obstacle lcd.setCursor(obstacleCol, 1); lcd.print("#"); // score lcd.setCursor(10, 0); lcd.print("S:"); lcd.print(score); } void showGameOver() { lcd.clear(); lcd.setCursor(0, 0); lcd.print("Game Over!"); lcd.setCursor(0, 1); lcd.print("Score:"); lcd.print(score); delay(1500); lcd.clear(); lcd.setCursor(0, 0); lcd.print("Press button"); lcd.setCursor(0, 1); lcd.print("to restart"); }