Description
Color Chaser is a wordle inspired color guessing game, where players aim to complete the puzzle as quick as possible. Players are shown a 5-pattern color code, and the goal is to replicate that code in your neopixels. Players use motion controls to change which neo pixel is being selected and use the potentiometer to change the color of their input. When players are ready to guess on their slot, they press the button. If the color was correct, the neopixel on the left side of the circuit playground illuminates green, if wrong, it flashes red. Once the player gets all 5 neopixels correct, the game is over.
Picture
Video
Schematic
Code
// Code by Finnegan Albritton and Brett Brown, Analog Team 7 // In this minigame, a series of colors are quickly shown and you have to copy them. Use the potentionmeter to switch between 6 colors, and press the board button to submit. // Global Variables // Establishes A2 as the potentiometer const int potPin = A2; int potValue = 0; // Establishes A3 as the button const int buttonPin = A3; int selectedColorIndex = 0; int lastColorIndex = -1; bool toggle = false; // Saves the color of guesses int savedColorIndexFive = 0; int savedColorIndexSix = 0; int savedColorIndexSeven = 0; int savedColorIndexEight = 0; int savedColorIndexNine = 0; // Compares colors to randomizer int randCheck[5]; // Sets up randomizer bool setupCheck = false; // Variables to check if each guess is correct bool fiveCheck = false; bool sixCheck = false; bool sevenCheck = false; bool eightCheck = false; bool nineCheck = false; // Variables to check if each guess has been submitted bool fiveSubmit = false; bool sixSubmit = false; bool sevenSubmit = false; bool eightSubmit = false; bool nineSubmit = false; // Set up possible colors as: Red, Pink, Green, Yellow, Blue, and Purple int colorArray[6][3] = { {255, 0, 0}, {255, 77, 166}, {0, 255, 0}, {255, 255, 0}, {0, 0, 255}, {128, 0, 128} }; int randomArray = sizeof(colorArray); int* r; void setup() { CircuitPlayground.begin(); Serial.begin(9600); pinMode(buttonPin, INPUT_PULLUP); } void loop() { //Game runs while the switch is on if(CircuitPlayground.slideSwitch() == true) { //int value = CircuitPlayground.motionX(); // Read potentiometer potValue = analogRead(potPin); // Map 0–1023 to 0–5 (since you have 6 colors) selectedColorIndex = map(potValue, 0, 1023, 0, 5); selectedColorIndex = constrain(selectedColorIndex, 0, 5); int randomIndex = random(randomArray); r = colorArray[randomIndex]; if(setupCheck == false){ for(int i = 0; i < 5; i++){ randomSeed(analogRead(A1)); int colorIndex = random(5); randCheck[i] = colorIndex; CircuitPlayground.setPixelColor(i, colorArray[colorIndex][0], colorArray[colorIndex][1], colorArray[colorIndex][2]); delay(100); } setupCheck = true; } //forward and back tilt movement int valueTwo = CircuitPlayground.motionY(); int doubleMap = map(valueTwo, -9, 9, 0, 9); bool buttonState = false; int buttonBstate = digitalRead(10); // The code below checks to see how far the board is tilted to select a neopixel and change the color to guess switch (valueTwo) { case -1: // If the board is tilted to a reading of -1 and the button has not been pressed to submit a guess... if(fiveSubmit != true){ // If potentiometer is turned to a certain number, set the color to red and save it if (potValue <= 170){ CircuitPlayground.setPixelColor(5, 255, 0, 0); savedColorIndexFive = 0; // If the random color is red and guess is red, set guess variable to correct if (randCheck[4] == 0){ fiveCheck = true; } } // If potentiometer is turned to a certain number, set the color to pink and save it else if (potValue > 170 && potValue <= 340){ CircuitPlayground.setPixelColor(5, 255, 77, 166); savedColorIndexFive = 1; // If the random color is pink and guess is pink, set guess variable to correct if (randCheck[4] == 1){ fiveCheck = true; } } // If potentiometer is turned to a certain number, set the color to green and save it else if (potValue > 340 && potValue <= 510){ CircuitPlayground.setPixelColor(5, 0, 255, 0); savedColorIndexFive = 2; // If the random color is green and guess is green, set guess variable to correct if (randCheck[4] == 2){ fiveCheck = true; } } // If potentiometer is turned to a certain number, set the color to yellow and save it else if (potValue > 510 && potValue <= 680){ CircuitPlayground.setPixelColor(5, 255, 255, 0); savedColorIndexFive = 3; // If the random color is yellow and guess is yellow, set guess variable to correct if (randCheck[4] == 3){ fiveCheck = true; } } // If potentiometer is turned to a certain number, set the color to blue and save it else if (potValue > 680 && potValue <= 850){ CircuitPlayground.setPixelColor(5, 0, 0, 255); savedColorIndexFive = 4; // If the random color is blue and guess is blue, set guess variable to correct if (randCheck[4] == 4){ fiveCheck = true; } } // If potentiometer is turned to a certain number, set the color to purple and save it else if (potValue > 850 && potValue <= 1023){ CircuitPlayground.setPixelColor(5, 128, 0, 128); savedColorIndexFive = 5; // If the random color is purple and guess is purple, set guess variable to correct if (randCheck[4] == 5){ fiveCheck = true; } } // If button is pressed, submit guess if(buttonBstate == true){ fiveSubmit = true; } } // Clear neopixel delay(100); CircuitPlayground.clearPixels(); break; case -2: // If the board is tilted to a reading of -2 and the button has not been pressed to submit a guess... if(sevenSubmit != true){ // If potentiometer is turned to a certain number, set the color to red and save it if (potValue <= 170){ CircuitPlayground.setPixelColor(6, 255, 0, 0); savedColorIndexSix = 0; // If the random color is red and guess is red, set guess variable to correct if (randCheck[3] == 0){ sixCheck = true; } } // If potentiometer is turned to a certain number, set the color to pink and save it else if (potValue > 170 && potValue <= 340){ CircuitPlayground.setPixelColor(6, 255, 77, 166); savedColorIndexSix = 1; // If the random color is pink and guess is pink, set guess variable to correct if (randCheck[3] == 1){ sixCheck = true; } } // If potentiometer is turned to a certain number, set the color to green and save it else if (potValue > 340 && potValue <= 510){ CircuitPlayground.setPixelColor(6, 0, 255, 0); savedColorIndexSix = 2; // If the random color is green and guess is green, set guess variable to correct if (randCheck[3] == 2){ sixCheck = true; } } // If potentiometer is turned to a certain number, set the color to yellow and save it else if (potValue > 510 && potValue <= 680){ CircuitPlayground.setPixelColor(6, 255, 255, 0); savedColorIndexSix = 3; // If the random color is yellow and guess is yellow, set guess variable to correct if (randCheck[3] == 3){ sixCheck = true; } } // If potentiometer is turned to a certain number, set the color to blue and save it else if (potValue > 680 && potValue <= 850){ CircuitPlayground.setPixelColor(6, 0, 0, 255); savedColorIndexSix = 4; // If the random color is blue and guess is blue, set guess variable to correct if (randCheck[3] == 4){ sixCheck = true; } } // If potentiometer is turned to a certain number, set the color to purple and save it else if (potValue > 850 && potValue <= 1023){ CircuitPlayground.setPixelColor(6, 128, 0, 128); savedColorIndexSix = 5; // If the random color is purple and guess is purple, set guess variable to correct if (randCheck[3] == 5){ sixCheck = true; } } // If button is pressed, submit guess if(buttonBstate == true){ sixSubmit = true; } } // Clear neopixel delay(100); CircuitPlayground.clearPixels(); break; case -3: // If the board is tilted to a reading of -3 and the button has not been pressed to submit a guess... if(sevenSubmit != true){ // If potentiometer is turned to a certain number, set the color to red and save it if (potValue <= 170){ CircuitPlayground.setPixelColor(7, 255, 0, 0); savedColorIndexSeven = 0; // If the random color is red and guess is red, set guess variable to correct if (randCheck[2] == 0){ sevenCheck = true; } } // If potentiometer is turned to a certain number, set the color to pink and save it else if (potValue > 170 && potValue <= 340){ CircuitPlayground.setPixelColor(7, 255, 77, 166); savedColorIndexSeven = 1; // If the random color is pink and guess is pink, set guess variable to correct if (randCheck[2] == 1){ sevenCheck = true; } } // If potentiometer is turned to a certain number, set the color to green and save it else if (potValue > 340 && potValue <= 510){ CircuitPlayground.setPixelColor(7, 0, 255, 0); savedColorIndexSeven = 2; // If the random color is green and guess is green, set guess variable to correct if (randCheck[2] == 2){ sevenCheck = true; } } // If potentiometer is turned to a certain number, set the color to yellow and save it else if (potValue > 510 && potValue <= 680){ CircuitPlayground.setPixelColor(7, 255, 255, 0); savedColorIndexSeven = 3; // If the random color is yellow and guess is yellow, set guess variable to correct if (randCheck[2] == 3){ sevenCheck = true; } } // If potentiometer is turned to a certain number, set the color to blue and save it else if (potValue > 680 && potValue <= 850){ CircuitPlayground.setPixelColor(7, 0, 0, 255); savedColorIndexSeven = 4; // If the random color is blue and guess is blue, set guess variable to correct if (randCheck[2] == 4){ sevenCheck = true; } } // If potentiometer is turned to a certain number, set the color to purple and save it else if (potValue > 850 && potValue <= 1023){ CircuitPlayground.setPixelColor(7, 128, 0, 128); savedColorIndexSeven = 5; // If the random color is purple and guess is purple, set guess variable to correct if (randCheck[2] == 5){ sevenCheck = true; } } // If button is pressed, submit guess if(buttonBstate == true){ sevenSubmit = true; } } // Clear neopixel delay(100); CircuitPlayground.clearPixels(); break; case -4: // If the board is tilted to a reading of -4 and the button has not been pressed to submit a guess... if(eightSubmit != true){ // If potentiometer is turned to a certain number, set the color to red and save it if (potValue <= 170){ CircuitPlayground.setPixelColor(8, 255, 0, 0); savedColorIndexEight = 0; // If the random color is red and guess is red, set guess variable to correct if (randCheck[1] == 0){ eightCheck = true; } } // If potentiometer is turned to a certain number, set the color to pink and save it else if (potValue > 170 && potValue <= 340){ CircuitPlayground.setPixelColor(8, 255, 77, 166); savedColorIndexEight = 1; // If the random color is pink and guess is pink, set guess variable to correct if (randCheck[1] == 1){ eightCheck = true; } } // If potentiometer is turned to a certain number, set the color to green and save it else if (potValue > 340 && potValue <= 510){ CircuitPlayground.setPixelColor(8, 0, 255, 0); savedColorIndexEight = 2; // If the random color is green and guess is green, set guess variable to correct if (randCheck[1] == 2){ eightCheck = true; } } // If potentiometer is turned to a certain number, set the color to yellow and save it else if (potValue > 510 && potValue <= 680){ CircuitPlayground.setPixelColor(8, 255, 255, 0); savedColorIndexEight = 3; // If the random color is yellow and guess is yellow, set guess variable to correct if (randCheck[1] == 3){ eightCheck = true; } } // If potentiometer is turned to a certain number, set the color to blue and save it else if (potValue > 680 && potValue <= 850){ CircuitPlayground.setPixelColor(8, 0, 0, 255); savedColorIndexEight = 4; // If the random color is blue and guess is blue, set guess variable to correct if (randCheck[1] == 4){ eightCheck = true; } } // If potentiometer is turned to a certain number, set the color to purple and save it else if (potValue > 850 && potValue <= 1023){ CircuitPlayground.setPixelColor(8, 128, 0, 128); savedColorIndexEight = 5; // If the random color is purple and guess is purple, set guess variable to correct if (randCheck[1] == 5){ eightCheck = true; } } // If button is pressed, submit guess if(buttonBstate == true){ eightSubmit = true; } } // Clear neopixel delay(100); CircuitPlayground.clearPixels(); break; case -5: // If the board is tilted to a reading of -5 and the button has not been pressed to submit a guess... if(nineSubmit != true){ // If potentiometer is turned to a certain number, set the color to red and save it if (potValue <= 170){ CircuitPlayground.setPixelColor(9, 255, 0, 0); savedColorIndexNine = 0; // If the random color is red and guess is red, set guess variable to correct if (randCheck[0] == 0){ nineCheck = true; } } // If potentiometer is turned to a certain number, set the color to pink and save it else if (potValue > 170 && potValue <= 340){ CircuitPlayground.setPixelColor(9, 255, 77, 166); savedColorIndexNine = 1; // If the random color is pink and guess is pink, set guess variable to correct if (randCheck[0] == 1){ nineCheck = true; } } // If potentiometer is turned to a certain number, set the color to green and save it else if (potValue > 340 && potValue <= 510){ CircuitPlayground.setPixelColor(9, 0, 255, 0); savedColorIndexNine = 2; // If the random color is green and guess is green, set guess variable to correct if (randCheck[0] == 2){ nineCheck = true; } } // If potentiometer is turned to a certain number, set the color to yellow and save it else if (potValue > 510 && potValue <= 680){ CircuitPlayground.setPixelColor(9, 255, 255, 0); savedColorIndexNine = 3; // If the random color is yellow and guess is yellow, set guess variable to correct if (randCheck[0] == 3){ nineCheck = true; } } // If potentiometer is turned to a certain number, set the color to blue and save it else if (potValue > 680 && potValue <= 850){ CircuitPlayground.setPixelColor(9, 0, 0, 255); savedColorIndexNine = 4; // If the random color is blue and guess is blue, set guess variable to correct if (randCheck[0] == 4){ nineCheck = true; } } // If potentiometer is turned to a certain number, set the color to purple and save it else if (potValue > 850 && potValue <= 1023){ CircuitPlayground.setPixelColor(9, 128, 0, 128); savedColorIndexNine = 5; // If the random color is purple and guess is purple, set guess variable to correct if (randCheck[0] == 5){ nineCheck = true; } } // If button is pressed, submit guess if(buttonBstate == true){ nineSubmit = true; } } // Clear neopixel delay(100); CircuitPlayground.clearPixels(); break; default: //Shows no lit neopixels if the board is not tilted CircuitPlayground.clearPixels(); break; } // The code below check to see if a guess has been submitted, corresponding to each of the 5 neopixels on the left of the microusb port // Checks if neopixel five has a submitted guess if(fiveSubmit == true){ // If the guess is correct, keep it that color and make the corresponding match on the other side green to indicate a correct guess if(fiveCheck == true){ CircuitPlayground.setPixelColor(5, colorArray[savedColorIndexFive][0], colorArray[savedColorIndexFive][1], colorArray[savedColorIndexFive][2]); CircuitPlayground.setPixelColor(4, 0, 255, 0); } // If the guess is false, flash red on the corresponding match on the other side and allow another guess after a short delay if(fiveCheck == false){ CircuitPlayground.setPixelColor(4, 255, 0, 0); delay(100); fiveSubmit = false; } } // Checks if neopixel six has a submitted guess if(sixSubmit == true){ // If the guess is correct, keep it that color and make the corresponding match on the other side green to indicate a correct guess if(sixCheck == true){ CircuitPlayground.setPixelColor(6, colorArray[savedColorIndexSix][0], colorArray[savedColorIndexSix][1], colorArray[savedColorIndexSix][2]); CircuitPlayground.setPixelColor(3, 0, 255, 0); } // If the guess is false, flash red on the corresponding match on the other side and allow another guess after a short delay if(sixCheck == false){ CircuitPlayground.setPixelColor(3, 255, 0, 0); delay(100); sixSubmit = false; } } // Checks if neopixel seven has a submitted guess if(sevenSubmit == true){ // If the guess is correct, keep it that color and make the corresponding match on the other side green to indicate a correct guess if(sevenCheck == true){ CircuitPlayground.setPixelColor(7, colorArray[savedColorIndexSeven][0], colorArray[savedColorIndexSeven][1], colorArray[savedColorIndexSeven][2]); CircuitPlayground.setPixelColor(2, 0, 255, 0); } // If the guess is false, flash red on the corresponding match on the other side and allow another guess after a short delay if(sevenCheck == false){ CircuitPlayground.setPixelColor(2, 255, 0, 0); delay(100); sevenSubmit = false; } } // Checks if neopixel eight has a submitted guess if(eightSubmit == true){ // If the guess is correct, keep it that color and make the corresponding match on the other side green to indicate a correct guess if(eightCheck == true){ CircuitPlayground.setPixelColor(8, colorArray[savedColorIndexEight][0], colorArray[savedColorIndexEight][1], colorArray[savedColorIndexEight][2]); CircuitPlayground.setPixelColor(1, 0, 255, 0); } // If the guess is false, flash red on the corresponding match on the other side and allow another guess after a short delay if(eightCheck == false){ CircuitPlayground.setPixelColor(1, 255, 0, 0); delay(100); eightSubmit = false; } } // Checks if neopixel nine has a submitted guess if(nineSubmit == true){ // If the guess is correct, keep it that color and make the corresponding match on the other side green to indicate a correct guess if(nineCheck == true){ CircuitPlayground.setPixelColor(9, colorArray[savedColorIndexNine][0], colorArray[savedColorIndexNine][1], colorArray[savedColorIndexNine][2]); CircuitPlayground.setPixelColor(0, 0, 255, 0); } // If the guess is false, flash red on the corresponding match on the other side and allow another guess after a short delay if(nineCheck == false){ CircuitPlayground.setPixelColor(0, 255, 0, 0); delay(100); nineSubmit = false; } } toggle = false; } else { //turn off 5 neopixels when button is not toggled for(int i = 0; i < 5; i++) { // turns all neopixels off CircuitPlayground.clearPixels(); } toggle = true; } } //}


No comments:
Post a Comment
Note: Only a member of this blog may post a comment.