🎲 Retro Game: Guess The Number (with Levels, Score, and Custom Icons)


πŸ”§ Components Needed

  • Arduino Board (e.g., Uno, R4)
  • I2C LCD 1602 Display (I2C address usually 0x27)
  • Push Button (for resetting/advancing the game)
  • Jumper Wires & Breadboard
  • Computer (for Serial Monitor input)

πŸ“Œ Wiring Connections

1. I2C LCD 1602 Display

  • VCC: β†’ Arduino 5V
  • GND: β†’ Arduino GND
  • SDA: β†’ Arduino A4 (Uno) or corresponding SDA pin
  • SCL: β†’ Arduino A5 (Uno) or corresponding SCL pin

2. Reset/Advance Button

  • One Terminal: β†’ Arduino Digital Pin 4
  • Other Terminal: β†’ GND
  • Note: Use the internal pull-up resistor by setting the pin mode to INPUT_PULLUP in the code.

πŸ’» Code Example

Below is the complete sketch that implements the game with the extra features. In this code:

  • The secret number is generated within a range that increases with each level (Level 1: 1–100, Level 2: 1–200, etc.).
  • Score keeping tracks the number of guesses.
  • A custom smiley face (created as a custom LCD character) is displayed when the player wins.
  • Pressing the reset button restarts the game immediately.
#include <LiquidCrystal_I2C.h>

// ----- LCD Settings -----
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 columns x 2 rows

// ----- Game Variables -----
int secretNumber;
bool gameOver = false;
int guessCount = 0;
int level = 1; // Starting level

// ----- Reset/Advance Button Pin -----
const int resetButtonPin = 4;

// ----- Custom Character: Smiley Face -----
byte smiley[8] = {
  B00000,
  B01010,
  B01010,
  B00000,
  B10001,
  B01110,
  B00000,
  B00000
};

void setup() {
  Serial.begin(9600);
  lcd.init();
  lcd.backlight();
  
  // Set up reset button with internal pull-up resistor
  pinMode(resetButtonPin, INPUT_PULLUP);
  
  // Create custom smiley face character at slot 0
  lcd.createChar(0, smiley);
  
  // Seed the random generator using an unconnected analog pin
  randomSeed(analogRead(0));
  
  // Start the first game
  startNewGame();
}

void loop() {
  // ----- Check Reset/Advance Button -----
  if (digitalRead(resetButtonPin) == LOW) {
    // Wait until the button is released to avoid multiple triggers
    while (digitalRead(resetButtonPin) == LOW) { delay(10); }
    startNewGame();
    delay(500); // Debounce delay
  }
  
  // ----- Process Serial Input for Guesses -----
  if (Serial.available() > 0 && !gameOver) {
    int guess = Serial.parseInt();
    if (guess == 0) return; // Skip invalid input
    
    guessCount++; // Increase guess count
    
    lcd.clear();
    
    if (guess < secretNumber) {
      lcd.setCursor(0, 0);
      lcd.print("Too Low!");
      Serial.println("Too low! Try again:");
    } 
    else if (guess > secretNumber) {
      lcd.setCursor(0, 0);
      lcd.print("Too High!");
      Serial.println("Too high! Try again:");
    } 
    else {
      // Correct guess!
      lcd.setCursor(0, 0);
      lcd.print("Correct!");
      lcd.setCursor(0, 1);
      lcd.print("Guesses: ");
      lcd.print(guessCount);
      lcd.write(byte(0)); // Display custom smiley
      Serial.println("Correct! You win!");
      gameOver = true;
      
      // Increase level for the next game
      level++;
      
      // Wait a moment before starting a new game
      delay(3000);
      startNewGame();
    }
    
    delay(1500); // Short delay for user to see feedback
    
    // If the game isn't over, re-prompt on the LCD
    if (!gameOver) {
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Level ");
      lcd.print(level);
      lcd.print(" Guess?");
      Serial.println("Enter your next guess:");
    }
  }
}

void startNewGame() {
  gameOver = false;
  guessCount = 0;
  // Generate a new secret number between 1 and (level * 100)
  secretNumber = random(1, level * 100 + 1);
  
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Level ");
  lcd.print(level);
  lcd.setCursor(0, 1);
  lcd.print("1-");
  lcd.print(level * 100);
  
  Serial.print("New game started! Level ");
  Serial.print(level);
  Serial.print(": Guess a number between 1 and ");
  Serial.println(level * 100);
}

πŸ“‹ How It Works

  • Game Initialization:
    The startNewGame() function resets the game state: it generates a new secret number based on the current level, resets the guess counter, and displays the level and number range on the LCD.
  • Score Keeping:
    Every valid guess increases the guessCount. When you guess correctly, the number of guesses is shown on the LCD along with a smiley face icon.
  • Multiple Levels:
    Upon a correct guess, the game automatically advances to the next level by increasing the maximum possible secret number range (Level 1: 1–100, Level 2: 1–200, etc.).
  • Reset Button:
    Pressing the push button (connected to pin 4) will immediately reset and start a new game, letting you restart at any time.
  • User Input:
    The Serial Monitor is used to enter your guesses, and feedback (“Too Low!” or “Too High!”) is displayed on both the LCD and the Serial Monitor.
  • Custom Characters:
    A custom smiley face character is defined and displayed when you win.

🎯 Conclusion

This enhanced retro game project now includes:

  • Reset Button: Restart the game anytime.
  • Score Keeping: Track how many guesses you make.
  • Multiple Levels: Increase the difficulty as you win.
  • Custom Icons: Enjoy a fun smiley face when you win!

Feel free to adjust or expand the project further, such as adding more game levels or integrating additional features. Enjoy coding your retro game! πŸ˜ŠπŸ‘

πŸ“‘Broadcast the signal β€” amplify the connection.

Leave a Reply