Skip to content

Instantly share code, notes, and snippets.

@Caellian
Last active July 2, 2018 22:03
Show Gist options
  • Select an option

  • Save Caellian/c771559dada93d3ab28bd40ede82ba36 to your computer and use it in GitHub Desktop.

Select an option

Save Caellian/c771559dada93d3ab28bd40ede82ba36 to your computer and use it in GitHub Desktop.
Avoid cars on LCD.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x27
#define Rs_pin 0
#define Rw_pin 1
#define En_pin 2
#define Bl_pin 3
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
#define UP_BUTTON 7
#define DOWN_BUTTON 8
byte playerChar[8] = {
B00000,
B01110,
B01110,
B00100,
B01110,
B10101,
B00100,
B01010,
};
byte carChar[8] = {
B00000,
B00000,
B00000,
B00000,
B01111,
B11001,
B11111,
B01010,
};
bool road[3][17] = {
{false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
{false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
{false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
};
int score = -16;
int character = 1;
int buttonUpLast = LOW;
int buttonDownLast = LOW;
int carSpeed = 500;
bool canMove = false;
unsigned long lastMillis = millis();
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
void setup() {
Serial.begin(9600);
// Setup pin modes
pinMode(UP_BUTTON, INPUT);
pinMode(DOWN_BUTTON, INPUT);
lcd.begin(16,4);
lcd.setBacklightPin(Bl_pin, POSITIVE);
lcd.setBacklight(HIGH);
// Create characters
lcd.createChar(0, playerChar);
lcd.createChar(1, carChar);
lcd.home();
lcd.print("Score: ");
}
void updateCarMatrix() {
for (int i = 0; i < 16; i++) {
road[0][i] = road[0][i+1];
road[1][i] = road[1][i+1];
road[2][i] = road[2][i+1];
}
switch(random(0, 4)) {
case 0:
road[0][15] = true;
break;
case 1:
road[1][15] = true;
break;
case 2:
road[2][15] = true;
break;
}
}
void resetGameStats() {
for (int h = 0; h < 3; h++) {
for (int w = 0; w < 16; w++) {
road[h][w] = false;
}
}
canMove = false;
score = -16;
character = 1;
}
void gameWon() {
lcd.clear();
lcd.setCursor(3,1);
lcd.print("Game won!");
lcd.setCursor(1,1);
lcd.print("You're a God..");
lcd.setCursor(3,2);
lcd.print("Score: ");
lcd.setCursor(10,2);
lcd.print(score, DEC);
delay(1000);
lcd.setCursor(0,2);
lcd.print(" ");
delay(500);
lcd.setCursor(3,2);
lcd.print("Score: ");
lcd.setCursor(10,2);
lcd.print(score, DEC);
delay(1000);
lcd.setCursor(0,2);
lcd.print(" ");
delay(500);
lcd.setCursor(3,2);
lcd.print("Score: ");
lcd.setCursor(10,2);
lcd.print(score, DEC);
delay(1000);
lcd.setCursor(0,2);
lcd.print(" ");
delay(500);
lcd.setCursor(3,2);
lcd.print("Score: ");
lcd.setCursor(10,2);
lcd.print(score, DEC);
delay(2000);
resetGameStats();
}
void gameOver() {
lcd.clear();
lcd.setCursor(3,1);
lcd.print("Game over!");
lcd.setCursor(3,2);
lcd.print("Score: ");
lcd.setCursor(10,2);
lcd.print(score, DEC);
delay(1000);
lcd.setCursor(0,2);
lcd.print(" ");
delay(500);
lcd.setCursor(3,2);
lcd.print("Score: ");
lcd.setCursor(10,2);
lcd.print(score, DEC);
delay(1000);
lcd.setCursor(0,2);
lcd.print(" ");
delay(500);
lcd.setCursor(3,2);
lcd.print("Score: ");
lcd.setCursor(10,2);
lcd.print(score, DEC);
delay(1000);
lcd.setCursor(0,2);
lcd.print(" ");
delay(500);
lcd.setCursor(3,2);
lcd.print("Score: ");
lcd.setCursor(10,2);
lcd.print(score, DEC);
delay(2000);
resetGameStats();
}
void loop() {
// Detect character movement
int up = digitalRead(UP_BUTTON);
int down = digitalRead(DOWN_BUTTON);
if (up == HIGH && down == LOW && up != buttonUpLast) {
character -= 1;
if (character < 0) character = 2;
} else if (up == LOW && down == HIGH && down != buttonDownLast) {
character += 1;
character %= 3;
}
if (!canMove && (up != buttonUpLast || down != buttonDownLast)) {
randomSeed(millis());
canMove = true;
character = 1;
}
buttonUpLast = up;
buttonDownLast = down;
if (score >= 500) {
gameWon();
}
if (millis() < lastMillis) {
lastMillis = millis();
} else if (canMove && millis() - lastMillis >= carSpeed) {
updateCarMatrix();
score++;
carSpeed = 500 - score;
lastMillis = millis();
}
// Print score
lcd.home();
lcd.print("Score: ");
lcd.setCursor(7,0);
lcd.print(" ");
lcd.setCursor(7,0);
if (score <= 0) {
lcd.print(0, DEC);
} else {
lcd.print(score, DEC);
}
// Draw cars
for (int h = 1; h < 4; h++) {
for (int w = 0; w < 16; w++) {
lcd.setCursor(w,h);
if(road[h-1][w]) {
lcd.write(byte(1));
} else if (w != 0) {
lcd.print(" ");
}
}
}
// Draw character
switch (character) {
case 0:
if(!road[1][0]) {
lcd.setCursor(0,2);
lcd.print(" ");
}
if(!road[2][0]) {
lcd.setCursor(0,3);
lcd.print(" ");
}
lcd.setCursor(0,1);
break;
case 1:
if(!road[0][0]) {
lcd.setCursor(0,1);
lcd.print(" ");
}
if(!road[2][0]) {
lcd.setCursor(0,3);
lcd.print(" ");
}
lcd.setCursor(0,2);
break;
case 2:
if(!road[0][0]) {
lcd.setCursor(0,1);
lcd.print(" ");
}
if(!road[1][0]) {
lcd.setCursor(0,2);
lcd.print(" ");
}
lcd.setCursor(0,3);
break;
}
lcd.write(byte(0));
if (!canMove) {
lcd.setCursor(3,2);
lcd.print("Press any");
lcd.setCursor(0,3);
lcd.print("button to start!");
}
if ((road[0][0] && character == 0) || (road[1][0] && character == 1) || (road[2][0] && character == 2)){
gameOver();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment