Components and supplies
Single Turn Potentiometer- 10k ohms
Analog joystick (Generic)
Arduino UNO
Jumper wires (generic)
Alphanumeric LCD, 16 x 2
Resistor 330 ohm
Pushbutton Switch, Pushbutton
Tools and machines
Hot glue gun (generic)
Apps and platforms
Arduino IDE
Project description
Code
Code
arduino
1// https://www.youtube.com/channel/UCaXI2PcsTlH5g0et67kdD6g // 2// Space Impact Rocket Game LCD // 3// By MOHD SOHAIL // 4 5#include <LiquidCrystal.h> 6LiquidCrystal lcd(2, 3, 8, 9, 10, 11);// RS,E,D4,D5,D6,D7 7 8uint8_t area[4][15] = { 9 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 10 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 11 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 12 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 13}; 14 15const byte pinBuzzer = 12; 16const byte pinFire = 5; 17const int pinX = 0; // joystick x an analog-pin 0 18const int pinY = 1; // joystick y an analog-pin 1 19 20void setup() 21{ 22 lcd.begin(16,2); 23 byte a[8] = {B11100, B01111, B11100, B00000, B00000, B00000, B00000, B00000,}; 24 lcd.createChar(0, a); 25 byte b[8] = {B00000, B00000, B00000, B00000, B11100, B01111, B11100, B00000,}; 26 lcd.createChar(1, b); 27 byte c[8] = {B11100, B01111, B11100, B00000, B11100, B10100, B11100, B00000,}; 28 lcd.createChar(2, c); 29 byte d[8] = {B11100, B10100, B11100, B00000, B11100, B01111, B11100, B00000,}; 30 lcd.createChar(3, d); 31 byte e[8] = {B11100, B10100, B11100, B00000, B11100, B10100, B11100, B00000,}; 32 lcd.createChar(4, e); 33 byte f[8] = {B00000, B00000, B00000, B00000, B00100, B10010, B01000, B00000,}; 34 lcd.createChar(5, f); 35 byte g[8] = {B00100, B10010, B01000, B00000, B11100, B10100, B11100, B00000,}; 36 lcd.createChar(6, g); 37 byte h[8] = {B11100, B10100, B11100, B00000, B00100, B10010, B01000, B00000,}; 38 lcd.createChar(7, h); 39 lcd.home (); 40 pinMode(pinBuzzer,OUTPUT); 41 pinMode(pinFire, INPUT_PULLUP); 42} 43 44void loop() 45{ 46 lcd.clear(); 47 for (uint8_t y=0; y<2; y++) { 48 for (uint8_t x=0; x<16; x++) { 49 lcd.setCursor(x, y); 50 lcd.print(char(1)); 51 delay(100); 52 lcd.setCursor(x, y); 53 if (y==0) { 54 lcd.print(" SPACE "[x]); 55 } else { 56 lcd.print(" IMPACT "[x]); 57 } 58 } 59 } 60 delay(1000); 61 lcd.clear(); 62 lcd.setCursor(0,0); 63 lcd.print("3 -> LIFE POINTS"); 64 lcd.setCursor(0,1); 65 lcd.print("9 -> WEAPON LOAD"); 66 delay(3000); 67 lcd.clear(); 68 lcd.setCursor(0,0); 69 lcd.print(" PRESS BUTTON "); 70 lcd.setCursor(0,1); 71 lcd.print(" TO START "); 72 while(digitalRead(pinFire)) { 73 } 74 lcd.clear(); 75 delay(500); 76 77 for (uint8_t y=0; y<4; y++) { 78 for (uint8_t x=0; x<15; x++) { 79 area[y][x] = 0; 80 } 81 } 82 area[0][0] = 1; 83 uint8_t sleep = 400; 84 uint8_t junkRisk = 10; 85 uint8_t fireLoad = 0; 86 uint8_t fireConsumption = 9; 87 unsigned long score = 0; 88 uint8_t life = 3; 89 unsigned long count = 0; 90 91 lcd.setCursor(0,0); 92 lcd.print(life); 93 lcd.setCursor(0,1); 94 lcd.print(fireLoad); 95 96 draw(); 97 bool gameover = false; 98 99 while (!gameover) { 100 101 for (uint8_t y=0; y<4; y++) { 102 for (uint8_t x=15; x>0; x--) { 103 if (area[y][x-1]==4) { 104 area[y][x-1] = 0; 105 if (x<15) { 106 area[y][x] += 4; 107 } 108 } 109 } 110 } 111 112 if (!digitalRead(pinFire) && fireLoad>=fireConsumption) { 113 fireLoad-=fireConsumption; 114 for (uint8_t y=0; y<4; y++) { 115 for (uint8_t x=0; x<14; x++) { 116 if (area[y][x]==1) { // raumschiff 117 area[y][x+1] += 4; 118 } 119 } 120 } 121 } 122 if (fireLoad<9) { 123 fireLoad++; 124 lcd.setCursor(0,1); 125 lcd.print(fireLoad); 126 } 127 128 draw(); 129 delay(sleep); 130 131 bool doBreak = false; 132 for (uint8_t y=0; y<4; y++) { 133 for (uint8_t x=0; x<15; x++) { 134 if (area[y][x]==1) { 135 doBreak = true; 136 int dx = analogRead(pinX); 137 int dy = analogRead(pinY); 138 if(dy>534 && x>0) { 139 area[y][x] = 0; 140 area[y][x-1] += 1; 141 } else if((dy<490) && x<14) { 142 if (area[y][x+1]!=4) { 143 area[y][x] = 0; 144 area[y][x+1] += 1; 145 } 146 } else if(dx<490 && y>0) { 147 area[y][x] = 0; 148 area[y-1][x] += 1; 149 } else if(dx>534 && y<3) { 150 area[y][x] = 0; 151 area[y+1][x] += 1; 152 } 153 } 154 if (doBreak) { 155 break; 156 } 157 } 158 if (doBreak) { 159 break; 160 } 161 } 162 163 for (uint8_t y=0; y<4; y++) { 164 for (uint8_t x=0; x<15; x++) { 165 if (area[y][x]==2) { 166 area[y][x] = 0; 167 if (x>0) { 168 area[y][x-1] += 2; 169 } 170 } 171 } 172 } 173 174 for (uint8_t y=0; y<4; y++) { 175 if (random(100) < junkRisk) { 176 area[y][14] += 2; 177 } 178 } 179 180 draw(); 181 delay(sleep); 182 183 for (uint8_t y=0; y<4; y++) { 184 for (uint8_t x=0; x<15; x++) { 185 if (area[y][x]==3) { // kollidiertes schiff 186 area[y][x] = 1; 187 blinkShip(); 188 life--; 189 lcd.setCursor(0, 0); 190 lcd.print(life); 191 if(life==0) { 192 gameover = true; 193 lcd.clear(); 194 lcd.setCursor(6, 0); 195 lcd.print("GAME"); 196 lcd.setCursor(6, 1); 197 lcd.print("OVER"); 198 for (uint8_t y=0; y<2; y++) { 199 for (uint8_t x=0; x<16; x++) { 200 lcd.setCursor(x, y); 201 lcd.print(char(1)); 202 delay(100); 203 lcd.setCursor(x, y); 204 lcd.print(" "); 205 } 206 } 207 lcd.clear(); 208 lcd.setCursor(0, 0); 209 lcd.print("SCORE:"); 210 lcd.setCursor(0, 1); 211 lcd.print(score); 212 delay(1000); 213 for (uint8_t y=0; y<2; y++) { 214 for (uint8_t x=0; x<16; x++) { 215 lcd.setCursor(x, y); 216 lcd.print(char(1)); 217 delay(100); 218 lcd.setCursor(x, y); 219 lcd.print(" "); 220 } 221 } 222 delay(1000); 223 } 224 } else if (area[y][x]>4) { 225 for (uint8_t i=0; i<10; i++) { 226 digitalWrite(pinBuzzer,HIGH); 227 delay(3); 228 digitalWrite(pinBuzzer,LOW); 229 delay(3); 230 } 231 score+=10; 232 area[y][x] -= 6; 233 } 234 } 235 } 236 237 score++; 238 count++; 239 240 if (count%100==0){ 241 sleep -=5; 242 junkRisk +=3; 243 fireConsumption--; 244 } 245 } 246} 247 248void draw() { 249 for (uint8_t y=0; y<4; y+=2) { 250 for (uint8_t x=0; x<15; x++) { 251 lcd.setCursor(x+1, y/2); 252 253 if (area[y][x]==1) { 254 if (area[y+1][x]==0) { 255 lcd.print(char(0)); 256 } else if (area[y+1][x]==2 || area[y+1][x]==6 || area[y+1][x]==10) { // unten hindernis 257 lcd.print(char(2)); 258 } 259 } else if (area[y][x]==2 || area[y][x]==6 || area[y][x]==10) { 260 if (area[y+1][x]==0) { 261 lcd.write(0b11011111); 262 } else if (area[y+1][x]==1) { 263 lcd.print(char(3)); 264 } else if (area[y+1][x]==2) { 265 lcd.print(char(4)); 266 } else if (area[y+1][x]==4 || area[y+1][x]==6 || area[y+1][x]==10) { 267 lcd.print(char(7)); 268 } 269 } else if (area[y][x]==4) { 270 if (area[y+1][x]==0) { 271 lcd.write(0b11011110); 272 } else if (area[y+1][x]==2 || area[y+1][x]==6 || area[y+1][x]==10) { 273 lcd.print(char(6)); 274 } 275 } else if (area[y][x]==0) { // oben nichts 276 if (area[y+1][x]==0) { // unten nichts 277 lcd.print(" "); 278 } else if (area[y+1][x]==1) { // unten schiff 279 lcd.print(char(1)); 280 } else if (area[y+1][x]==2 || area[y+1][x]==6 || area[y+1][x]==10) { 281 lcd.write(0b10100001); 282 } else if (area[y+1][x]==4) { 283 lcd.print(char(5)); 284 } 285 } else { 286 lcd.print(" "); 287 } 288 } 289 } 290} 291 292void blinkShip() { 293 for (uint8_t y=0; y<4; y++) { 294 for (uint8_t x=0; x<15; x++) { 295 if (area[y][x]==1) { 296 297 for (uint8_t i=0; i<3; i++) { 298 lcd.setCursor(x+1, y>1); 299 if (y==0 || y==2) { 300 if (area[y+1][x]==0) { 301 lcd.print(" "); 302 } else { 303 lcd.write(0b10100001); 304 } 305 } else { 306 if (area[y-1][x]==0) { 307 lcd.print(" "); 308 } else { 309 lcd.write(0b11011111); 310 } 311 } 312 for (uint8_t i=0; i<10; i++) { 313 digitalWrite(pinBuzzer,HIGH); 314 delay(25); 315 digitalWrite(pinBuzzer,LOW); 316 delay(5); 317 } 318 319 lcd.setCursor(x+1, y>1); 320 if (y==0 || y==2) { 321 if (area[y+1][x]==0) { 322 lcd.print(char(0)); 323 } else { 324 lcd.print(char(2)); 325 } 326 } else { 327 if (area[y-1][x]==0) { 328 lcd.print(char(1)); 329 } else { 330 lcd.print(char(3)); 331 } 332 } 333 for (uint8_t i=0; i<10; i++) { 334 digitalWrite(pinBuzzer,HIGH); 335 delay(25); 336 digitalWrite(pinBuzzer,LOW); 337 delay(5); 338 } 339 } 340 } 341 } 342 } 343} 344
Downloadable files
Circuit Diagram
Circuit Diagram
Circuit Diagram
Circuit Diagram
Comments
Only logged in users can leave comments
Anonymous user
3 years ago
Hi Mohd. Great project 👍👍👍. Here is my variant of it - https://www.hackster.io/john-bradnam/tiny-space-impact-game-ab1d77. Keep up the good work 😃