#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define BUTTON_PIN 2
LiquidCrystal_I2C lcd(0x27, 16, 2); // Alamat I2C LCD 16x2
int dinoY = 1; // Posisi Y Dino (hanya 2 baris LCD)
int groundLevel = 1;
int obstacleX = 15;
bool isJumping = false;
int jumpHeight = 1;
int jumpStep = 0;
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP); // Tombol untuk lompat
lcd.begin(16, 2); // Inisialisasi LCD 16x2
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Dino Game");
delay(1000);
}
void loop() {
handleInput();
updateGame();
renderGame();
delay(500); // Mengatur kecepatan game
}
void handleInput() {
if (digitalRead(BUTTON_PIN) == LOW && !isJumping) {
isJumping = true;
jumpStep = 0;
}
}
void updateGame() {
// Update posisi dino
if (isJumping) {
jumpStep++;
dinoY = groundLevel - jumpHeight + abs(jumpStep - 5);
if (jumpStep >= 10) {
isJumping = false;
dinoY = groundLevel;
}
}
// Update posisi obstacle
obstacleX--;
if (obstacleX < 0) {
obstacleX = 15;
}
// Cek tabrakan
if (obstacleX == 0 && dinoY == 1) {
gameOver();
}
}
void renderGame() {
lcd.clear();
lcd.setCursor(obstacleX, 0);
lcd.print("O"); // Rintangan
if (dinoY == 1) {
lcd.setCursor(0, 1);
lcd.print("D"); // Dino
}
// Tampilkan teks skor atau pesan lainnya (misalnya "GAME OVER")
}
void gameOver() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("GAME OVER");
while (true); // Loop tanpa henti setelah game over
}