#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
//Verbindung I2C
//GND to GND
//VCC to 5V
//SDA to A4
//SCL to A5
#define BUTTON_PIN 2
// BITMAPS
byte dino[8] = {
0b00000,0b00111,0b00111,0b10110,0b11111,0b01010,0b01010,0b00000
};
byte cacti[8] = {
0b00100,0b00101,0b10101,0b10101,0b10111,0b11100,0b00100,0b00000
};
bool gameRunning = false;
int dinoY = 1;
bool jumping = false;
unsigned long jumpStart = 0;
int obstacleX = 15;
int obstacleY = 1;
int prevObstacleX = 15;
int prevObstacleY = 1;
unsigned long lastMove = 0;
int speedDelay = 300;
unsigned long score = 0;
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP);
lcd.init();
lcd.backlight();
lcd.createChar(0, dino);
lcd.createChar(1, cacti);
lcd.setCursor(0, 0);
lcd.print("Dino Game V1");
delay(1500);
lcd.clear();
}
void loop() {
if (!gameRunning) {
waitForStart();
return;
}
handleInput();
updateGame();
drawGame();
delay(10);
}
void waitForStart() {
lcd.setCursor(0, 0);
lcd.print("Press to start");
if (digitalRead(BUTTON_PIN) == LOW) {
delay(200);
resetGame();
lcd.clear();
gameRunning = true;
}
}
void handleInput() {
if (digitalRead(BUTTON_PIN) == LOW && !jumping) {
jumping = true;
jumpStart = millis();
}
if (jumping && millis() - jumpStart > 400) {
jumping = false;
}
dinoY = jumping ? 0 : 1;
}
void updateGame() {
if (millis() - lastMove > speedDelay) {
lastMove = millis();
prevObstacleX = obstacleX;
prevObstacleY = obstacleY;
obstacleX--;
if (obstacleX < 0) {
lcd.setCursor(0, prevObstacleY);
lcd.print(" ");
obstacleX = 15;
obstacleY = 1;
prevObstacleX = obstacleX;
prevObstacleY = obstacleY;
score++;
}
checkCollision();
}
}
void checkCollision() {
if (obstacleX == 1 && obstacleY == dinoY) {
gameOver();
}
}
void gameOver() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("GAME OVER");
lcd.setCursor(0, 1);
lcd.print("Score:");
lcd.print(score);
gameRunning = false;
}
void resetGame() {
lcd.clear();
score = 0;
obstacleX = 15;
obstacleY = 1;
prevObstacleX = 15;
prevObstacleY = 1;
speedDelay = 300;
}
void drawGame() {
lcd.setCursor(prevObstacleX, prevObstacleY);
lcd.print(" ");
lcd.setCursor(obstacleX, obstacleY);
lcd.write(byte(1));
lcd.setCursor(1, 0); lcd.print(" ");
lcd.setCursor(1, 1); lcd.print(" ");
lcd.setCursor(1, dinoY);
lcd.write(byte(0));
lcd.setCursor(10, 0);
lcd.print(" ");
lcd.setCursor(10, 0);
lcd.print(score);
}