LCD Bow and arrow game
A cool game where you are an arrow.
Components and supplies
2
Pushbutton switch 12mm
2
Resistor 1k ohm
1
Breadboard (generic)
1
Arduino UNO
1
Standard LCD - 16x2 White on Blue
1
Rotary potentiometer (generic)
21
Jumper wires (generic)
Project description
Code
LCD BOW AND ARROW GAME
c_cpp
1#include <LiquidCrystal.h> 2 3const int buttonPin1 = 6; 4const int buttonPin2 = 1; 5int button1state = 0; 6int button2state = 0; 7int arrowRow = 0; 8int newRow = 0; 9int sameRowCounter = 0; 10 11LiquidCrystal lcd(12, 11, 5, 4, 3, 2); 12byte weakblock[] = { 13 B01100, 14 B01100, 15 B01100, 16 B01100, 17 B01100, 18 B01100, 19 B01100, 20 B01100 21}; 22byte strongblock[] = { 23 B00000, 24 B00000, 25 B11111, 26 B11111, 27 B11111, 28 B11111, 29 B11111, 30 B00000 31}; 32byte bow[] = { 33 B00000, 34 B00110, 35 B01110, 36 B10010, 37 B11111, 38 B10010, 39 B01110, 40 B00110 41}; 42byte bow2[] = { 43 B00100, 44 B00110, 45 B00110, 46 B00101, 47 B00101, 48 B00110, 49 B00110, 50 B00100 51}; 52byte arrow[] = { 53 B00000, 54 B00000, 55 B00100, 56 B00010, 57 B11111, 58 B00010, 59 B00100, 60 B00000 61}; 62byte superarrow[] = { 63 B00000, 64 B01000, 65 B00100, 66 B00110, 67 B11111, 68 B00110, 69 B00100, 70 B01000 71}; 72 73struct OneBlock 74{ 75 int row; 76 int col; 77 int type; 78}; 79 80OneBlock blocks[15]; 81int counter; 82int score; 83int scoreCol; 84unsigned long prevScoreMillis = 0; 85unsigned long prevBlockMillis = 0; 86unsigned long prevArrowMillis = 0; 87unsigned long prevSuperMillis = 0; 88bool gameOver = false; 89bool superArrow = false; 90 91void setup() 92{ 93 pinMode(buttonPin1, INPUT); 94 pinMode(buttonPin2, INPUT); 95 lcd.begin(16, 2); 96 lcd.createChar(0, weakblock); 97 lcd.createChar(1, strongblock); 98 lcd.createChar(2, bow); 99 lcd.createChar(3, bow2); 100 lcd.createChar(4, arrow); 101 lcd.createChar(5, superarrow); 102 randomSeed(analogRead(0)); 103 arrowRow = 0; 104 superArrow = false; 105 score = 0; 106 counter = 0; 107 blocks[0].row = random(0, 2); 108 blocks[0].col = 15; 109 for (int i = 0; i < 15; ++i) 110 blocks[i].row = -1; 111} 112 113void EndGame() 114{ 115 lcd.clear(); 116 lcd.setCursor(3, 0); 117 lcd.print("Game Over"); 118 lcd.setCursor(3, 1); 119 lcd.print("Score:"); 120 lcd.setCursor(9, 1); 121 lcd.print(score); 122 gameOver = true; 123} 124 125void IncrementScore() 126{ 127 score++; 128 if (score > 9999) 129 scoreCol = 11; 130 else if (score > 999) 131 scoreCol = 12; 132 else if (score > 99) 133 scoreCol = 13; 134 else if (score > 9) 135 scoreCol = 14; 136 else 137 scoreCol = 15; 138} 139 140void loop() 141{ 142 if (gameOver) 143 { 144 button1state = digitalRead(buttonPin1); 145 button2state = digitalRead(buttonPin2); 146 if (button1state == HIGH || button2state == HIGH) 147 { 148 gameOver = false; 149 delay(1000); 150 setup(); 151 } 152 else 153 return; 154 } 155 156 unsigned long currentMillis = millis(); 157 button1state = digitalRead(buttonPin1); 158 button2state = digitalRead(buttonPin2); 159 160 if (currentMillis - prevArrowMillis >= 450) 161 { 162 if (button1state == HIGH && !superArrow) 163 { 164 lcd.setCursor(1, arrowRow); 165 lcd.print(" "); 166 if (arrowRow == 0) 167 { 168 arrowRow = 1; 169 prevArrowMillis = currentMillis; 170 } 171 else if (arrowRow == 1) 172 { 173 arrowRow = 0; 174 prevArrowMillis = currentMillis; 175 } 176 } 177 } 178 179 if (currentMillis - prevSuperMillis >= 450) 180 { 181 if (button2state == HIGH) 182 { 183 superArrow = !superArrow; 184 prevSuperMillis = currentMillis; 185 } 186 } 187 188 if (currentMillis - prevScoreMillis >= 1000) 189 { 190 prevScoreMillis = currentMillis; 191 IncrementScore(); 192 } 193 194 bool redraw = false; 195 if (score > 9999 && currentMillis - prevBlockMillis >= 1) 196 { 197 prevBlockMillis = currentMillis; 198 redraw = true; 199 } 200 else if (score > 500 && currentMillis - prevBlockMillis >= 200) 201 { 202 prevBlockMillis = currentMillis; 203 redraw = true; 204 } 205 else if (score > 99 && currentMillis - prevBlockMillis >= 350) 206 { 207 prevBlockMillis = currentMillis; 208 redraw = true; 209 } 210 else if (score > 9 && currentMillis - prevBlockMillis >= 450) 211 { 212 prevBlockMillis = currentMillis; 213 redraw = true; 214 } 215 else if (currentMillis - prevBlockMillis >= 500) 216 { 217 prevBlockMillis = currentMillis; 218 redraw = true; 219 } 220 221 if (score > 0) 222 { 223 lcd.setCursor(scoreCol, 0); 224 lcd.print(score); 225 } 226 227 if (score >= 5) 228 { 229 lcd.setCursor(1, arrowRow); 230 if (superArrow) 231 lcd.write(byte(5)); 232 else 233 lcd.write(byte(4)); 234 } 235 236 if (score < 5) 237 { 238 lcd.setCursor(0, 0); 239 lcd.write(byte(2)); 240 } 241 242 if (score == 10) 243 { 244 lcd.setCursor(0, 0); 245 lcd.print(" "); 246 } 247 248 if (score < 10 && score >= 5) 249 { 250 lcd.setCursor(0, 0); 251 lcd.write(byte(3)); 252 } 253 254 if (!redraw) 255 return; 256 257 lcd.clear(); 258 259 for (int i = 0; i < 15; ++i) 260 { 261 if (blocks[i].row != -1) 262 { 263 lcd.setCursor(blocks[i].col, blocks[i].row); 264 lcd.write(byte(blocks[i].type)); 265 blocks[i].col--; 266 if (blocks[i].col < 0) 267 { 268 if (arrowRow == blocks[i].row) 269 { 270 if (!superArrow || superArrow && blocks[i].type == 1) 271 { 272 EndGame(); 273 return; 274 } 275 } 276 blocks[i].row = -1; 277 } 278 279 if (superArrow && arrowRow == blocks[i].row && blocks[i].col == 1 && blocks[i].type == 0) 280 { 281 IncrementScore(); 282 blocks[i].row = -1; 283 } 284 } 285 } 286 if (sameRowCounter > 0) 287 { 288 counter++; 289 if (counter == 15) 290 counter = 0; 291 blocks[counter].row = newRow; 292 blocks[counter].col = 15; 293 blocks[counter].type = random(0, 2); 294 sameRowCounter--; 295 } 296 else 297 { 298 sameRowCounter = random(1, 5); 299 if (newRow == 1) 300 newRow = 0; 301 else 302 newRow = 1; 303 } 304}
LCD BOW AND ARROW GAME
c_cpp
1#include <LiquidCrystal.h> 2 3const int buttonPin1 = 6; 4const int 5 buttonPin2 = 1; 6int button1state = 0; 7int button2state = 0; 8int arrowRow 9 = 0; 10int newRow = 0; 11int sameRowCounter = 0; 12 13LiquidCrystal lcd(12, 14 11, 5, 4, 3, 2); 15byte weakblock[] = { 16 B01100, 17 B01100, 18 B01100, 19 20 B01100, 21 B01100, 22 B01100, 23 B01100, 24 B01100 25}; 26byte strongblock[] 27 = { 28 B00000, 29 B00000, 30 B11111, 31 B11111, 32 B11111, 33 B11111, 34 35 B11111, 36 B00000 37}; 38byte bow[] = { 39 B00000, 40 B00110, 41 B01110, 42 43 B10010, 44 B11111, 45 B10010, 46 B01110, 47 B00110 48}; 49byte bow2[] 50 = { 51 B00100, 52 B00110, 53 B00110, 54 B00101, 55 B00101, 56 B00110, 57 58 B00110, 59 B00100 60}; 61byte arrow[] = { 62 B00000, 63 B00000, 64 B00100, 65 66 B00010, 67 B11111, 68 B00010, 69 B00100, 70 B00000 71}; 72byte superarrow[] 73 = { 74 B00000, 75 B01000, 76 B00100, 77 B00110, 78 B11111, 79 B00110, 80 81 B00100, 82 B01000 83}; 84 85struct OneBlock 86{ 87 int row; 88 int col; 89 90 int type; 91}; 92 93OneBlock blocks[15]; 94int counter; 95int score; 96int 97 scoreCol; 98unsigned long prevScoreMillis = 0; 99unsigned long prevBlockMillis 100 = 0; 101unsigned long prevArrowMillis = 0; 102unsigned long prevSuperMillis = 0; 103bool 104 gameOver = false; 105bool superArrow = false; 106 107void setup() 108{ 109 pinMode(buttonPin1, 110 INPUT); 111 pinMode(buttonPin2, INPUT); 112 lcd.begin(16, 2); 113 lcd.createChar(0, 114 weakblock); 115 lcd.createChar(1, strongblock); 116 lcd.createChar(2, bow); 117 118 lcd.createChar(3, bow2); 119 lcd.createChar(4, arrow); 120 lcd.createChar(5, 121 superarrow); 122 randomSeed(analogRead(0)); 123 arrowRow = 0; 124 superArrow 125 = false; 126 score = 0; 127 counter = 0; 128 blocks[0].row = random(0, 2); 129 130 blocks[0].col = 15; 131 for (int i = 0; i < 15; ++i) 132 blocks[i].row = -1; 133} 134 135void 136 EndGame() 137{ 138 lcd.clear(); 139 lcd.setCursor(3, 0); 140 lcd.print("Game 141 Over"); 142 lcd.setCursor(3, 1); 143 lcd.print("Score:"); 144 lcd.setCursor(9, 145 1); 146 lcd.print(score); 147 gameOver = true; 148} 149 150void IncrementScore() 151{ 152 153 score++; 154 if (score > 9999) 155 scoreCol = 11; 156 else if (score > 999) 157 158 scoreCol = 12; 159 else if (score > 99) 160 scoreCol = 13; 161 else if 162 (score > 9) 163 scoreCol = 14; 164 else 165 scoreCol = 15; 166} 167 168void 169 loop() 170{ 171 if (gameOver) 172 { 173 button1state = digitalRead(buttonPin1); 174 175 button2state = digitalRead(buttonPin2); 176 if (button1state == HIGH || button2state 177 == HIGH) 178 { 179 gameOver = false; 180 delay(1000); 181 setup(); 182 183 } 184 else 185 return; 186 } 187 188 unsigned long currentMillis = 189 millis(); 190 button1state = digitalRead(buttonPin1); 191 button2state = digitalRead(buttonPin2); 192 193 194 if (currentMillis - prevArrowMillis >= 450) 195 { 196 if (button1state == 197 HIGH && !superArrow) 198 { 199 lcd.setCursor(1, arrowRow); 200 lcd.print(" 201 "); 202 if (arrowRow == 0) 203 { 204 arrowRow = 1; 205 prevArrowMillis 206 = currentMillis; 207 } 208 else if (arrowRow == 1) 209 { 210 arrowRow 211 = 0; 212 prevArrowMillis = currentMillis; 213 } 214 } 215 } 216 217 218 if (currentMillis - prevSuperMillis >= 450) 219 { 220 if (button2state == 221 HIGH) 222 { 223 superArrow = !superArrow; 224 prevSuperMillis = currentMillis; 225 226 } 227 } 228 229 if (currentMillis - prevScoreMillis >= 1000) 230 { 231 prevScoreMillis 232 = currentMillis; 233 IncrementScore(); 234 } 235 236 bool redraw = false; 237 238 if (score > 9999 && currentMillis - prevBlockMillis >= 1) 239 { 240 prevBlockMillis 241 = currentMillis; 242 redraw = true; 243 } 244 else if (score > 500 && currentMillis 245 - prevBlockMillis >= 200) 246 { 247 prevBlockMillis = currentMillis; 248 redraw 249 = true; 250 } 251 else if (score > 99 && currentMillis - prevBlockMillis >= 350) 252 253 { 254 prevBlockMillis = currentMillis; 255 redraw = true; 256 } 257 else 258 if (score > 9 && currentMillis - prevBlockMillis >= 450) 259 { 260 prevBlockMillis 261 = currentMillis; 262 redraw = true; 263 } 264 else if (currentMillis - prevBlockMillis 265 >= 500) 266 { 267 prevBlockMillis = currentMillis; 268 redraw = true; 269 270 } 271 272 if (score > 0) 273 { 274 lcd.setCursor(scoreCol, 0); 275 lcd.print(score); 276 277 } 278 279 if (score >= 5) 280 { 281 lcd.setCursor(1, arrowRow); 282 if 283 (superArrow) 284 lcd.write(byte(5)); 285 else 286 lcd.write(byte(4)); 287 288 } 289 290 if (score < 5) 291 { 292 lcd.setCursor(0, 0); 293 lcd.write(byte(2)); 294 295 } 296 297 if (score == 10) 298 { 299 lcd.setCursor(0, 0); 300 lcd.print(" 301 "); 302 } 303 304 if (score < 10 && score >= 5) 305 { 306 lcd.setCursor(0, 307 0); 308 lcd.write(byte(3)); 309 } 310 311 if (!redraw) 312 return; 313 314 315 lcd.clear(); 316 317 for (int i = 0; i < 15; ++i) 318 { 319 if (blocks[i].row 320 != -1) 321 { 322 lcd.setCursor(blocks[i].col, blocks[i].row); 323 lcd.write(byte(blocks[i].type)); 324 325 blocks[i].col--; 326 if (blocks[i].col < 0) 327 { 328 if 329 (arrowRow == blocks[i].row) 330 { 331 if (!superArrow || superArrow 332 && blocks[i].type == 1) 333 { 334 EndGame(); 335 return; 336 337 } 338 } 339 blocks[i].row = -1; 340 } 341 342 if 343 (superArrow && arrowRow == blocks[i].row && blocks[i].col == 1 && blocks[i].type 344 == 0) 345 { 346 IncrementScore(); 347 blocks[i].row = -1; 348 349 } 350 } 351 } 352 if (sameRowCounter > 0) 353 { 354 counter++; 355 356 if (counter == 15) 357 counter = 0; 358 blocks[counter].row = newRow; 359 360 blocks[counter].col = 15; 361 blocks[counter].type = random(0, 2); 362 sameRowCounter--; 363 364 } 365 else 366 { 367 sameRowCounter = random(1, 5); 368 if (newRow == 369 1) 370 newRow = 0; 371 else 372 newRow = 1; 373 } 374}
Downloadable files
LCD ARROW AND BOW GAME CIRCUIT DIAGRAM
LCD ARROW AND BOW GAME CIRCUIT DIAGRAM

LCD ARROW AND BOW GAME CIRCUIT DIAGRAM
LCD ARROW AND BOW GAME CIRCUIT DIAGRAM
LCD ARROW AND BOW GAME CIRCUIT DIAGRAM
LCD ARROW AND BOW GAME CIRCUIT DIAGRAM

Comments
Only logged in users can leave comments