#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#define PIN_BUTTON 2
#define PIN_AUTOPLAY 1
#define SPRITE_RUN1 1
#define SPRITE_RUN2 2
#define SPRITE_JUMP 3
#define SPRITE_JUMP_UPPER '.'
#define SPRITE_JUMP_LOWER 4
#define SPRITE_TEREN_EMPTY ' '
#define SPRITE_TEREN_SOLID 5
#define SPRITE_TEREN_SOLID_RIGHT 6
#define SPRITE_TEREN_SOLID_LEFT 7
#define PLANE_HORIZONTAL_POSITION 1
#define TEREN_WIDTH 16
#define TEREN_EMPTY 0
#define TEREN_LOWER_BLOCK 1
#define TEREN_UPPER_BLOCK 2
#define PLANE_POSITION_OFF 0
#define PLANE_POSITION_RUN_LOWER_1 1
#define PLANE_POSITION_RUN_LOWER_2 2
#define PLANE_POSITION_JUMP_1 3
#define PLANE_POSITION_JUMP_2 4
#define PLANE_POSITION_JUMP_3 5
#define PLANE_POSITION_JUMP_4 6
#define PLANE_POSITION_JUMP_5 7
#define PLANE_POSITION_JUMP_6 8
#define PLANE_POSITION_JUMP_7 9
#define PLANE_POSITION_JUMP_8 10
#define PLANE_POSITION_RUN_UPPER_1 11
#define PLANE_POSITION_RUN_UPPER_2 12
#define GAME_STATE_START 0
#define GAME_STATE_PLAYING 1
LiquidCrystal_I2C lcd(0x27, 16, 2);
static char terenUpper[TEREN_WIDTH + 1];
static char terenLower[TEREN_WIDTH + 1];
static bool buttonPushed = false;
int gameState = GAME_STATE_START;
static byte PLANEPos = PLANE_POSITION_RUN_LOWER_1;
static byte newTerenType = TEREN_EMPTY;
static byte newTerenDuration = 1;
static bool playing = false;
static bool blink = false;
static unsigned int dystans = 0;
void Graficzka() {
static byte graphics[] = {
// Run position 1
0b00000,
0b01100,
0b01110,
0b11111,
0b01110,
0b01100,
0b00000,
0b00000,
// Run position 2
0b00000,
0b01100,
0b01110,
0b11111,
0b01110,
0b01100,
0b00000,
0b00000,
// Jump
0b00000,
0b00000,
0b01100,
0b01110,
0b11111,
0b01110,
0b01100,
0b00000,
// Jump lower
0b00000,
0b00000,
0b01100,
0b01110,
0b11111,
0b01110,
0b01100,
0b00000,
// Ground
0b11111,
0b11111,
0b10101,
0b11111,
0b10101,
0b11111,
0b10101,
0b11111,
// Ground right
0b11111,
0b11111,
0b10101,
0b11111,
0b10101,
0b11111,
0b10101,
0b11111,
// Ground left
0b11111,
0b11111,
0b10101,
0b11111,
0b10101,
0b11111,
0b10101,
0b11111,
};
int i;
for (i = 0; i < 7; ++i) {
lcd.createChar(i + 1, &graphics[i * 8]);
}
for (i = 0; i < TEREN_WIDTH; ++i) {
terenUpper[i] = SPRITE_TEREN_EMPTY;
terenLower[i] = SPRITE_TEREN_EMPTY;
}
}
void advanceTeren(char* teren, byte newTeren) {
for (int i = 0; i < TEREN_WIDTH; ++i) {
char current = teren[i];
char next = (i == TEREN_WIDTH - 1) ? newTeren : teren[i + 1];
switch (current) {
case SPRITE_TEREN_EMPTY:
teren[i] = (next == SPRITE_TEREN_SOLID) ? SPRITE_TEREN_SOLID_RIGHT : SPRITE_TEREN_EMPTY;
break;
case SPRITE_TEREN_SOLID:
teren[i] = (next == SPRITE_TEREN_EMPTY) ? SPRITE_TEREN_SOLID_LEFT : SPRITE_TEREN_SOLID;
break;
case SPRITE_TEREN_SOLID_RIGHT:
teren[i] = SPRITE_TEREN_SOLID;
break;
case SPRITE_TEREN_SOLID_LEFT:
teren[i] = SPRITE_TEREN_EMPTY;
break;
}
}
}
bool drawPLANE(byte position, char* terenUpper, char* terenLower, unsigned int score) {
bool collide = false;
char upperSave = terenUpper[PLANE_HORIZONTAL_POSITION];
char lowerSave = terenLower[PLANE_HORIZONTAL_POSITION];
byte upper, lower;
switch (position) {
case PLANE_POSITION_OFF:
upper = lower = SPRITE_TEREN_EMPTY;
break;
case PLANE_POSITION_RUN_LOWER_1:
upper = SPRITE_TEREN_EMPTY;
lower = SPRITE_RUN1;
break;
case PLANE_POSITION_RUN_LOWER_2:
upper = SPRITE_TEREN_EMPTY;
lower = SPRITE_RUN2;
break;
case PLANE_POSITION_JUMP_1:
case PLANE_POSITION_JUMP_8:
upper = SPRITE_TEREN_EMPTY;
lower = SPRITE_JUMP;
break;
case PLANE_POSITION_JUMP_2:
case PLANE_POSITION_JUMP_7:
upper = SPRITE_JUMP_UPPER;
lower = SPRITE_JUMP_LOWER;
break;
case PLANE_POSITION_JUMP_3:
case PLANE_POSITION_JUMP_4:
case PLANE_POSITION_JUMP_5:
case PLANE_POSITION_JUMP_6:
upper = SPRITE_JUMP;
lower = SPRITE_TEREN_EMPTY;
break;
case PLANE_POSITION_RUN_UPPER_1:
upper = SPRITE_RUN1;
lower = SPRITE_TEREN_EMPTY;
break;
case PLANE_POSITION_RUN_UPPER_2:
upper = SPRITE_RUN2;
lower = SPRITE_TEREN_EMPTY;
break;
}
if (upper != ' ') {
terenUpper[PLANE_HORIZONTAL_POSITION] = upper;
collide = (upperSave == SPRITE_TEREN_EMPTY) ? false : true;
}
if (lower != ' ') {
terenLower[PLANE_HORIZONTAL_POSITION] = lower;
collide |= (lowerSave == SPRITE_TEREN_EMPTY) ? false : true;
}
byte digits = (score > 9999) ? 5 : (score > 999) ? 4 : (score > 99) ? 3 : (score > 9) ? 2 : 1;
terenUpper[TEREN_WIDTH] = '\0';
terenLower[TEREN_WIDTH] = '\0';
char temp = terenUpper[16 - digits];
terenUpper[16 - digits] = '\0';
lcd.setCursor(0, 0);
lcd.print(terenUpper);
terenUpper[16 - digits] = temp;
lcd.setCursor(0, 1);
lcd.print(terenLower);
lcd.setCursor(16 - digits, 0);
lcd.print(score);
terenUpper[PLANE_HORIZONTAL_POSITION] = upperSave;
terenLower[PLANE_HORIZONTAL_POSITION] = lowerSave;
return collide;
}
void buttonPush() {
buttonPushed = true;
}
void setup() {
pinMode(PIN_BUTTON, INPUT);
digitalWrite(PIN_BUTTON, HIGH);
pinMode(PIN_AUTOPLAY, OUTPUT);
digitalWrite(PIN_AUTOPLAY, HIGH);
attachInterrupt(0, buttonPush, FALLING);
Graficzka();
lcd.init();
lcd.backlight();
}
void loop() {
if (gameState == GAME_STATE_START) {
drawPLANE((blink) ? PLANE_POSITION_OFF : PLANEPos, terenUpper, terenLower, dystans >> 3);
if (blink) {
lcd.setCursor(0, 0);
lcd.print("Starten");
}
delay(100);
blink = !blink;
if (buttonPushed) {
Graficzka();
PLANEPos = PLANE_POSITION_RUN_LOWER_1;
gameState = GAME_STATE_PLAYING;
buttonPushed = false;
dystans = 0;
}
return;
}
advanceTeren(terenLower, newTerenType == TEREN_LOWER_BLOCK ? SPRITE_TEREN_SOLID : SPRITE_TEREN_EMPTY);
advanceTeren(terenUpper, newTerenType == TEREN_UPPER_BLOCK ? SPRITE_TEREN_SOLID : SPRITE_TEREN_EMPTY);
if (--newTerenDuration == 0) {
if (newTerenType == TEREN_EMPTY) {
newTerenType = (random(3) == 0) ? TEREN_UPPER_BLOCK : TEREN_LOWER_BLOCK;
newTerenDuration = 10 + random(6);
} else {
newTerenType = TEREN_EMPTY;
newTerenDuration = 10 + random(6);
}
}
if (buttonPushed) {
if (PLANEPos <= PLANE_POSITION_RUN_LOWER_2)
PLANEPos = PLANE_POSITION_JUMP_1;
buttonPushed = false;
}
if (drawPLANE(PLANEPos, terenUpper, terenLower, dystans >> 3)) {
gameState = GAME_STATE_START;
} else {
if (PLANEPos == PLANE_POSITION_RUN_LOWER_2 || PLANEPos == PLANE_POSITION_JUMP_8) {
PLANEPos = PLANE_POSITION_RUN_LOWER_1;
} else if ((PLANEPos >= PLANE_POSITION_JUMP_3 && PLANEPos <= PLANE_POSITION_JUMP_5) && terenLower[PLANE_HORIZONTAL_POSITION] != SPRITE_TEREN_EMPTY) {
PLANEPos = PLANE_POSITION_RUN_UPPER_1;
} else if (PLANEPos >= PLANE_POSITION_RUN_UPPER_1 && terenLower[PLANE_HORIZONTAL_POSITION] == SPRITE_TEREN_EMPTY) {
PLANEPos = PLANE_POSITION_JUMP_5;
} else if (PLANEPos == PLANE_POSITION_RUN_UPPER_2) {
PLANEPos = PLANE_POSITION_RUN_UPPER_1;
} else {
++PLANEPos;
}
++dystans;
digitalWrite(PIN_AUTOPLAY, terenLower[PLANE_HORIZONTAL_POSITION + 2] == SPRITE_TEREN_EMPTY ? HIGH : LOW);
}
}