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