Components and supplies
Arduino UNO
Knowledge on operating your computer and the Arduino IDE
Kuman LCD Shield
Tools and machines
Hands
Computer
Apps and platforms
Arduino IDE
Project description
Code
bitmaps.h
h
Upload as a seperate tab named bitmaps.h
1// Bitmaps-Do not modify 2 3byte dino[8] = { 4 0b00000, 5 0b00111, 6 0b00111, 7 0b10110, 8 0b11111, 9 0b01010, 10 0b01010, 11 0b00000 12}; 13 14byte cacti[8] = { 15 0b00100, 16 0b00101, 17 0b10101, 18 0b10101, 19 0b10111, 20 0b11100, 21 0b00100, 22 0b00000 23}; 24 25byte bird[8] = { 26 0b00000, 27 0b00100, 28 0b01100, 29 0b11110, 30 0b00111, 31 0b00110, 32 0b00100, 33 0b00000 34}; 35 36byte block[8] = { 37 0b11111, 38 0b11111, 39 0b11111, 40 0b11111, 41 0b11111, 42 0b11111, 43 0b11111, 44 0b11111 45}; 46 47 48
Chrome_Dino_Game_on_LCD
c_cpp
Paste into editor as main sketch
1#include <LiquidCrystal.h> 2#include <EEPROM.h> 3 4#include "bitmaps.h" 5 6LiquidCrystal 7 lcd(8, 9, 4, 5, 6, 7); 8 9int adc_key_in = 0; 10 11#define btnRIGHT 0 12#define 13 btnUP 1 14#define btnDOWN 2 15#define btnLEFT 3 16#define btnSELECT 4 17#define 18 btnNONE 5 19 20byte runnerArea[16] {32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 21 32, 32, 32, 32, 32, 32}; 22byte jump = 32; 23 24int score = 0; 25bool freeze_score 26 = 0; 27 28byte correct_code = 123; 29 30unsigned long previousMillis = 0; 31unsigned 32 long previousMillisLED = 0; 33unsigned long jumpTime = 0; 34const int jumpLength 35 = 500; 36 37#define checkSafe runnerArea[1] == 32 || runnerArea[1] == 0 38 39const 40 byte chance_of_ob = 15; 41int speedOfScroller = 300; 42 43void setup() { 44 45 lcd.begin(16, 2); 46 Serial.begin(9600); 47 48 lcd.createChar(0, dino); 49 50 lcd.createChar(1, cacti); 51 lcd.createChar(2, bird); 52 lcd.createChar(3, 53 block); 54 pinMode(A0, INPUT); 55 pinMode(A1, INPUT); 56 pinMode(13, OUTPUT); 57 58 randomSeed(A1); 59 lcd.clear(); 60 showSplashScreen(1000, true); 61} 62 63void 64 loop() { 65 unsigned long currentMillis = millis(); 66 unsigned long currentMillisOb 67 = millis(); 68 if (currentMillisOb - previousMillis >= speedOfScroller) { 69 70 previousMillis = currentMillisOb; 71 if (random(chance_of_ob) == 0) { 72 73 runnerArea[15] = 1; 74 } else if (random(chance_of_ob) == 1) { 75 runnerArea[15] 76 = 2; 77 } else { 78 runnerArea[15] = 32; 79 } 80 for (int i = 81 0; i <= 15; i++) { 82 runnerArea[i] = runnerArea[i + 1]; 83 } 84 if 85 (freeze_score == 0) { 86 score++; 87 } 88 } 89 drawBarrier(); 90 91 92 if (read_LCD_buttons() == btnSELECT) { 93 // runnerArea[1] = 32; 94 if 95 (runnerArea[1] != 32 && (runnerArea[1] != 1 || runnerArea[1] != 2)) { 96 runnerArea[1] 97 = 32; 98 } 99 jump = 0; 100 freeze_score = 1; 101 jumpTime = millis(); 102 103 } 104 if (millis() - jumpTime >= jumpLength) { 105 if (checkSafe) { 106 runnerArea[1] 107 = 0; 108 jump = 32; 109 freeze_score = 0; 110 } else { 111 showCrashScreen(); 112 113 } 114 } 115 updateLcd(); 116 printScore(); 117 118 if (millis() - previousMillisLED 119 >= 500) { 120 previousMillisLED = currentMillis; 121 digitalWrite(13, !digitalRead(13)); 122 123 } 124}
bitmaps.h
h
Upload as a seperate tab named bitmaps.h
1// Bitmaps-Do not modify 2 3byte dino[8] = { 4 0b00000, 5 0b00111, 6 7 0b00111, 8 0b10110, 9 0b11111, 10 0b01010, 11 0b01010, 12 0b00000 13}; 14 15byte 16 cacti[8] = { 17 0b00100, 18 0b00101, 19 0b10101, 20 0b10101, 21 0b10111, 22 23 0b11100, 24 0b00100, 25 0b00000 26}; 27 28byte bird[8] = { 29 0b00000, 30 31 0b00100, 32 0b01100, 33 0b11110, 34 0b00111, 35 0b00110, 36 0b00100, 37 38 0b00000 39}; 40 41byte block[8] = { 42 0b11111, 43 0b11111, 44 0b11111, 45 46 0b11111, 47 0b11111, 48 0b11111, 49 0b11111, 50 0b11111 51}; 52 53 54
Functions
c_cpp
Upload as a tab named Functions
1// Functions for main.cpp 2 3int read_LCD_buttons() { 4 adc_key_in = analogRead(0); 5 6 if (adc_key_in > 1000) return btnNONE; 7 8 if (adc_key_in < 10) return btnRIGHT; 9 if (adc_key_in < 110) return btnUP; 10 if (adc_key_in < 300) return btnDOWN; 11 if (adc_key_in < 450) return btnLEFT; 12 if (adc_key_in < 700) return btnSELECT; 13 14 return btnNONE; 15} 16 17void updateLcd() { 18 for (int i = 0; i <= 15; i++) { 19 lcd.setCursor(i, 1); 20 lcd.write(runnerArea[i]); 21 } 22 lcd.setCursor(1, 0); 23 lcd.write(jump); 24} 25 26void drawBarrier() { 27 runnerArea[0] = 3; 28 runnerArea[15] = 3; 29} 30 31void printScore() { 32 lcd.setCursor(4, 0); 33 lcd.print("Score: "); 34 lcd.setCursor(11, 0); 35 lcd.print(score); 36} 37 38void showSplashScreen(int delayTime, boolean wait_for_start) { 39 lcd.home(); 40 lcd.print("Chrome Dino Game"); 41 drawSplashGraphics(); 42 delay(delayTime); 43 if (wait_for_start) { 44 while (read_LCD_buttons() != btnSELECT) { 45 checkForEEPROMUserInitErase(); 46 } 47 } 48 lcd.clear(); 49} 50 51boolean checkForEEPROMUserInitErase() { 52 if (read_LCD_buttons() == btnDOWN) { 53 lcd.clear(); 54 lcd.home(); 55 lcd.print("Enter Executive"); 56 lcd.setCursor(0, 1); 57 lcd.print("Code: "); 58 if (EnterPassword() == 0) { 59 lcd.clear(); 60 lcd.home(); 61 lcd.print("Chrome Dino Game"); 62 drawSplashGraphics(); 63 return 0; 64 } 65 lcd.home(); 66 lcd.print("Clearing EEPROM"); 67 EEPROMWriteInt(0, 0); 68 delay(250); 69 lcd.home(); 70 lcd.print(" "); 71 lcd.home(); 72 lcd.print("Done!"); 73 digitalWrite(13, !digitalRead(13)); 74 delay(100); 75 digitalWrite(13, !digitalRead(13)); 76 delay(400); 77 lcd.setCursor(0, 1); 78 lcd.print(" "); 79 lcd.home(); 80 lcd.print("Chrome Dino Game"); 81 drawSplashGraphics(); 82 } 83 return 1; 84} 85 86bool leave = 0; 87byte code = 0; 88 89boolean EnterPassword() { 90 lcd.setCursor(0, 1); 91 lcd.print("Code: "); 92 while (leave == 0) { 93 lcd.setCursor(6, 1); 94 lcd.print(" "); 95 lcd.setCursor(6, 1); 96 lcd.print(code); 97 if (read_LCD_buttons() == btnUP) { 98 code++; 99 delay(50); 100 } else if (read_LCD_buttons() == btnDOWN) { 101 code--; 102 delay(50); 103 } 104 if (read_LCD_buttons() == btnSELECT) { 105 if (code == correct_code) { 106 leave = 1; 107 } else { 108 wrong(); 109 } 110 } 111 delay(50); 112 } 113 delay(250); 114 lcd.home(); 115 lcd.clear(); 116 lcd.print("Access granted!"); 117 delay(500); 118 lcd.clear(); 119 return 1; 120} 121 122void wrong() { 123 lcd.setCursor(0, 1); 124 lcd.print("Incorrect!"); 125 code = 0; 126 delay(250); 127 lcd.setCursor(0, 1); 128 lcd.print("Code: "); 129} 130 131void drawRandChar() { 132 lcd.setCursor(random(3, 15), 1); 133 lcd.write(byte(random(1, 3))); 134} 135 136void drawSplashGraphics() { 137 lcd.setCursor(0, 1); 138 lcd.write(byte(3)); 139 lcd.setCursor(15, 1); 140 lcd.write(byte(3)); 141 lcd.setCursor(1, 1); 142 lcd.write(byte(0)); 143 drawRandChar(); 144 drawRandChar(); 145 drawRandChar(); 146} 147 148void showCrashScreen() { 149 lcd.setCursor(4, 1); 150 lcd.print("Game Over!"); 151 delay(2500); 152 lcd.setCursor(4, 1); 153 lcd.print("Best: "); 154 lcd.setCursor(10, 1); 155 lcd.print(" "); 156 lcd.setCursor(10, 1); 157 if (EEPROMReadInt(0) <= score) { 158 EEPROMWriteInt(0, score); 159 } 160 lcd.print(EEPROMReadInt(0)); 161 while (true) { 162 digitalWrite(13, !digitalRead(13)); 163 delay(500); 164 } 165} 166 167void EEPROMWriteInt(int address, int value) 168{ 169 byte two = (value & 0xFF); 170 byte one = ((value >> 8) & 0xFF); 171 172 EEPROM.update(address, two); 173 EEPROM.update(address + 1, one); 174} 175 176int EEPROMReadInt(int address) 177{ 178 long two = EEPROM.read(address); 179 long one = EEPROM.read(address + 1); 180 181 return ((two << 0) & 0xFFFFFF) + ((one << 8) & 0xFFFFFFFF); 182}
Chrome_Dino_Game_on_LCD
c_cpp
Paste into editor as main sketch
1#include <LiquidCrystal.h> 2#include <EEPROM.h> 3 4#include "bitmaps.h" 5 6LiquidCrystal lcd(8, 9, 4, 5, 6, 7); 7 8int adc_key_in = 0; 9 10#define btnRIGHT 0 11#define btnUP 1 12#define btnDOWN 2 13#define btnLEFT 3 14#define btnSELECT 4 15#define btnNONE 5 16 17byte runnerArea[16] {32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32}; 18byte jump = 32; 19 20int score = 0; 21bool freeze_score = 0; 22 23byte correct_code = 123; 24 25unsigned long previousMillis = 0; 26unsigned long previousMillisLED = 0; 27unsigned long jumpTime = 0; 28const int jumpLength = 500; 29 30#define checkSafe runnerArea[1] == 32 || runnerArea[1] == 0 31 32const byte chance_of_ob = 15; 33int speedOfScroller = 300; 34 35void setup() { 36 lcd.begin(16, 2); 37 Serial.begin(9600); 38 39 lcd.createChar(0, dino); 40 lcd.createChar(1, cacti); 41 lcd.createChar(2, bird); 42 lcd.createChar(3, block); 43 pinMode(A0, INPUT); 44 pinMode(A1, INPUT); 45 pinMode(13, OUTPUT); 46 randomSeed(A1); 47 lcd.clear(); 48 showSplashScreen(1000, true); 49} 50 51void loop() { 52 unsigned long currentMillis = millis(); 53 unsigned long currentMillisOb = millis(); 54 if (currentMillisOb - previousMillis >= speedOfScroller) { 55 previousMillis = currentMillisOb; 56 if (random(chance_of_ob) == 0) { 57 runnerArea[15] = 1; 58 } else if (random(chance_of_ob) == 1) { 59 runnerArea[15] = 2; 60 } else { 61 runnerArea[15] = 32; 62 } 63 for (int i = 0; i <= 15; i++) { 64 runnerArea[i] = runnerArea[i + 1]; 65 } 66 if (freeze_score == 0) { 67 score++; 68 } 69 } 70 drawBarrier(); 71 72 if (read_LCD_buttons() == btnSELECT) { 73 // runnerArea[1] = 32; 74 if (runnerArea[1] != 32 && (runnerArea[1] != 1 || runnerArea[1] != 2)) { 75 runnerArea[1] = 32; 76 } 77 jump = 0; 78 freeze_score = 1; 79 jumpTime = millis(); 80 } 81 if (millis() - jumpTime >= jumpLength) { 82 if (checkSafe) { 83 runnerArea[1] = 0; 84 jump = 32; 85 freeze_score = 0; 86 } else { 87 showCrashScreen(); 88 } 89 } 90 updateLcd(); 91 printScore(); 92 93 if (millis() - previousMillisLED >= 500) { 94 previousMillisLED = currentMillis; 95 digitalWrite(13, !digitalRead(13)); 96 } 97}
Downloadable files
Schematic
It is a bare Arduino. Pretend there is a LCD Shield attached to the Arduino
Schematic
Schematic
It is a bare Arduino. Pretend there is a LCD Shield attached to the Arduino
Schematic
Comments
Only logged in users can leave comments