Devices & Components
Set of 70 assorted color 5mm LEDs
Breadboard - 830 contacts
Box 525 1% precision resistors - 17 values
Arduino Uno Rev3
16x2 LCD display with I²C interface
Software & Tools
Arduino IDE
Project description
Code
GAME LOGIC
cpp
The code with the game logic here
1#include <LiquidCrystal.h> 2 3// LCD pins 4const int RS = 6; 5const int E = 7; 6const int DB4 = 8; 7const int DB5 = 9; 8const int DB6 = 10; 9const int DB7 = 11; 10LiquidCrystal lcd(RS, E, DB4, DB5, DB6, DB7); 11 12// BUTTONS 13const int BTN_1 = 2; // BUTTON 1 = PLAYER 1 14const int BTN_2 = 13; // BUTTON 2 = PLAYER 2 15 16// LED 17const int BLUE_LED = 3; 18const int RED_LED = 12; 19 20// DELAY 21const int LAG = 300; 22const int SEC = 1000; 23 24// GAME STATE 25unsigned long lastButtonSmash_1 = 0; // PLAYER 1 BUTTON SMASH 26unsigned long lastButtonSmash_2 = 0; // PLAYER 2 BUTTON SMASH 27 28unsigned long gameStartTimer; // START TIMER 29const long GAME_TIME = 15000; // 15s - GAME ROUND TIME - CHANGE TO LIMIT TIME 30 31bool gameStart = false; // HAS GAME BEGUN 32bool isPlaying = false; // ACTIVE GAME 33bool gameOver = false; // END GAME 34 35int playerScore_1 = 0; 36int playerScore_2 = 0; 37 38// ---------- SETUP 39void setup() { 40 //DEBUG 41 Serial.begin(9600); 42 43 // LED 44 pinMode(BLUE_LED, OUTPUT); 45 pinMode(RED_LED, OUTPUT); 46 47 // BUTTONS 48 pinMode(BTN_1, INPUT); 49 pinMode(BTN_2, INPUT); 50 51 // LCD BEGIIN MESSAGE 52 lcd.begin(16, 2); 53 lcd.setCursor(0,0); 54 lcd.print("-BUTTON SMASHER-"); // 16 WOO! :) 55 lcd.setCursor(0,1); 56 lcd.print(" WHO IS FASTER? "); 57 58 // LED ON 59 digitalWrite(BLUE_LED, HIGH); 60 digitalWrite(RED_LED, HIGH); 61 62 delay(3000); 63 64 // LED OFF 65 digitalWrite(BLUE_LED, LOW); 66 digitalWrite(RED_LED, LOW); 67 68} 69 70// --------------------------------------------------------- GAME LOOP 71void loop() { 72 if(!gameStart) { 73 74 //CHANGE SCREEN DISPLAY 75 static unsigned long lastScreenChange = 0; 76 static bool showFirstScreen = true; 77 78 if (millis() - lastScreenChange > 2000) { 79 lastScreenChange = millis(); 80 showFirstScreen = !showFirstScreen; 81 82 // LED 83 digitalWrite(BLUE_LED, HIGH); 84 digitalWrite(RED_LED, HIGH); 85 delay(LAG); 86 digitalWrite(BLUE_LED, LOW); 87 digitalWrite(RED_LED, LOW); 88 89 if (showFirstScreen) { 90 91 // LCD BEGIIN MESSAGE 92 lcd.setCursor(0,0); 93 lcd.print("-BUTTON SMASHER-"); // 16 WOO! :) 94 lcd.setCursor(0,1); 95 lcd.print(" WHO IS FASTER? "); 96 97 } else { 98 99 // LCD START MESSAGE 100 lcd.setCursor(0,0); 101 lcd.print("PLYR 1 VS PLYR 2"); 102 lcd.setCursor(0,1); 103 lcd.print("-CLICK TO START-"); 104 } 105 } 106 if (digitalRead(BTN_1) == LOW || digitalRead(BTN_2) == LOW) { 107 108 while (digitalRead(BTN_1) == LOW || digitalRead(BTN_2) == LOW) { 109 110 // DEBUG: 111 // delay(20); 112 Serial.print("BTN_1: "); 113 Serial.print(digitalRead(BTN_1)); 114 Serial.print(" BTN_2: "); 115 Serial.println(digitalRead(BTN_2)); 116 // delay(100); 117 } 118 119 Serial.println("BUTTON RELEASE - GAME STARTING"); 120 121 lcd.clear(); 122 lcd.setCursor(0,0); 123 lcd.print("-GAME STARTING!-"); 124 delay(SEC); 125 126 // GAME COUNTDOWN LCD + LED 127 lcd.setCursor(0,1); 128 129 // LED FLASH AND LCD SCREEN COUNTDOWN 130 const char* countdown[] = {"3.. ", "2.. ", "1.. ", "GO!!"}; 131 132 // LED FLASH COUNTDOWN LOOP 133 for (int i = 0; i < 4; i++) { 134 digitalWrite(BLUE_LED, HIGH); 135 digitalWrite(RED_LED, HIGH); 136 137 lcd.print(countdown[i]); 138 139 delay(SEC); 140 141 digitalWrite(BLUE_LED, LOW); 142 digitalWrite(RED_LED, LOW); 143 144 if (i < 3) { 145 delay(100); 146 } 147 } 148 149 lcd.clear(); 150 151 // RESEST SCORE 152 playerScore_1 = 0; 153 playerScore_2 = 0; 154 155 // START TIMER 156 gameStartTimer = millis(); // RECORD TIME START 157 158 // GAME START 159 gameStart = true; 160 161 // ACTIVE GAME? 162 isPlaying = true; 163 } 164 // ---------------------------------------------------- GAME ROUND BEGINGS 165 } else if (isPlaying == true) { 166 167 unsigned long elapsed = millis() - gameStartTimer; 168 169 if (elapsed >= GAME_TIME) { // IF OVER 15 SECONDS, GAME ENDS 170 171 // END OF ROUND 172 isPlaying = false; 173 gameOver = true; 174 175 } else { 176 177 // PLAYER ONE 178 if (digitalRead(BTN_1) == LOW && (millis() - lastButtonSmash_1 > 50)) { 179 // BUTTON REGISTER 180 lastButtonSmash_1 = millis(); 181 182 // PLAYER SCORE 183 playerScore_1++; 184 185 // LED 186 digitalWrite(BLUE_LED, HIGH); 187 delay(20); 188 digitalWrite(BLUE_LED, LOW); 189 190 // BUTTON RELEASE 191 while (digitalRead(BTN_1) == LOW) { 192 delay(2); 193 } 194 } 195 196 // PLAYER TWO 197 if (digitalRead(BTN_2) == LOW && (millis() - lastButtonSmash_2 > 50)) { 198 199 // BUTTON REGISTER 200 lastButtonSmash_2 = millis(); 201 202 // PLAYER SCORE 203 playerScore_2++; 204 205 // LED 206 digitalWrite(RED_LED, HIGH); 207 delay(20); 208 digitalWrite(RED_LED, LOW); 209 210 // BUTTON RELEASE 211 while (digitalRead(BTN_2) == LOW) { 212 delay(2); 213 } 214 } 215 216 // --- LCD PLAYER SCORE KEEPER 217 218 // FIRST PLAYER POSITION 219 lcd.setCursor(0, 0); 220 lcd.print("P1:"); 221 222 lcd.setCursor(3, 0); 223 if (playerScore_1 < 100) lcd.print(" "); 224 if (playerScore_1 < 10) lcd.print(" "); 225 lcd.print(playerScore_1); 226 227 // --- SECOND PLAYER POSITION 228 lcd.setCursor(10, 0); 229 lcd.print("P2:"); 230 231 lcd.setCursor(13,0); 232 if(playerScore_2 < 100) lcd.print(" "); 233 if(playerScore_2 < 10) lcd.print(" "); 234 lcd.print(playerScore_2); 235 236 // --- UPDATE LCD GAME TIME COUNTDOWN 237 lcd.setCursor(0, 1); 238 int secondsLeft = (GAME_TIME - elapsed) / 1000; 239 lcd.print("TIME: "); 240 241 lcd.setCursor(10, 1); 242 if (secondsLeft < 10) lcd.print(" "); 243 lcd.print(secondsLeft); 244 245 lcd.setCursor(13,1); 246 lcd.print("SEC"); 247 } 248 // ---------------------------------------------------- GAME ROUND ENDS 249 } else if (gameOver) { 250 251 // --- SHOW LCD WINNER 252 lcd.clear(); 253 lcd.setCursor(0,0); 254 255 // --- P1: WINNER MESSAGE 256 if (playerScore_1 > playerScore_2) { 257 258 lcd.setCursor(0,0); 259 lcd.print("PLAYER 1 WINNER!"); 260 lcd.setCursor(0,1); 261 lcd.print("PLAYER 2 LOSES! "); 262 blinkLED(BLUE_LED, 3, 500); 263 264 // --- P2: WINNER MESSAGE 265 } else if (playerScore_2 > playerScore_1) { 266 267 lcd.setCursor(0,0); 268 lcd.print("PLAYER 2 WINNER!"); 269 lcd.setCursor(0,1); 270 lcd.print("PLAYER 1 LOSES! "); 271 blinkLED(RED_LED, 3, 500); 272 273 } else { 274 275 // --- TIED GAME 276 lcd.print("-- IT'S A TIE --"); 277 278 // --- LED - ON 279 digitalWrite(BLUE_LED, HIGH); 280 digitalWrite(RED_LED, HIGH); 281 delay(LAG); 282 } 283 284 // --- LED OFF 285 digitalWrite(BLUE_LED, LOW); 286 digitalWrite(RED_LED, LOW); 287 288 // --- END GAME -> GO THROUGH LOOP AGAIN 289 delay(3000); 290 gameStart = false; 291 gameOver = false; 292 } 293} 294 295// ------------------------------------------------------ REUSABLE LED BLINKERS 296 297void blinkLED(int ledPin, int times, int duration) { 298 for (int i = 0; i < times; i++) { 299 digitalWrite(ledPin, HIGH); 300 delay(duration); 301 digitalWrite(ledPin, LOW); 302 delay(duration); 303 } 304}
Comments
Only logged in users can leave comments