#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // Alamat I2C LCD
const int buttonPin = 2; // Pin tombol
int starPosition = 0; // Posisi bintang
bool movingRight = true; // Arah gerakan bintang
bool gameRunning = true; // Status game
void setup() {
lcd.init();
lcd.backlight();
pinMode(buttonPin, INPUT_PULLUP);
lcd.setCursor(0, 0);
lcd.print("Catch the Star!");
delay(2000);
lcd.clear();
}
void loop() {
if (gameRunning) {
// Bersihkan baris kedua
lcd.setCursor(0, 1);
lcd.print(" ");
// Tampilkan bintang di posisi saat ini
lcd.setCursor(starPosition, 1);
lcd.print("*");
// Cek jika tombol ditekan
if (digitalRead(buttonPin) == LOW) {
if (starPosition == 7) { // Posisi menang (di tengah layar)
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("You Win!");
gameRunning = false; // Akhiri permainan
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Missed!");
delay(1000);
}
}
// Gerakkan bintang
if (movingRight) {
starPosition++;
if (starPosition >= 15) movingRight = false; // Ubah arah ke kiri
} else {
starPosition--;
if (starPosition <= 0) movingRight = true; // Ubah arah ke kanan
}
delay(200); // Kecepatan gerakan bintang
}
}