#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
};
int dinoY = 1;
bool jumping = false;
unsigned long jumpStart = 0;
int obstacleX = 15;
int prevObstacleX = 15;
unsigned long lastMove = 0;
int speedDelay = 300;
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 V0.1");
delay(1000);
lcd.clear();
}
void loop() {
handleInput();
updateObstacle();
drawGame();
delay(10);
}
void handleInput() {
if (digitalRead(BUTTON_PIN) == LOW && !jumping) {
jumping = true;
jumpStart = millis();
}
if (jumping && millis() - jumpStart > 400) {
jumping = false;
}
dinoY = jumping ? 0 : 1;
}
void updateObstacle() {
if (millis() - lastMove > speedDelay) {
lastMove = millis();
prevObstacleX = obstacleX;
obstacleX--;
if (obstacleX < 0) {
// clear last visible cactus at column 0
lcd.setCursor(0, 1);
lcd.print(" ");
obstacleX = 15;
prevObstacleX = obstacleX;
}
}
}
void drawGame() {
// erase previous cactus
lcd.setCursor(prevObstacleX, 1);
lcd.print(" ");
// draw cactus
lcd.setCursor(obstacleX, 1);
lcd.write(byte(1));
// erase dino
lcd.setCursor(1, 0); lcd.print(" ");
lcd.setCursor(1, 1); lcd.print(" ");
// draw dino
lcd.setCursor(1, dinoY);
lcd.write(byte(0));
}