Arduino Plug and Make Kit ile RainCatcher 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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
/* RainCatcher Game for Arduino Plug and Make Kit This sketch implements the "RainCatcher" game, where players control a pixel "catcher" at the bottom of the Uno R4 WiFi LED matrix, attempting to catch falling raindrops. The game performs the following tasks: - Continuously spawns raindrops that fall from the top of the LED matrix. - The player controls the catcher using an encoder to move left and right. - If the player catches the raindrop, the game speeds up slightly. - If the player misses a raindrop, they lose one of their three lives, indicated by LEDs at the top left. - The game ends when all three lives are lost, and the speed resets upon restart. - Visual feedback is provided through an LED matrix, and buzzer sounds indicate catch and miss events. Compatibility: - This code is designed to work with the Modulino system components and an Arduino UNO R4 WiFi. created 23 Aug 2024 by METE HOCA */ #include <Modulino.h> #include "Arduino_LED_Matrix.h" ModulinoKnob encoder; ModulinoBuzzer buzzer; ArduinoLEDMatrix matrix; // Create an instance of the LED matrix // Frame layout for the 12x8 matrix byte frame[8][12] = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }; int catcherPosition = 5; // Initial position of the catcher pixel at the bottom int raindropColumn; // Column where the raindrop falls int raindropRow = 0; // Initial row for the raindrop bool raindropFalling = false; // Flag to track if a raindrop is falling int fallDelay = 200; // Initial delay for falling raindrop const int minFallDelay = 100; // Minimum delay for falling raindrop const int fallSpeedDecrease = 5; // Amount to decrease the delay int missCount = 0; // Count of missed raindrops const int maxMisses = 3; // Maximum allowed misses int health = 3; // Initial health // Variable to store the initial fall delay int initialFallDelay; void setup() { Modulino.begin(); encoder.begin(); buzzer.begin(); matrix.begin(); // Initialize the LED matrix encoder.set(catcherPosition); // Set the starting position of the catcher initialFallDelay = fallDelay; // Store the initial fall delay } void loop() { // Clear the frame clearFrame(); // Check if a raindrop is falling if (raindropFalling) { // Move the raindrop down frame[raindropRow][raindropColumn] = 1; raindropRow++; // Check if the raindrop has reached the bottom if (raindropRow == 8) { buzzer.tone(500, 200); // Sound the buzzer at 500 Hz for not catching missCount++; // Increment the miss count health--; // Decrease health on miss raindropFalling = false; // Reset the raindrop raindropRow = 0; // Reset raindrop row spawnRaindrop(); // Spawn a new raindrop // Check if the game is over if (missCount >= maxMisses) { gameOver(); } } } else { // Spawn a new raindrop if one isn't falling spawnRaindrop(); } // Get the current position from the encoder catcherPosition = encoder.get(); // Limit the position to the range of 0-11 if (catcherPosition < 0) { catcherPosition = 0; encoder.set(0); } else if (catcherPosition > 11) { catcherPosition = 11; encoder.set(11); } // Check for catching the raindrop if (raindropRow == 7 && catcherPosition == raindropColumn) { buzzer.tone(1000, 200); // Sound the buzzer for catching raindropFalling = false; // Reset the raindrop raindropRow = 0; // Reset raindrop row fallDelay = max(fallDelay - fallSpeedDecrease, minFallDelay); // Speed up the raindrop } // Set the catcher pixel at the current position frame[7][catcherPosition] = 1; // Display health indicators in the first row displayHealth(); // Render the updated frame on the LED matrix matrix.renderBitmap(frame, 8, 12); delay(fallDelay); // Control the speed of the raindrop falling } // Function to clear the frame void clearFrame() { for (int i = 0; i < 8; i++) { for (int j = 0; j < 12; j++) { frame[i][j] = 0; // Clear each pixel } } } // Function to spawn a new raindrop void spawnRaindrop() { raindropColumn = random(0, 12); // Get a random column for the raindrop raindropRow = 0; // Start at the top raindropFalling = true; // Set the raindrop as falling } // Function to display health indicators void displayHealth() { for (int i = 0; i < 3; i++) { if (i < health) { frame[0][i] = 1; // Display health indicator } else { frame[0][i] = 0; // No health indicator } } } // Function to handle game over void gameOver() { // Fill the entire frame to indicate game over for (int i = 0; i < 8; i++) { for (int j = 0; j < 12; j++) { frame[i][j] = 1; // Turn on all pixels } } // Render the filled game over screen matrix.renderBitmap(frame, 8, 12); buzzer.tone(200, 1000); // Sound buzzer for game over delay(2000); // Delay to show game over screen // Reset the game state missCount = 0; // Reset miss count health = 3; // Reset health raindropFalling = false; // Stop falling raindrop raindropRow = 0; // Reset raindrop row fallDelay = initialFallDelay; // Reset the falling speed clearFrame(); // Clear the frame for the next game spawnRaindrop(); // Spawn a new raindrop } |