SHARE
TWEET

Rainbowduino Snake Game w/ 8x8 LED matrix

a guest Jul 23rd, 2015 223 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Rainbowduino.h>
  2. #include <Wire.h>
  3. #include "MMA7660.h"
  4. MMA7660 accelemeter;
  5. int grid[8][8];
  6. int headX = 4; int headY = 4;
  7. int foodY=2; int foodX=2; int food=0;
  8. int tempdir=1; int dir=1;
  9. int tailval=63;
  10. int i=0; int j=0;
  11. int num=0;
  12. int del=400;int sense=5;
  13. int gamemode=0; int menu=1;
  14. int red=255; int blue=255; int green=255;
  15.  
  16.  
  17. void setup()
  18. {
  19.   //Initializes rainbowduino library
  20.   Rb.init();
  21.   //Initializes accelerometer library
  22.   accelemeter.init();
  23.   //Reads from an unconneted pin to generate random numbers
  24.   randomSeed(analogRead(0));
  25. }
  26.  
  27. void loop()
  28. {
  29. //Initializes and reads from the accelerometer
  30.   int8_t x;
  31.   int8_t y;
  32.   int8_t z;
  33.   float ax,ay,az;
  34.   accelemeter.getXYZ(&x,&y,&z);
  35.  
  36.   while(menu==1){
  37.     int8_t x;
  38.     int8_t y;
  39.     int8_t z;
  40.     float ax,ay,az;
  41.     accelemeter.getXYZ(&x,&y,&z);
  42.     if(x>0){
  43.       if(green>35){
  44.         green=255-x*14;
  45.         blue=255-x*14;
  46.         }
  47.       else{
  48.         green=0;
  49.         blue=0;
  50.         for(int i=0;i<8;i++)
  51.           for(int j=0;j<8;j++)
  52.             Rb.setPixelXY(i,j,red,green,blue);
  53.         delay(2000);
  54.         Rb.blankDisplay();
  55.         gamemode=1;
  56.         menu=0;
  57.         break;
  58.         }
  59.       }
  60.  
  61.     if(x<0){
  62.       if(red>35){
  63.         red=255+x*14;
  64.         blue=255+x*14;
  65.         }
  66.       else{
  67.         red=0;
  68.         blue=0;
  69.         for(int i=0;i<8;i++)
  70.           for(int j=0;j<8;j++)
  71.             Rb.setPixelXY(i,j,red,green,blue);
  72.         delay(2000);
  73.         Rb.blankDisplay();
  74.         gamemode=0;
  75.         menu=0;
  76.         break;
  77.         }
  78.       }
  79.  
  80.  
  81.     for(int i=0;i<8;i++)
  82.       for(int j=0;j<8;j++)
  83.         Rb.setPixelXY(i,j,red,green,blue);
  84.  
  85.     delay(200);
  86.   }
  87.  
  88.   if(menu==0){
  89.   //Goes through each value in the matrix and if it's over 0, subtracts one from it
  90.     for(i=0;i<8;i++)
  91.       for(j=0;j<8;j++)
  92.         if(grid[i][j]>0)
  93.           grid[i][j]--;
  94.  
  95.   //Lights a pixel up at the x and y of the food with a random color
  96.     Rb.setPixelXY(foodX, foodY, random(1,255),random(1,255),random(1,255));
  97.  
  98.   //Interprets the input from the accelerometer and interprets it as a temporary direction
  99.     if(x>sense){tempdir=4;}
  100.     if(x<-sense){tempdir=3;}
  101.     if(y>sense){tempdir=1;}
  102.     if(y<-sense){tempdir=2;}
  103.  
  104.   //Prevents snake from doubing back on itself, turns temporary direction into actuall direction
  105.     switch(dir)
  106.     {
  107.       case 1:
  108.         if(tempdir==2){dir=1;}
  109.         else{dir=tempdir;}
  110.       break;
  111.      
  112.       case 2:
  113.         if(tempdir==1){dir=2;}
  114.         else{dir=tempdir;}
  115.       break;
  116.      
  117.       case 3:
  118.         if(tempdir==4){dir=3;}
  119.         else{dir=tempdir;}
  120.       break;
  121.      
  122.       case 4:
  123.         if(tempdir==3){dir=4;}
  124.         else{dir=tempdir;}
  125.       break;
  126.     }
  127.  
  128.   //Interprets direction into movement in the matrix
  129.     switch(dir)
  130.     {
  131.       case 1:
  132.         if(headY<8){headY++;}
  133.         if(headY>7){headY=0;}
  134.       break;
  135.  
  136.       case 2:
  137.         if(headY<8){headY--;}
  138.         if(headY<0){headY=7;}
  139.       break;
  140.      
  141.       case 3:
  142.       if(headX<8){headX--;}
  143.       if(headX<0){headX=7;}
  144.       break;
  145.  
  146.       case 4:
  147.         if(headX<8){headX++;}
  148.         if(headX>7){headX=0;}
  149.       break;
  150.     }
  151.  
  152.   //Death condition
  153.     if(grid[headX][headY]>tailval)
  154.     {
  155.     //Flashes snake different colors
  156.       for(i=0;i<8;i++){
  157.         for(j=0;j<8;j++){
  158.           if(grid[i][j]>=tailval){Rb.setPixelXY(i, j, 0x00FF00);}
  159.         }
  160.       }
  161.       delay(500);
  162.       for(i=0;i<8;i++){
  163.         for(j=0;j<8;j++){
  164.           if(grid[i][j]>=tailval){Rb.setPixelXY(i, j, 0x0000FF);}
  165.         }
  166.       }
  167.       delay(500);
  168.       for(i=0;i<8;i++){
  169.         for(j=0;j<8;j++){
  170.           if(grid[i][j]>=tailval){Rb.setPixelXY(i, j, 0x00FF00);}
  171.         }
  172.       }
  173.     //Prints "GAMEOVER" to the screen
  174.       while(num==0)
  175.       {
  176.         Rb.blankDisplay();
  177.         Rb.drawChar(0x47,0,0,0xFFFFFF);
  178.         delay(500);
  179.         Rb.blankDisplay();
  180.         Rb.drawChar(0x41,0,0,0xFFFFFF);
  181.         delay(500);
  182.         Rb.blankDisplay();
  183.         Rb.drawChar(0x4d,0,0,0xFFFFFF);
  184.         delay(500);
  185.         Rb.blankDisplay();
  186.         Rb.drawChar(0x45,0,0,0xFFFFFF);
  187.         delay(500);
  188.         Rb.blankDisplay();
  189.         Rb.drawChar(0x4f,0,0,0xFFFFFF);
  190.         delay(500);
  191.         Rb.blankDisplay();
  192.         Rb.drawChar(0x56,0,0,0xFFFFFF);
  193.         delay(500);
  194.         Rb.blankDisplay();
  195.         Rb.drawChar(0x45,0,0,0xFFFFFF);
  196.         delay(500);
  197.         Rb.blankDisplay();
  198.         Rb.drawChar(0x52,0,0,0xFFFFFF);
  199.         delay(500);
  200.       }    
  201.     }
  202.  
  203.   //Adds a delay to the game
  204.     delay(del);
  205.  
  206.   //Randomly generates a new food x and y when it's eaten
  207.     if(foodX == headX && foodY == headY)
  208.     {
  209.       tailval--;
  210.       if(gamemode==1){del-15;}
  211.       grid[foodX][foodY]==0;
  212.       foodX=random(0,8);
  213.       foodY=random(0,8);
  214.         while(grid[foodX][foodY]>0){
  215.           foodX=random(0,8);
  216.           foodY=random(0,8);
  217.         }
  218.       Rb.setPixelXY(foodX, foodY, 0xFFFFFF);
  219.       food==1;
  220.     }
  221.  
  222.   //Sets the values of wherever the food and head are to -3 and 64 respectively so they can be marked for lighting
  223.     grid[headX][headY] = 64;
  224.     grid[foodX][foodY] =-3;
  225.  
  226.   //Blanks the display for a clean slate
  227.     Rb.blankDisplay();
  228.  
  229.     if(gamemode==1){del--;}
  230.  
  231.   //Lights the head and body of the snake
  232.   for(i=0;i<8;i++){
  233.     for(j=0;j<8;j++){
  234.       if(grid[i][j]>tailval && grid[i][j]<64){Rb.setPixelXY(i, j, 0xFF0000);}
  235.       if(grid[i][j]==64){Rb.setPixelXY(i, j, 0xFF8000);}
  236.         }
  237.       }
  238.  
  239.   if(del<=0){gamemode=0;}
  240. }
  241. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top