#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);
//define joystick
#define vx A1
#define vy A0
//directions of Dinosaur
int VRx = 1;
int VRy = 0;
byte row;
byte i;
uint16_t score = 0;
unsigned long time;
unsigned long currentTime;
bool millisCounter = false;
//create character for killing arrow D:
byte arrow[8] = {
B00001,
B00011,
B00111,
B01111,
B01111,
B00111,
B00011,
B00001
};
//create character for Dinosaur :D
byte dino[8] = {
B00110,
B01101,
B01111,
B01110,
B11110,
B11110,
B11100,
B00100
};
void dinoMove() //method for moving the Dinosaur on lcd 20x4
{
if(analogRead(vy) == 1023 && analogRead(vx) == 512)
{
lcd.setCursor(VRy,VRx);
lcd.write(byte(0));
if(VRx <= 0)VRx=4;
VRx = VRx - 1;
lcd.setCursor(VRy,VRx);
lcd.write(byte(2));
delay(150);
}
if(analogRead(vy) == 0 && analogRead(vx) == 512)
{
lcd.setCursor(VRy,VRx);
lcd.write(byte(0));
VRx++;
if(VRx >= 4)VRx=0;
lcd.setCursor(VRy,VRx);
lcd.write(byte(2));
delay(150);
}
if(analogRead(vx) == 1023 && analogRead(vy) == 512)
{
lcd.setCursor(VRy,VRx);
lcd.write(byte(0));
if(VRy <= 0)VRy=20;
VRy = VRy - 1;
lcd.setCursor(VRy,VRx);
lcd.write(byte(2));
delay(150);
}
if(analogRead(vx) == 0 && analogRead(vy) == 512)
{
lcd.setCursor(VRy,VRx);
lcd.write(byte(0));
VRy++;
if(VRy >= 20)VRy=0;
lcd.setCursor(VRy,VRx);
lcd.write(byte(2));
delay(150);
}
}
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
//Create all new characters
lcd.createChar(1,arrow);
lcd.createChar(2,dino);
//pins of joystick
pinMode(A0, INPUT);
pinMode(A1, INPUT);
//start the game
lcd.setCursor(VRy, VRx);
lcd.write(byte(2));
}
void loop() {
row = random(4);//select a random value for row between{0,1,2,3}.
for(i = 20;i>=0;i=i-1)
{
lcd.setCursor(i,row);
lcd.write(1);
dinoMove();
if((VRy == i) && (VRx == row))
{
Serial.print("Score = ");
score = score +1;
Serial.println(score);
lcd.setCursor(VRy,VRx);
lcd.write(byte(2));
i = 19;
break;
}
if((VRy == (i-1)) && (VRx == row))
{
lcd.setCursor(i,row);
lcd.write(byte(1));
lcd.setCursor(VRy,VRx);
lcd.write(byte(2));
for(int t = 0;t<=10000;t++)
{
if(millisCounter == false)
{
time = millis();
millisCounter = true;
}
dinoMove();
currentTime = millis() - time;
if(currentTime >= 2000)
{
Serial.println("Game Over");
Serial.print("Your Score = ");
Serial.println(score);
Serial.println("===============");
delay(500);
lcd.clear();
score = 0;
VRx = 1;
VRy = 0;
i = 20;
millisCounter = false;
lcd.setCursor(VRy,VRx);
lcd.write(byte(2));
row = random(4);
break;
}
if((i == VRy) && (row == VRx))
{
Serial.print("Score = ");
score = score +1;
Serial.println(score);
lcd.setCursor(i,row);
lcd.write(byte(2));
i = 19;
millisCounter = false;
row = random(4);
break;
}
delay(1);
}
}
delay(90);
lcd.setCursor(i,row);
lcd.print(" ");
dinoMove();
if(i==0)break;
}
}