#include <LiquidCrystal_I2C.h>
#include <time.h>


LiquidCrystal_I2C lcd(0x27,20,4); //initializing LCD (address, width, height)


void setup() {


  int buttonPin = 2;
  pinMode(buttonPin, INPUT);

  int dino_x=1;




  lcd.init();
  lcd.backlight();
  uint8_t tree[8] = {
      0b00000,
      0b01000,
      0b01000,
      0b01000,
      0b01100,
      0b01100,
      0b11100,
      0b11100,

    };
  lcd.createChar(3,tree);

  uint8_t chare[8] = {
    0b00000,
    0b01010,
    0b11111,
    0b11111,
    0b01110,
    0b00100,
    0b00000,
    0b00000,

    };
  lcd.createChar(4,chare);

  };

void move_tree(int i, int s){ 
 
  lcd.setCursor(i,1);
  lcd.write(3);
  lcd.setCursor(s, 1);
  lcd.write(3);
  delay(150);
  lcd.setCursor(i,1);
  lcd.print(" ");  
  lcd.setCursor(s,1);
  lcd.print(" "); 

}

void move_dino(int BS, int x){

  if(BS ==1){
    lcd.setCursor(x, 1);
    lcd.print(" ");
    lcd.setCursor(x, 0);;
    lcd.write(4);
  }

  else if(BS ==0){
    lcd.setCursor(x, 0);
    lcd.print(" ");
    lcd.setCursor(x, 1);
    lcd.write(4);
  }

}

void game(int x){

  int i = 16 ;
  int s = 24;
  bool BS;



  do{

    int buttonPin = 2;
    int buttonState = digitalRead(buttonPin);
    BS = buttonState;


    
    

    move_dino(buttonState, x);

    move_tree(i,s);
    

    i--;
    s--;

    if (i==0){
      i = random(16,20);
    }

    else if(s==0){
      s = random(20,24);
    }

  }

  while((i>=0) && (s>=0));


}




void loop() {

  game(1);



}


// THINGS TO DO YET: 

    // - Write the score function.
    // - Write the function for the end game condition (aka when the tree and the dino collidde)
    // - write the score function 
    // - Write the move_dino function 
    // - Write the game function that calls the move_dino ands the move_tree function
    // - Write the function for the max time the dino can stay in the air before it 
    //   starts falling down.
    // - Make the move dino function buitton-press-time sensitive
    // - correct the functions for two trees coming simullateneosuly so that they dont come
    //   together.