#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#define PIN_BUTTON 2 // Pino onde o botao esta configurado
#define SPRITE_RUN 1 //Car sprite
#define SPRITE_TERRAIN_EMPTY ' ' // bloco vazio (Espacos em branco entre obstaculos)
#define SPRITE_TERRAIN_SOLID 5
#define SPRITE_TERRAIN_SOLID_RIGHT 6
#define SPRITE_TERRAIN_SOLID_LEFT 7
#define CAR_HORIZONTAL_POSITION 1 // Horizontal position of CAR on screen
#define TERRAIN_WIDTH 16 // essa variavel indicar o tamanho maximo de espaco
#define TERRAIN_EMPTY 0
#define TERRAIN_LOWER_BLOCK 1
#define TERRAIN_UPPER_BLOCK 2
#define CAR_POSITION_OFF 0 // CAR is invisible
#define CAR_POSITION_RUN_LOWER 1 // CAR is running on lower row
#define CAR_POSITION_RUN_UPPER 11 // CAR is running on upper row
LiquidCrystal_I2C lcd(0x27, 16, 2);
static char terrainUpper[TERRAIN_WIDTH + 1];
static char terrainLower[TERRAIN_WIDTH + 1];
static bool buttonPushed = false;
static byte CARPos = CAR_POSITION_RUN_LOWER; // CAR positon initial
static byte newTerrainType = TERRAIN_EMPTY;
static byte newTerrainDuration = 1;
static bool playing = false;
static bool blink = true;
static unsigned int distance = 0;
int total = 0;
// Esse metodo cria o desenho do carro que vai subir e descer e da sequencia de carros que serao os obstaculos
void initializeGraphics() {
static const byte graphics[][8] = {
// Run position 1
{0b00000, 0b00000, 0b00100, 0b11110, 0b11110, 0b11111, 0b01001, 0b00000},
// Run position 2
{0b00000, 0b00000, 0b00100, 0b11110, 0b11110, 0b11111, 0b01001, 0b00000},
// Jump
{0b00000, 0b00000, 0b00100, 0b11110, 0b11110, 0b11111, 0b01001, 0b00000},
// Jump lower
{0b00000, 0b00000, 0b00100, 0b11110, 0b11110, 0b11111, 0b01001, 0b00000},
// Ground
{0b00000, 0b00000, 0b00100, 0b11110, 0b11110, 0b11111, 0b01001, 0b00000},
// Ground right
{0b00000, 0b00000, 0b00100, 0b11110, 0b11110, 0b11111, 0b01001, 0b00000},
// Ground left
{0b00000, 0b00000, 0b00100, 0b11110, 0b11110, 0b11111, 0b01001, 0b00000}
};
for (int i = 0; i < 7; ++i) {
lcd.createChar(i + 1, graphics[i]);
}
for (int i = 0; i < TERRAIN_WIDTH; ++i) {
terrainUpper[i] = SPRITE_TERRAIN_EMPTY;
terrainLower[i] = SPRITE_TERRAIN_EMPTY;
}
}
// Slide the terrain to the left in half-character increments
// Faz a logica para saber obstaculo sera criado
void advanceTerrain(char* terrain, byte newTerrain) {
for (int i = 0; i < TERRAIN_WIDTH; ++i) {
char current = terrain[i];
char next = (i == TERRAIN_WIDTH - 1) ? newTerrain : terrain[i + 1]; // Quando o i for 15, sera usado newTerrain
// Condicao para criar os espacos vazios e obstaculos
// o terrain[i] vai recebendo os valores de acordo com as variaveis passadas.
switch (current) {
case SPRITE_TERRAIN_EMPTY:
terrain[i] = (next == SPRITE_TERRAIN_SOLID) ? SPRITE_TERRAIN_SOLID_RIGHT : SPRITE_TERRAIN_EMPTY;
break;
case SPRITE_TERRAIN_SOLID:
terrain[i] = (next == SPRITE_TERRAIN_EMPTY) ? SPRITE_TERRAIN_SOLID_LEFT : SPRITE_TERRAIN_SOLID;
break;
case SPRITE_TERRAIN_SOLID_RIGHT:
terrain[i] = SPRITE_TERRAIN_SOLID;
break;
case SPRITE_TERRAIN_SOLID_LEFT:
terrain[i] = SPRITE_TERRAIN_EMPTY;
break;
}
}
}
// Esse metodo distingue como sera renderizado o carro e a sua posicao
// calcula o score
bool drawCAR(byte position, char* terrainUpper, char* terrainLower, unsigned int score) {
bool collide = false;
char upperSave = terrainUpper[CAR_HORIZONTAL_POSITION];
char lowerSave = terrainLower[CAR_HORIZONTAL_POSITION];
byte upper, lower;
switch (position) {
case CAR_POSITION_OFF:
upper = lower = SPRITE_TERRAIN_EMPTY;
break;
case CAR_POSITION_RUN_LOWER:
upper = SPRITE_TERRAIN_EMPTY;
lower = SPRITE_RUN;
break;
case CAR_POSITION_RUN_UPPER:
upper = SPRITE_RUN;
lower = SPRITE_TERRAIN_EMPTY;
break;
}
if (upper != ' ') {
terrainUpper[CAR_HORIZONTAL_POSITION] = upper;
collide = (upperSave == SPRITE_TERRAIN_EMPTY) ? false : true;
}
if (lower != ' ') {
terrainLower[CAR_HORIZONTAL_POSITION] = lower;
collide |= (lowerSave == SPRITE_TERRAIN_EMPTY) ? false : true;
}
// Dificuldade é aumentada ao atingir a pontuacao de 9 a 9999
byte digits = (score > 9999) ? 5 : (score > 999) ? 4 : (score > 99) ? 3 : (score > 9) ? 2 : 1;
// Draw the scene
terrainUpper[TERRAIN_WIDTH] = '\0';
terrainLower[TERRAIN_WIDTH] = '\0';
char temp = terrainUpper[16 - digits];
terrainUpper[16 - digits] = '\0';
lcd.setCursor(0, 0);
lcd.print(terrainUpper);
terrainUpper[16 - digits] = temp;
lcd.setCursor(0, 1);
lcd.print(terrainLower);
lcd.setCursor(16 - digits, 0);
lcd.print(score);
// variable receives the value score to show in lcd
total = score;
terrainUpper[CAR_HORIZONTAL_POSITION] = upperSave;
terrainLower[CAR_HORIZONTAL_POSITION] = lowerSave;
return collide;
}
// Handle the button push as an interrupt
// é o metodo que altera o valor do buttonPushed (botao foi acionado)
void buttonPush() {
buttonPushed = true;
}
// Esse metodo configura as infomacoes do arduino
// o metodo pinMode pega o valor do pin (2) onde esta o botao
void setup() {
pinMode(PIN_BUTTON, INPUT);
digitalWrite(PIN_BUTTON, HIGH);
// Digital pin 2 maps to interrupt 0
attachInterrupt(0/*PIN_BUTTON*/, buttonPush, FALLING);
initializeGraphics();
lcd.init();
lcd.backlight();
}
void loop() {
// Quando a variavel playing estiver false, significa que o jogo ainda nao comecou, e ficara exibindo a mensagem press start
if (!playing) {
drawCAR((blink) ? CAR_POSITION_OFF : CARPos, terrainUpper, terrainLower, distance >> 3);
if (blink) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Press Start");
lcd.setCursor(0, 1);
lcd.print("F - uno");
delay(500);
}
delay(100);
// Quando for clicado o botao, iniciara o jogo. Essa condicao se for true(verdade), renderiza os graficos (carros e obstaculos)
// Ao aperta o botao pela primeira vez ira iniciar o jogo e nao passara por esse if(!playing) novamente, so voltara
// a passar nesse if caso seja gameover
if (buttonPushed) {
initializeGraphics();
playing = true;
buttonPushed = false;
distance = 0;
blink = false;
}
return;
}
// Shift the terrain to the left
// esse metodo recebe valores para desenhar a obstaculos da rota
advanceTerrain(terrainLower, newTerrainType == TERRAIN_LOWER_BLOCK ? SPRITE_TERRAIN_SOLID : SPRITE_TERRAIN_EMPTY);
advanceTerrain(terrainUpper, newTerrainType == TERRAIN_UPPER_BLOCK ? SPRITE_TERRAIN_SOLID : SPRITE_TERRAIN_EMPTY);
// Make new terrain to enter on the right
// Essa parte gera randomicamente os carros e o tempo que ele permanecerá na rota
if (--newTerrainDuration == 0) {
if (newTerrainType == TERRAIN_EMPTY) {
newTerrainType = (random(3) == 0) ? TERRAIN_UPPER_BLOCK : TERRAIN_LOWER_BLOCK;
newTerrainDuration = 6 + random(3);
} else {
newTerrainType = TERRAIN_EMPTY;
newTerrainDuration = 6 + random(3);
}
}
// Esse if checara se o carro bateu em algum obstaculo, caso bata, sera game over, caso nao, ele continuara
// a mudar as posicoes do carro para LOW ou HIGH
if (drawCAR(CARPos, terrainUpper, terrainLower, distance >> 3)) {
gameOver();
} else {
if (CARPos == CAR_POSITION_RUN_LOWER && buttonPushed) {
CARPos = CAR_POSITION_RUN_UPPER;
buttonPushed = false;
} else if (CARPos == CAR_POSITION_RUN_UPPER && buttonPushed) {
CARPos = CAR_POSITION_RUN_LOWER;
buttonPushed = false;
}
}
++distance;
}
// metodo printa na tela o texto gamer over e a distancia percorrida, reseta as variaveis.
void gameOver() {
playing = false;
buttonPushed = false;
CARPos = CAR_POSITION_RUN_LOWER;
distance = 0;
lcd.clear();
lcd.print("Game over");
lcd.setCursor(0, 1);
lcd.print("score: ");
lcd.print(total);
delay(2000);
blink = true;
}