Innovation: NeoPixels Matrix:Snake River Halt

Do you still remember Snake back we played connected our game box or Mobile River during our puerility? Today we are going to instruct how to make a snake game with 8*8 NeoPixels Ground substance. We select Arduino uno arsenic the control center and Stick breakout module to control the snake.

Step 1: Components List:

Step 2: Hardware Connection

Connect NeoPixels to pin D2.

"X" connected Control stick breakout module get in touch pin A0 on Arduino UNO card, "Y" connect peg A1, "K" connect bowling pin A2.

Ill-trea 3: Programming

<pair style="color: black; font-sept: Arial; font-size: 12pt;">P#include <Adafruit_NeoPixel.h>   #define PIN 2           //Output Pin #define MAX_LED 64       //Quantity of bead uint32_t c; int a = 0 , number; unsigned char huan = 0, ci = 0; unsigned charwoman  u = 40; //The font size of it of word group unsigned cleaning woman x = 0; unsigned char Colorize[3]; unsigned char go[] = {2, 3, 4, 5, 9, 14, 17, 20, 22, 26, 27, 29, 34, 35, 36, 37, 41, 46, 49, 54, 58, 59, 60, 61}; //24 unsigned charr palish[8][8] = {   {0, 1, 2, 3, 4, 5, 6, 7},   {15, 14, 13, 12, 11, 10, 9, 8},   {16, 17, 18, 19, 20, 21, 22, 23},   {31, 30, 29, 28, 27, 26, 25, 24},   {32, 33, 34, 35, 36, 37, 38, 39},   {47, 46, 45, 44, 43, 42, 41, 40},   {48, 49, 50, 51, 52, 53, 54, 55},   {63, 62, 61, 60, 59, 58, 57, 56}, } ; unsigned char character[][8] =  //Position the Wor to be transmitted {   {0, 1, 1, 1, 1, 1, 0, 0},   {0, 1, 0, 0, 0, 1, 0, 0}, // 0   {0, 1, 1, 1, 1, 1, 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}, // 1   {0, 1, 1, 1, 1, 1, 0, 0},   {0, 0, 0, 0, 0, 0, 0, 0},     {0, 1, 0, 1, 1, 1, 0, 0},   {0, 1, 0, 1, 0, 1, 0, 0}, // 2   {0, 1, 1, 1, 0, 1, 0, 0},   {0, 0, 0, 0, 0, 0, 0, 0},     {0, 1, 0, 1, 0, 1, 0, 0},   {0, 1, 0, 1, 0, 1, 0, 0}, // 3   {0, 1, 1, 1, 1, 1, 0, 0},   {0, 0, 0, 0, 0, 0, 0, 0},     {0, 1, 1, 1, 0, 0, 0, 0},   {0, 0, 0, 1, 0, 0, 0, 0}, // 4   {0, 1, 1, 1, 1, 1, 0, 0},   {0, 0, 0, 0, 0, 0, 0, 0},     {0, 1, 1, 1, 0, 1, 0, 0},   {0, 1, 0, 1, 0, 1, 0, 0}, // 5   {0, 1, 0, 1, 1, 1, 0, 0},   {0, 0, 0, 0, 0, 0, 0, 0},     {0, 1, 1, 1, 1, 1, 0, 0},   {0, 1, 0, 1, 0, 1, 0, 0}, // 6   {0, 1, 0, 1, 1, 1, 0, 0},   {0, 0, 0, 0, 0, 0, 0, 0},     {0, 1, 0, 0, 0, 0, 0, 0},   {0, 1, 0, 0, 0, 0, 0, 0}, // 7   {0, 1, 1, 1, 1, 1, 0, 0},   {0, 0, 0, 0, 0, 0, 0, 0},     {0, 1, 1, 1, 1, 1, 0, 0},   {0, 1, 0, 1, 0, 1, 0, 0}, // 8   {0, 1, 1, 1, 1, 1, 0, 0},   {0, 0, 0, 0, 0, 0, 0, 0},     {0, 1, 1, 1, 0, 1, 0, 0},   {0, 1, 0, 1, 0, 1, 0, 0}, // 9   {0, 1, 1, 1, 1, 1, 0, 0},   {0, 0, 0, 0, 0, 0, 0, 0}, };   unsigned char displayscore [8][8];   int speedS = 400;                                                         // Initial speed of Snake River int score;                                                              // Game score int xValue;                                                             // Stick-X int yValue;                                                             // JoyStick-Y int zValue;                                                             // JoyStick-Z int FX, FY;                                                             // Coordinate of food for thought int SX, SY;                                                             // Ordinate of snake head int KEY, K; int Enabled;                                                            // Enable to restart game   woman sx[64] ;                                                     //Coordinate of Snake torso  cleaning lady sy[64]; Adafruit_NeoPixel strip = Adafruit_NeoPixel( MAX_LED, PIN, NEO_RGB + NEO_KHZ800 );       vacancy RANDOM() { A:   FX = random(0, 7);   FY = hit-or-miss(0, 7);     for (int i = 0; i < 3 + grievance; i++)          //Prevent food expose along snake body   {     if ( FX == sx[i] && FY == sy[i])       goto A;   } } void myDelay( unsigned int Time) {                             // During the delay period of snake motility, we have to do something like food glitter and read the direction of Joystick     for (int t = 1; t <= Time; t++)   {     joyStick();     delay(1);                                     //Read the direction of Joystick    } } void joyStick() {     xValue = analogRead(A0);                             // JoyStick-X   The left value is 0, the right value is 1023, and the middle value is 515.   yValue = analogRead(A1);                             // JoyStick-Y   The bottom value is 0, the top value is 1023, the midway value is 510.     if (yValue > 950 && KEY != 5) {     K = 8;   }   else if (yValue < 50 && KEY != 8) {     K = 5;   }   else if (xValue < 50  && KEY != 6 &adenosine monophosphate;& K != 0) {       // Before operate Joystic first, you put up't move snake to the left direction.      K = 4;   }   other if (xValue > 950 &A;& KEY != 4) {     K = 6;   } } // Here you lav compare whether the direction of keypad is opposite to the snake motivate direction. If IT is opposite, then give up the keypad.  /***************************************************************************************************************************************************************************************************************/ void gameover() {                                      // Erst Gameover curriculum stopped, compact the central button to restart the game.   unsigned char measure, y;     People of colour[0] = random(3 , 18); // Set color.Parametric quantity is R G B, range 0-255.   Colour[1] = random(3, 18);   Colour[2] = random(3, 18);   c = strip.Color(Colour[1], Distort[0], Colour[2]);   apprais = score / 10;   prize = value * 4;   y = 0;     for (enumerate = assess; phone number < value + 4; turn++)   {     for (unsigned char vertical = 0; vertical < 8; vertical++)     {       displayscore[y][vertical] = character[amoun][vertical];         }     y++;   }     value = score % 10;   prize = appreciate * 4;     for (number = value; numerate < value + 4; number++)   {     for (unsigned char vertical = 0; vertical < 8; vertical++)     {       displayscore[y][vertical] = character[enumerate][vertical];       }     y++;   }     for (unsigned char horizontal = 0; horizontal < 8; horizontal++)   {     for (unsigned char vertical = 0; vertical < 8; statant++)     {         if (displayscore[horizontal][vertical] == 1) //Label whether the light is along.       {         strip.setPixelColor(light[level][vertical],  c);       }       else         strip.setPixelColor(light[horizontal][vertical],  0);       }   }     strip.show();//Send data   delay(5000);   K = 0;                  // Reset Joystick direction   sx[0] = 2;   sx[1] = 1;   sx[2] = 0;   sy[0] = 1;   sy[1] = 1;   sy[2] = 1;               // Reset snake coordinate   score = 0;              // Reset plot score   speedS = 400;                                                         // The initial speed of snake in the grass   } vitiate setup() {   Serial.begin(9600);   // Initialise program library   disinvest.begin();   // Send information. Default on the color of each point atomic number 3 0. So every point is non illuminated at the beginning.    strip.reveal();   K = 0;                  // Reset the direction of Joystick   sx[0] = 2;   sx[1] = 1;   sx[2] = 0;   sy[0] = 1;   sy[1] = 1;   sy[2] = 1;               // Reset snake coordinate   score = 0;              // Reset game score   Vividness[0] = random(3 , 18); // Set back color.Parameter is R G B, range 0-255.   Colorize[1] = random(3, 18);   Colour[2] = random(3, 18);   c = strip.Colorize(Colour[1], Emblazon[0], People of colour[2]); //Viridity  Red Blue // Set color   for (number = 0; number < 24; phone number++)   {     strip.setPixelColor(go[list],  c);   }   strip.show();// Send off data   delay(2000);   RANDOM();     //Produce nutrient } void mobile() {     KEY = K;                                      //Every movement of snake makes the direction change for once.        if (KEY == 8)                                 // Snake up movement   {     for (int i = 2 +  score; i > 0; i--)     {       sx[i] = sx[i - 1];       sy[i] = sy[i - 1];     }     sy[0] = sy[0] - 1;     if (sy[0] < 0)                               // Blend beyond the border and continue along the past side of meat       sy[0] = 7;   }   other if (KEY == 5)                            // Snake downward movement   {     for (int i = 2 +  grade; i > 0; i--)     {       sx[i] = sx[i - 1];       sy[i] = sy[i - 1];     }     sy[0] = sy[0] + 1;     if (sy[0] > 7)                               // Go on the far side the border and continue on the other side       sy[0] = 0;   }   else if (KEY == 4)                            // Snake left movement   {     for (int i = 2 +  grade; i > 0; i--)     {       sx[i] = sx[i - 1];       sy[i] = sy[i - 1];     }     sx[0] = sx[0] - 1;     if (sx[0] < 0)                               // Go on the far side the border and continue on the other sidelong       sx[0] = 7;   }   other if (KEY == 6)                            // Snake right movement   {     for (int i = 2 +  score; i > 0; i--)     {       sx[i] = sx[i - 1];       sy[i] = sy[i - 1];     }     sx[0] = sx[0] + 1;     if (sx[0] > 7)                               // Go up on the far side the border and continue connected the other side       sx[0] = 0;   }   // Move snake consistence with button } void displays() {   for (turn = 0; number < 64; number++)   //Clear the screen   {     strip.setPixelColor(number,  0);   }   strip.show();     Colour[0] = 40; // Set color. Parameter is R G B, range 0-255.   Colour[1] = 0;   Colour[2] = 0;   c = funnies.Coloration(Colour[1], Coloration[0], Colour[2]); //Green Red Blue    // Set color   x = light[FX][FY];                                              //Display food   strip.setPixelColor(x, c);     Colour[0] = random(3 , 18); // Congeal color. Parametric quantity is R G B, range 0-255.   Colour[1] = random(3, 18);   Colour[2] = random(3, 18);   c = slip.Colour in(Colour[1], Discolour[0], Colour[2]); //Green Cherry-red Low  // Set color     for (int i = 2 +  score; i > 0; i--)                   //Display serpent body   {       x = light[sx[i]][sy[i]];     loot.setPixelColor(x,  c);   }     Colour[0] = 0; // Set color. Parameter is R G B, range 0-255.   Colour[1] = 40;   Colour[2] = 0;   c = flight strip.Coloring material(Colour[1], Colour[0], Emblazon[2]); //William Green Red Blue    // Set color   x = light[sx[0]][sy[0]];                                        //Display ophidian head   strip.setPixelColor(x,  c);     strip.show();//Send out data } void loop() {       moving();   myDelay(speedS);                                // Within() is delay clock.   displays();   SX = sx[0];   SY = sy[0];   // Get coordinate of snake straits     for (int i = 1; i <= 2 + score; i++)   {     if ( SX == sx[i] && SY == sy[i])       gameover();   }   // Judge whether snake head touched its consistency. If touched, then forward to GameOver.     if (SY == FY &&ere; SX == FX)   {     Hit-or-miss();     score ++;     Colour[0] = 40; // Set down color. Parameter is R G B, range 0-255.     Colour[1] = 0;     Colour[2] = 0;     c = strip.Vividness(Colour[1], Colour[0], Colour[2]); //Green Red Blue  // Set color     x = light[FX][FY];                                              //Display food     strip.setPixelColor(x, c);     strip.show();//Send data     if ( !(score % 5)) {                      // Accerlerate snake focal ratio accordant to the score. Every 5 foods eaten, accerlerate 100ms.         speedS = speedS - 50;       if (speedS < 150)                         // The bottom limit is 200ms. If speed lower than 200ms, the velocity stays 200ms.         speedS = 150;     }   }   // Adjudicate whether food are eaten. If ophidian ate intellectual nourishment, and so plus scores and regenerate food randomly.  }              

Try out Lead

Whole tone 4: Experiment Result

Be the Primary to Share

Recommendations