LedStrike Game with Arduino Plug and Make Kit
This sketch implements the "LedStrike" game, which challenges players to react quickly by pressing a button when a moving LED aligns with a stationary red target LED.
Components and supplies
1
Arduino Plug and Make Kit
Apps and platforms
1
Arduino IDE
Project description
Code
LedStrike Game for Arduino Plug and Make Kit
cpp
LedStrike Game for Arduino Plug and Make Kit
1/* 2 LedStrike Game for Arduino Plug and Make Kit 3 4 This sketch implements the "LedStrike" game, which challenges players to react quickly 5 by pressing a button when a moving LED aligns with a stationary red target LED. The game 6 performs the following tasks: 7 8 - Continuously moves a blue LED up and down while keeping a red target LED fixed. 9 - Changes the target position randomly for each game round. 10 - Responds to button presses: if the player presses the button when the blue LED is 11 on the red target, they win; otherwise, they lose. 12 - The game speed increases gradually with each win and resets on a loss. 13 - The game state is indicated with buzzer sounds for win and loss conditions. 14 15 The game provides visual feedback with addressable RGB LEDs and a simple interface through 16 buttons for interactive gameplay. 17 18 Compatibility: 19 - This code is designed to work with the Modulino system components and an Arduino UNO R4 WiFi. 20 21 created 12 Aug 2024 22 by METE HOCA 23*/ 24 25#include "Modulino.h" // Include the Modulino library for accessing components 26#include "Arduino_LED_Matrix.h" // Include the Arduino LED Matrix library for controlling the LED matrix 27 28ModulinoBuzzer buzzer; // Buzzer instance 29ModulinoButtons buttons; // Buttons instance 30ModulinoPixels leds; // RGB LEDs instance 31ArduinoLEDMatrix matrix; 32 33ModulinoColor OFF(0, 0, 0); 34 35int ledPosition = 0; // Current position of the moving LED 36bool movingUp = true; // Direction of movement (true for up, false for down) 37unsigned long previousMillis = 0; 38long interval = 100; // Initial interval for LED movement 39const long intervalDecrement = 5; // Amount to decrease interval after each win 40const long minInterval = 50; // Minimum interval limit 41 42int targetPosition; // Position of the red target LED 43bool gameActive = true; // Flag to check if the game is active 44 45void setup() { 46 Serial.begin(115200); // Start serial communication 47 48 // Initialize components 49 Modulino.begin(); 50 buzzer.begin(); 51 buttons.begin(); 52 leds.begin(); 53 matrix.begin(); 54 55 // Generate a random seed using A0 pin 56 randomSeed(analogRead(A0)); 57 58 matrix.loadFrame(LEDMATRIX_EMOJI_BASIC); 59 startNewGame(); // Start the first game round 60} 61 62void loop() { 63 if (gameActive) { 64 unsigned long currentMillis = millis(); 65 66 // Check if it's time to move the LED 67 if (currentMillis - previousMillis >= interval) { 68 previousMillis = currentMillis; 69 70 // Clear the previous LED state 71 leds.set(ledPosition, OFF, 4); 72 73 // Move the LED 74 if (movingUp) { 75 ledPosition++; 76 if (ledPosition >= 7) { 77 movingUp = false; 78 } 79 } else { 80 ledPosition--; 81 if (ledPosition <= 0) { 82 movingUp = true; 83 } 84 } 85 86 // Set the new LED state 87 if (ledPosition == targetPosition) { 88 leds.set(ledPosition, VIOLET, 4); // Violet LED when green reaches red 89 } else { 90 leds.set(ledPosition, BLUE, 4); // Moving green LED 91 leds.set(targetPosition, RED, 4); // Red target LED (always on) 92 } 93 leds.show(); 94 } 95 96 // Check if any button is pressed 97 if (buttons.update() && (buttons.isPressed(0) || buttons.isPressed(1) || buttons.isPressed(2))) { 98 if (ledPosition == targetPosition) { 99 win(); // Win condition 100 } else { 101 lose(); // Lose condition 102 } 103 } 104 } 105} 106 107// Function to start a new game round 108void startNewGame() { 109 matrix.loadFrame(LEDMATRIX_EMOJI_BASIC); 110 gameActive = true; 111 ledPosition = 0; // Start position 112 movingUp = true; 113 114 // Randomly select a target position for the red LED 115 targetPosition = random(0, 8); 116 117 // Set the initial LED states 118 for (int i = 0; i < 8; i++) { 119 if (i == targetPosition) { 120 leds.set(i, RED, 4); // Red target LED (always on) 121 } else { 122 leds.set(i, OFF, 4); // Blue background 123 } 124 } 125 leds.show(); 126} 127 128// Function to handle the win condition 129void win() { 130 gameActive = false; 131 matrix.loadFrame(LEDMATRIX_EMOJI_HAPPY); 132 buzzer.tone(1000, 500); // Play a winning tone 133 leds.set(targetPosition, GREEN, 4); 134 leds.show(); 135 Serial.println("You Win!"); 136 137 // Decrease the interval to increase speed, but not below the minimum interval 138 if (interval > minInterval) { 139 interval -= intervalDecrement; 140 } 141 142 delay(1000); 143 startNewGame(); // Start a new game round after winning 144} 145 146// Function to handle the lose condition 147void lose() { 148 gameActive = false; 149 matrix.loadFrame(LEDMATRIX_EMOJI_SAD); 150 buzzer.tone(200, 500); // Play a losing tone 151 Serial.println("You Lose!"); 152 153 // Reset the interval to the initial value 154 interval = 100; 155 156 delay(1000); 157 startNewGame(); // Start a new game round after losing 158}
Downloadable files
LedStrike Game for Arduino Plug and Make Kit
LedStrike Game for Arduino Plug and Make Kit
LedStrike.zip
Comments
Only logged in users can leave comments