Components and supplies
1
I2C 16x2 Arduino LCD Display Module
1
Arduino UNO
1
Analog joystick (Generic)
1
Solderless Breadboard Full Size
1
USB-A to Mini-USB Cable
Apps and platforms
1
Arduino Web Editor
1
Arduino IDE
Project description
Code
sketch.ino
arduino
1 2#include <Wire.h> 3#include <VirtualWire.h> 4#include <LiquidCrystal_I2C.h> 5LiquidCrystal_I2C lcd(0x27,16,2); 6 7// VARIABLES DECLARATION 8int game = 0; 9int gameover = 0; 10int le = 0; 11float ll = 0; 12int key = LOW; 13int mov=0; 14int cc=1; 15int screen1[202]; 16int screen2[202]; 17int ind = 0; 18int randomNumber=0; 19int ax=0; 20int ay=0; 21int timegame=2000; 22int clef = 0; 23int jump = 0; 24int path = 42; 25int ck = 2; 26 27// CHARS DECLARATION 28byte man1[] = {0x04, 0x0E, 0x0E, 0x0E, 0x04, 0x04, 0x1F, 0x1F} ; 29byte man2[] = {0x04, 0x1E, 0x05, 0x04, 0x0B, 0x10, 0x1F, 0x1F} ; 30byte man3[] = {0x04, 0x0F, 0x14, 0x04, 0x1A, 0x01, 0x1F, 0x1F} ; 31byte man4[] = {0x15, 0x0E, 0x04, 0x0E, 0x11, 0x00, 0x00, 0x00} ; 32byte man5[] = {0x15, 0x0E, 0x04, 0x0E, 0x11, 0x00, 0x1F, 0x1F} ; 33byte man6[] = {0x00, 0x00, 0x04, 0x0E, 0x04, 0x04, 0x1F, 0x1F} ; 34byte flat[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1F, 0x1F}; 35byte hole[] = {0x0E, 0x1F, 0x15, 0x1B, 0x0E, 0x04, 0x1F, 0x1F} ; 36byte m1[] = {0x1F, 0x1F, 0x1F, 0x19, 0x10, 0x00, 0x00, 0x00} ; 37byte m2[] = {0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x0F, 0x06, 0x00} ; 38byte m3[] = {0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x0C, 0x00, 0x00} ; 39byte m4[] = {0x1F, 0x1F, 0x00, 0x0A, 0x00, 0x0A, 0x00, 0x0A} ; 40byte m5[] = {0x1F, 0x1F, 0x1F, 0x00, 0x0A, 0x00, 0x0A, 0x00} ; 41byte m6[] = {0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x00, 0x0A, 0x00} ; 42byte keys[8] = {0x00, 0x03, 0x1F, 0x13, 0x00, 0x00, 0x1F, 0x1F} ; 43byte dooro[8] = {0x1F, 0x11, 0x11, 0x11, 0x11, 0x11, 0x1F, 0x1F} ; 44byte door[8] = {0x1F, 0x1F, 0x1D, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F} ; 45 46 47 48void setup(){ 49 lcd.init(); 50 lcd.backlight(); 51 lcd.home(); 52 lcd.clear(); 53// CHARACTER ASSOCIATION 54 lcd.createChar(1, man1); 55 lcd.createChar(2, flat); 56 lcd.createChar(3, hole); 57 lcd.createChar(8, keys); 58 lcd.createChar(7, door); 59} 60 61 62void loop(){ 63 if (game == 0) 64 { 65 if (gameover == 0) 66 { 67 // TITLE SCREEN SCREEN 68 lcd.home(); 69 lcd.setCursor(0, 0); 70 lcd.print(" key game"); 71 lcd.setCursor(0, 1); 72 lcd.print(" 2020"); 73 key = digitalRead(7); 74 if (key == LOW) 75 { 76 // INIT GAME 77 lcd.clear(); 78 lcd.home(); 79 le = 0; 80 ll = 0; 81 gameover = 1; 82 cc=2; 83 jump = 0; 84 path = 16; 85 } 86 } 87 else 88 { 89 // NEW SCREEN - START 90 randomSeed(analogRead(0)); 91 game = 1; 92 key = LOW; 93 le = le + 1; 94 ll = ll + 1; 95 path = path + 10; 96 if (path>202) 97 { 98 path = 202; 99 } 100 ind = 0; 101 for(int s=0;s<(path + 10);s++) 102 { 103 screen1[s] = 2; 104 } 105 106 for(int s=11;s<(path -2 );s=s+3) 107 { 108 randomNumber = int(random(2, 4)); 109 //randomNumber = 3; 110 screen1[s] = randomNumber; 111 } 112 113 screen1[6]=7; 114 screen1[path + 1]=8; 115 116 for(int s=0;s<(path+10);s++) 117 { 118 randomNumber = int(random(4, 7)); 119 screen2[s] = randomNumber; 120 } 121 122 if ((ll/2) == int(ll/2)) 123 { 124 lcd.createChar(4, m4); 125 lcd.createChar(5, m5); 126 lcd.createChar(6, m6); 127 } 128 else 129 { 130 lcd.createChar(4, m1); 131 lcd.createChar(5, m2); 132 lcd.createChar(6, m3); 133 } 134 timegame = 2000; 135 clef = 0; 136 lcd.createChar(7, door); 137 cc = 2; 138 ck = 2; 139 } 140 } 141 else 142 { 143 144 // GAME LOOP 145 if (jump == 3) 146 { 147 jump = 0; 148 } 149 150 lcd.home(); 151 timegame = timegame - le; 152 for(int s=ind;s<(ind+16);s++) 153 { 154 lcd.setCursor(s-ind, 0); 155 lcd.write(byte(screen2[s])); 156 lcd.setCursor(s-ind, 1); 157 lcd.write(byte(screen1[s])); 158 } 159 160 if (timegame < 0) 161 { 162 timegame = 0; 163 } 164 lcd.setCursor(16-String(timegame).length(), 0); 165 lcd.print(timegame); 166 167if (jump == 0) 168{ 169 lcd.setCursor(7, 1); 170 lcd.write(byte(1)); 171 ck = screen1[ind+7]; 172 if (mov == 1) 173 { 174 if (cc==1) 175 { 176 lcd.createChar(1, man1); 177 } 178 else 179 { 180 lcd.createChar(1, man1); 181 } 182 mov = 0; 183 } 184 else 185 { 186 lcd.createChar(1, man1); 187 mov = 1; 188 } 189} 190 191if (jump > 0) 192{ 193 lcd.setCursor(7, 0); 194 lcd.write(byte(1)); 195 lcd.createChar(1, man4); 196 if (cc == 2) 197 { 198 ind = ind + 1; 199 } 200 else 201 { 202 ind = ind - 1; 203 } 204 if (jump == 1) 205 { 206 jump = 2; 207 } 208 else 209 { 210 jump = 3; 211 } 212} 213 // CHECK LIVE OR DIE - LOOP GAME OR GAME OVER 214 if ((ck == 2)&&(timegame>0)) // SET THE VARIABLE TO CHECK 215 { 216 ay = analogRead(1); 217 ax = analogRead(0); 218 if ((ax > 600) && (jump == 0)) 219 { 220 if (ind<(path-7)) 221 { 222 ind = ind + 1; 223 cc = 2; 224 } 225 else 226 { 227 screen1[path + 1]=2; 228 clef = 1; 229 lcd.createChar(7, dooro); 230 } 231 } 232 233 if ((ax < 200) && (jump == 0)) 234 { 235 if (ind>0) 236 { 237 ind = ind - 1; 238 cc = 1; 239 } 240 else 241 { 242 if (clef==1) 243 { 244 // CHECK LEVEL CLEAR - START 245 lcd.setCursor(0, 1); 246 lcd.print(" "); 247 lcd.setCursor((int(10-String(le).length())/2),1); 248 lcd.print("LEVEL "); 249 lcd.print(le+1); 250 delay(4000); 251 lcd.clear(); 252 game = 0; 253 gameover = 1; 254 255// CHECK LEVEL CLEAR - END 256 257 } 258 } 259 } 260 261 262 if ((ay < 200) && (ind >1) && (ind <(path - 8)) && (jump == 0)) 263 { 264 jump = 1; 265 } 266 267 268 269 270 delay(200); 271 272 273// GAME LOOP - END 274 275 } 276 else 277 { 278 279// GAME OVER - START 280 if (timegame>0) 281 { 282 lcd.setCursor(0, 0); 283 lcd.print(" GAME OVER :( "); 284 lcd.setCursor(7, 1); 285 lcd.write(byte(1)); 286 lcd.createChar(1, man6); 287 } 288 else 289 { 290 lcd.setCursor(0, 1); 291 lcd.print(" TIME OVER "); 292 } 293 delay(4000); 294 lcd.clear(); 295 game = 0; 296 gameover =0; 297 298// GAME OVER - END 299 300 } 301 } 302} 303
Downloadable files
Diagram
Diagram

Diagram
Diagram

Comments
Only logged in users can leave comments