#include <Wire.h>
#include <key>
#include <LiquidCrystal_I2C.h>
//LiquidCrystal_I2C lcd(0x27, 16,2);
//int SpielerX=0;
//int SpielerY=1;
//int HindernisX=15;
//int HindernisY=1;
//int Punktestand=0;
#define CMK_HARDWARE
#ifdef CMK_HARDWARE
#define RS 8
#define En 9
#define D4 4
#define D5 5
#define D6 6
#define D7 7
#else
#define RS 12
#define En 11
#define D4 5
#define D5 4
#define D6 3
#define D7 2
#endif
//LiquidCrystal_I2C Lcd(RS, En, D4, D5, D6, D7);
LiquidCrystal_I2C Lcd(0x27, 16,2);
int score=0;
bool allow_jump= false;
byte dino_1[8]={
B00000111,
B00000101,
B00000111,
B00010110,
B00011111,
B00011110,
B00001110,
B00000100
};
byte dino_r[8] = {
B00000111,
B00000101,
B00000111,
B00010110,
B00011111,
B00011110,
B00001110,
B00000010
};
byte cactus_small[8]= {
B00000000,
B00000000,
B00000100,
B00000101,
B00010101,
B00010110,
B00001100,
B00000100
};
byte cactus_big[8] ={
B00000000,
B00000100,
B00000101,
B00010101,
B00010110,
B00001100,
B00000100,
B00000100
};
char world[]={
32,32,32,32,32,32,32,83,99,111,114,101,58,32,32,32,32,0,32,32,32,32,32,32,32,32,32,32,32,32,32,32,
};
uint8_t scroll_world(){
#ifdef CMK_HARDWARE
delay(250);
#endif
char random_object = random(2, 35);
if(random_object<4) world[31]=random_object;
else world[31] = 32;
for (int i=16; i<32; i++){
if (world[i] ==2 or world[i] == 3){
char prev = (i <31) ? world[i+1]:32;
if(world[i-1]<2) return 1;
world[i-1]= world[i];
world[i]=prev;
}
}
world[15]=32;
if(world[16]<2) world[16]=32;
return 0;
}
void update_world(){
int game_over=scroll_world();
if(score==999){
Lcd.setCursor(0, 0);
Lcd.print(" YOU WIN! ");
Lcd.setCursor(0, 1);
Lcd.write(byte(0));
Lcd.write(byte(32));
Lcd.write(byte(2));
Lcd.write(byte(2));
Lcd.write(byte(2));
Lcd.write(byte(3));
Lcd.write(byte(3));
Lcd.write(byte(3));
Lcd.write(byte(3));
Lcd.write(byte(3));
Lcd.write(byte(3));
Lcd.write(byte(2));
Lcd.write(byte(2));
Lcd.write(byte(2));
Lcd.write(byte(32));
Lcd.write(byte(1));
while(1);
}
if (game_over){
Lcd.setCursor(0, 1);
Lcd.write(byte(0));
Lcd.write(byte(3));
Lcd.print(" GAME OVER! ");
Lcd.write(byte(3));
Lcd.write(byte(0));
while(1);
}
score++;
Lcd.setCursor(13, 0);
Lcd.print(score);
Lcd.setCursor(0, 0);
for (int i=0; i<32; i++) {
if (world[i] <2) world[i] ^=1;
if (i ==16) Lcd.setCursor(0, 1);
if (i<13 || i>15)
Lcd.write(byte(world[i]));
}
}
bool get_button(){
#ifdef CMK_HARDWARE
int shield_input;
shield_input=analogRead(0);
if (shield_input < 200) return LOW;
else return HIGH;
#else
return digitalRead(7);
#endif
}
void setup() {
#ifndef CMK_HARDWARE
pinMode(7, INPUT_PULLUP);
#endif
Lcd.init();
Lcd.backlight();
Lcd.createChar(0, dino_1);
Lcd.createChar(1, dino_r);
Lcd.createChar(2, cactus_small);
Lcd.createChar(3, cactus_big);
Lcd.begin(16, 2);
//Lcd.init ();
//Lcd.print("Dino Game");
//delay(3000);
// Lcd.clear();
// put your setup code here, to run once:
}
void loop() {
Lcd.setCursor(0, 0);
Lcd.print(" ARE YOU READY? ");
while(get_button() == HIGH);
while(true){
allow_jump^=1;
if (get_button() == LOW && allow_jump == true){
Lcd.setCursor(1, 1);
Lcd.write(byte(32));
Lcd.setCursor(1, 0);
Lcd.write(byte(0));
world[1] = byte(0);
world[17]= byte(32);
for (int i =0; i<4; i++) update_world();
world[1]=byte(32);
world[17]= byte(0);
Lcd.setCursor(1, 0);
Lcd.write(byte(32));
Lcd.setCursor(1, 1);
Lcd.write(byte(0));
}
update_world();
}
}