#include <LiquidCrystal.h> //biblioteca LiquidCrystal
#include <ezButton.h>
const int rs = 8;
const int en = 9;
const int d4 = 4;
const int d5 = 5;
const int d6 = 6;
const int d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
ezButton button1(2);
int botao = 2;
int score = 0;
int bestScore = 0;
byte dinoChar[8] = {
0b00110,
0b00111,
0b01110,
0b01111,
0b01111,
0b11110,
0b01110,
0b01010
};
byte cactus[] = {
B01000,
B01010,
B01110,
B00101,
B10111,
B11100,
B00100,
B00100
};
byte meteor[] = {
B00000,
B00111,
B01100,
B11110,
B01100,
B00111,
B00000,
B00000
};
// 20 f
// 1350 b
// 10 v
void setup() {
lcd.begin(16, 2);// LCD - 16 colunas por 2 linhas
pinMode(botao, INPUT);
button1.setDebounceTime(0);
lcd.createChar(0, dinoChar);
lcd.createChar(1, cactus);
lcd.createChar(2, meteor);
Serial.begin(9600);
randomSeed(analogRead(0));
}
void game() {
score = 0;
int cacto = 16;
int meteoro = 24; // 24
float speed = 100;
int dinoX = 1;
int dinoY = 2;
int jump = 0;
bool canJump = false;
unsigned long previousMillis = 0;
while (true) {
button1.loop();
unsigned long milliseconds = millis();
if (meteoro == 0 || cacto == 0) score++;
if ((dinoX == cacto && dinoY == 2) || (dinoX == meteoro && dinoY == 0)) {
if (score > bestScore) bestScore = score;
break;
}
if (cacto == 0 || meteoro == 0) {
int r = random(16, 20);
int r2 = random(2);
Serial.println(r2);
if (r2 == 0 && cacto <= 0) cacto = r;
if (r2 == 1 && meteoro <= 0) meteoro = r;
}
if (button1.isPressed() && canJump) {
if (milliseconds - previousMillis >= 200) {
previousMillis = milliseconds;
digitalWrite(10, HIGH);
delay(50);
digitalWrite(10, LOW);
}
canJump = false;
jump = 5;
}
if (jump > 0) {
dinoY = 0;
jump--;
} else {
canJump = true;
dinoY = 2;
}
lcd.clear();
lcd.setCursor(dinoX, dinoY);
lcd.write((uint8_t)0);
if (cacto >= 0) {
lcd.setCursor(cacto, 2);
lcd.write((uint8_t)1);
}
if (meteoro >= 0) {
lcd.setCursor(meteoro, 0);
lcd.write((uint8_t)2);
}
lcd.setCursor(12, 0);
lcd.print(score);
delay(speed);
cacto--;
meteoro--;
if (score == 25 && speed == 100) speed -= 25;
if (score == 50 && speed == 125) speed -= 25;
if (score == 75 && speed == 100) speed -= 25;
if (score == 100 && speed > 32) speed--;
Serial.println(meteoro);
}
}
void loop() {
button1.loop();
lcd.clear();
lcd.write((uint8_t)0);
lcd.print(" Dino Race ");
lcd.write((uint8_t)0);
lcd.setCursor(0, 2);
lcd.print(score);
lcd.setCursor(7, 2);
lcd.print(score == bestScore && score != 0 ? "New score" : "CC - UFFS");
delay(1000);
if (button1.isPressed()) game();
}