Advertisement
Guest User

Untitled

a guest
May 26th, 2015
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.26 KB | None | 0 0
  1. /* Gameduino- Snake
  2. Nate Edison
  3. 5/22/2015
  4. This device will display a game of snake on the the LED matrix that can be played while tilting
  5. the board with the triple-axis accelerometer on it. It will also have an Ethernet shield so that
  6. one can upload their score to Twitter after they finish playing a game.
  7. This code uses adaptations of code from
  8. https://github.com/adafruit/Adafruit-GPS-Library
  9. https://learn.sparkfun.com/tutorials/mma8452q-accelerometer-breakout-hookup-guide
  10. https://github.com/adafruit/Adafruit-GFX-Library
  11.  
  12. a lot of code to design the features to make the actual game work were adapted from
  13. jvisch's snake 8x8 led matrix project. The project and its code can be found at the
  14. link below
  15. http://fritzing.org/projects/snake-8x8-led-matrix-ada-fruit
  16. */
  17.  
  18. //First we need to set up some libraries so that we can use all the parts we want
  19. #include <Wire.h> // we need this to use I2C which is a requirement of the
  20. // acceleromter and LED matrix
  21. #include <SFE_MMA8452Q.h> // this will include the library so we can use the accelerometer
  22. #include "Adafruit_LEDBackpack.h"
  23. #include "Adafruit_GFX.h"
  24. //the previous two libraries are so we can use the LED matrix
  25. #include <SPI.h> // needed in Arduino 0019 or later
  26. #include <Ethernet.h>
  27. #include <Twitter.h>
  28. // the previous three libraries are needed to connect the Arduino with Twitter.
  29.  
  30. //Now we need to set up some features for our accelerometer
  31. MMA8452Q accel;
  32.  
  33. //Now we need to set up some features for our LED matrix
  34. Adafruit_BicolorMatrix matrix = Adafruit_BicolorMatrix();
  35.  
  36. //Now to set up our Ethernet shield and Twitter
  37.  
  38. byte mac[] { 0x90, 0xA2, 0xDA, 0x0D, 0x14, 0x79 };
  39. //This is where you enter the information for your Ethernet shield
  40. //You should find your code on a label on the bottom of the shield
  41. //You want to enter it 0x## where ## is the information found on your label.
  42.  
  43. //If you want to specify a specific a IP address you can enter it like seen below
  44. // byte ip[] = { 10, 3, 19, 73 };
  45.  
  46. //Specify your Token so it works with your Twitter
  47. //You can get your token from (http://arduino-tweet.appspot.com/)
  48. Twitter twitter("3196797122-2jESNtWDAvddc1zYJkhaQqUjokUpVwraJ1pAbiS");
  49.  
  50. //this will set up a message which we will use later when we want to post to Twitter
  51. char msg[40];
  52.  
  53. //Now we need to set up some variables
  54. const int powerButton = 7;
  55. const int sendButton = 6;
  56. //int x = 4;
  57. //int y = 3;
  58. const int maxSnake = 64; //this is the maximum length the snake can reach
  59. int snakeX[maxSnake]; // this is the x coordinate of the snake
  60. int snakeY[maxSnake]; // this is the y coordinate of the snake
  61. int snakeLength = 1; // this is the starting length of the snake
  62. int targetX; //this is the x coordinate of the item we are going after
  63. int targetY; //the y coordinate of the item we are going after
  64. unsigned long targetPrevTime = 0; //these will be used to help with the timing of the target
  65. unsigned long targetBlinkTime = 1000 / 250;
  66. int targetLED = LED_GREEN;
  67. unsigned long prevTime = 0;
  68. unsigned long delayTime = 500;
  69. int score = 0;
  70.  
  71. // these two variables will be used to make our switch work properly
  72. int mode = 0;
  73. const int NUMMODES = 3;
  74.  
  75. void setup() {
  76.   //Set up our pins as inputs
  77.   pinMode(powerButton, INPUT);
  78.   pinMode(sendButton, INPUT);
  79.  
  80.   Serial.begin(9600);
  81.   // Ethernet.begin(mac); // this will start our Ethernet shield, if
  82.   // you specify an IP address you want to use the command, Ethernet.begin(mac, ip);
  83.   matrix.begin(0x70); //pass inthe address, set up the matrix
  84.   accel.init();
  85.   randomSeed(analogRead(A0)); //we use this so that we will be able to
  86.   //make a random pixel lite up that will serve as what our snake is after.
  87.  
  88.   makeSnake(); //this will call a function to set up where our snake will start on the matrix
  89.   makeTarget(); // this will call a function to set up where our target will be on the matrix.
  90. }
  91.  
  92. void loop() {
  93.   // this will clear the display just in case something else was on it before
  94.   matrix.clear(); // this will clear the matrix in case something was on it before
  95.   matrix.writeDisplay();
  96.  
  97.   int powerButtonState = digitalRead(powerButton); //this will moniter the state of the button
  98.   if ( powerButtonState == HIGH) {
  99.     mode = (mode + 1) % NUMMODES;
  100.   }
  101.  
  102.   switch (mode) {
  103.     case 0: //if the button has not been pushed nothing happens
  104.       Serial.println("OFF"); //debug to make sure switch works properly
  105.       break;
  106.     case 1: //if the button is pushed the game starts
  107.       playGame();
  108.       break;
  109.   }
  110. }
  111.  
  112. void playGame() { // this funciton will start up the game
  113.   matrix.clear(); // this will clear the matrix just to ensure it is
  114.   /* matrix.drawPixel(y, x, LED_GREEN);
  115.    int targetX = random(8);
  116.    int targetY = random(8);
  117.    matrix.drawPixel(targetY, targetX, LED_RED); */
  118.  
  119.   unsigned long currentTime = millis();
  120.   if (currentTime - prevTime >= delayTime) {
  121.     nextstep();
  122.     prevTime = currentTime;
  123.   }
  124.   //matrix.writeDisplay();
  125.  
  126.   draw(); // this will call the draw function which will draw the snake and target
  127. }
  128.  
  129. void nextstep() {
  130.   for (int i = snakeLength - 1; i > 0; i--) {
  131.     snakeX[i] = snakeX[i - 1];
  132.     snakeY[i] = snakeY[i - 1];
  133.   }
  134.   if (inPlayField(snakeX[0], snakeY[0])) { // if the snake is in the playing field(the 8x8 matrix)
  135.   //then we will be able to continue to play
  136.     int prevDirection;
  137.     byte pl = accel.readPL(); //this will read the direction of the accelerometer
  138.     switch (pl) {
  139.       case PORTRAIT_U: // if the accelerometer is tilted right
  140.         Serial.println("Portrait Up"); //it will print a message on the moniter
  141.         snakeY[0] = snakeY[0] - 1; // and move the snake to the right.
  142.         prevDirection = PORTRAIT_U;
  143.         break;
  144.       case PORTRAIT_D: //if the accelerometer is tilted left
  145.         Serial.println("Portrait Down"); //it will print a message
  146.         snakeY[0] = snakeY[0] + 1; // and move the snake to the left
  147.         break;
  148.       case LANDSCAPE_R: //if the accelerometer is tilted up
  149.         Serial.println("Landscape Right"); //it will print a message
  150.         snakeX[0] = snakeX[0] - 1; // and move the snake up
  151.         break;
  152.       case LANDSCAPE_L: //if the accelerometer is tilted down
  153.         Serial.println("Landscape Left"); //it will print a message
  154.         snakeX[0] = snakeX[0] + 1; // and move the snake down
  155.         break;
  156.       case LOCKOUT:
  157.         Serial.println("Flat");
  158.         break;
  159.     }
  160.     if ((snakeX[0] == targetX) && (snakeY[0] == targetY)) { //this will read if the snake is on the target
  161.       snakeLength++; //if it is it will gain a length of one
  162.       score++; // and the score will increase by one
  163.       Serial.print("Score: "); // the score will then be written on the serial monitor
  164.       Serial.println(score);
  165.       if (snakeLength < maxSnake) { // if the snake is less than the maxium lenght
  166.         makeTarget(); //then we will make a new target to go after
  167.       }
  168.       else {
  169.         targetX = targetY = -1; //if it is not less than the max, then we will not
  170.         //make any more target
  171.       }
  172.     }
  173.   }
  174.   else { // if it is not in the play field then the game is over.
  175.     gameOver();
  176.   }
  177. }
  178.  
  179. void draw () { //this will call the functions to draw the snake and target
  180.   matrix.clear();
  181.   drawSnake();
  182.   drawTarget();
  183.   matrix.writeDisplay();
  184. }
  185.  
  186. void drawSnake() { //this will draw the snake
  187.   for ( int i = 0; i < snakeLength; i++) {
  188.     matrix.drawPixel(snakeX[i], snakeY[i], LED_GREEN);
  189.   }
  190. }
  191.  
  192. void drawTarget() { //this will draw the target
  193.   if (inPlayField(targetX, targetY)) {
  194.     unsigned long currenttime = millis();
  195.     if (currenttime - targetPrevTime >= targetBlinkTime) {
  196.       targetLED = (targetLED == LED_RED) ? LED_OFF : LED_RED;
  197.       targetPrevTime = currenttime;
  198.     }
  199.     matrix.drawPixel(targetX, targetY, targetLED);
  200.   }
  201. }
  202.  
  203. boolean inPlayField(int x, int y) { //this will set up the playing field for the target
  204. // and snake to spawn in.
  205.   return (x >= 0) && (x < 8) && (y >= 0) && (y < 8);
  206. }
  207.  
  208. void makeTarget() { //this will choose a random value and set up the target
  209.   int x;
  210.   int y;
  211.   x = random(0, 8);
  212.   y = random(0, 8);
  213.   while (isPartOfSnake(x, y)) {
  214.     x = random(0, 8);
  215.     y = random(0, 8);
  216.   }
  217.   targetX = x;
  218.   targetY = y;
  219. }
  220.  
  221. boolean isPartOfSnake(int x, int y) { //this will read if the value is part of the snake or not.
  222.   for (int i = 0; i < snakeLength - 1; i++) {
  223.     if ((x == snakeX[i]) && (y == snakeY[i])) {
  224.       return true;
  225.     }
  226.   }
  227.   return false;
  228. }
  229.  
  230. void makeSnake() { //this will set up the values of the snake
  231.   snakeX[0] = 3;
  232.   snakeY[0] = 4;
  233.   for (int i = 1; i < maxSnake; i++) {
  234.     snakeX[i] = snakeY[i] = -1;
  235.   }
  236. }
  237.  
  238. void gameOver() { //this will display the lightshow and score after the game is over
  239.   Serial.println("Game Over");
  240.   Serial.print("Final Score: ");
  241.   Serial.println(score);
  242.   matrix.clear();
  243.   matrix.drawPixel(3, 4, LED_YELLOW); //start a spiral lightshow from the middle to edge
  244.   matrix.writeDisplay();
  245.   delay(100);
  246.  
  247.   matrix.drawLine(3, 4, 4, 4, LED_YELLOW);
  248.   matrix.writeDisplay();
  249.   delay(100);
  250.  
  251.   matrix.drawLine(4, 4, 4, 3, LED_YELLOW);
  252.   matrix.writeDisplay();
  253.   delay(100);
  254.  
  255.   matrix.drawLine(4, 3, 2, 3, LED_YELLOW);
  256.   matrix.writeDisplay();
  257.   delay(100);
  258.  
  259.   matrix.drawLine(2, 3, 2, 5, LED_YELLOW);
  260.   matrix.writeDisplay();
  261.   delay(100);
  262.  
  263.   matrix.drawLine(2, 5, 5, 5, LED_YELLOW);
  264.   matrix.writeDisplay();
  265.   delay(100);
  266.  
  267.   matrix.drawLine(5, 5, 5, 2, LED_YELLOW);
  268.   matrix.writeDisplay();
  269.   delay(100);
  270.  
  271.   matrix.drawLine(5, 2, 1, 2, LED_YELLOW);
  272.   matrix.writeDisplay();
  273.   delay(100);
  274.  
  275.   matrix.drawLine(1, 2, 1, 6, LED_YELLOW);
  276.   matrix.writeDisplay();
  277.   delay(100);
  278.  
  279.   matrix.drawLine(1, 6, 6, 6, LED_YELLOW);
  280.   matrix.writeDisplay();
  281.   delay(100);
  282.  
  283.   matrix.drawLine(6, 6, 6, 1, LED_YELLOW);
  284.   matrix.writeDisplay();
  285.   delay(100);
  286.  
  287.   matrix.drawLine(6, 1, 0, 1, LED_YELLOW);
  288.   matrix.writeDisplay();
  289.   delay(100);
  290.  
  291.   matrix.drawLine(0, 1, 0, 7, LED_YELLOW);
  292.   matrix.writeDisplay();
  293.   delay(100);
  294.  
  295.   matrix.drawLine(0, 7, 7, 7, LED_YELLOW);
  296.   matrix.writeDisplay();
  297.   delay(100);
  298.  
  299.   matrix.drawLine(7, 7, 7, 0, LED_YELLOW);
  300.   matrix.writeDisplay();
  301.   delay(100);
  302.  
  303.   matrix.drawLine(7, 0, 0, 0, LED_YELLOW);
  304.   matrix.writeDisplay();
  305.   delay(100);
  306.  
  307.   matrix.setTextWrap(false); //this will display our score on our matrix
  308.   matrix.setTextSize(1);
  309.   matrix.setTextColor(LED_YELLOW);
  310.   for (int8_t x = 7; x >= -36; x--) {
  311.     matrix.clear();
  312.     matrix.setCursor(x, 0);
  313.     matrix.print(score);
  314.     matrix.writeDisplay();
  315.     delay (100);
  316.   }
  317.   matrix.setRotation(3);
  318.  
  319.   int sendButtonState = digitalRead(sendButton); //if we want to post to Twitter push the button
  320.   if (sendButtonState == HIGH) {
  321.     Serial.println("button high");
  322.     sendTweet();
  323.   }
  324.   int powerButtonState = digitalRead(powerButton);
  325.   if (powerButtonState == HIGH) {
  326.     Serial.print("reset");
  327.     return(loop ());
  328.   }
  329. }
  330.  
  331. void sendTweet() { // this function will post the score of the game to Twitter
  332.   Ethernet.begin(mac);
  333.   sprintf(msg, "Snake Score: %d. Game Length: %d", score, (millis()));
  334. if (twitter.post(msg)) {
  335.   int status = twitter.wait(&Serial);
  336.   if (status == 200) {
  337.     Serial.println("OK.");
  338.   }
  339.   else {
  340.     Serial.print("failed : code ");
  341.     Serial.print(status);
  342.   }
  343. }
  344. else {
  345.   Serial.println("connection failed");
  346. }
  347. delay(1000);
  348. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement