Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Gameduino- Snake
- Nate Edison
- 5/22/2015
- This device will display a game of snake on the the LED matrix that can be played while tilting
- the board with the triple-axis accelerometer on it. It will also have an Ethernet shield so that
- one can upload their score to Twitter after they finish playing a game.
- This code uses adaptations of code from
- https://github.com/adafruit/Adafruit-GPS-Library
- https://learn.sparkfun.com/tutorials/mma8452q-accelerometer-breakout-hookup-guide
- https://github.com/adafruit/Adafruit-GFX-Library
- a lot of code to design the features to make the actual game work were adapted from
- jvisch's snake 8x8 led matrix project. The project and its code can be found at the
- link below
- http://fritzing.org/projects/snake-8x8-led-matrix-ada-fruit
- */
- //First we need to set up some libraries so that we can use all the parts we want
- #include <Wire.h> // we need this to use I2C which is a requirement of the
- // acceleromter and LED matrix
- #include <SFE_MMA8452Q.h> // this will include the library so we can use the accelerometer
- #include "Adafruit_LEDBackpack.h"
- #include "Adafruit_GFX.h"
- //the previous two libraries are so we can use the LED matrix
- #include <SPI.h> // needed in Arduino 0019 or later
- #include <Ethernet.h>
- #include <Twitter.h>
- // the previous three libraries are needed to connect the Arduino with Twitter.
- //Now we need to set up some features for our accelerometer
- MMA8452Q accel;
- //Now we need to set up some features for our LED matrix
- Adafruit_BicolorMatrix matrix = Adafruit_BicolorMatrix();
- //Now to set up our Ethernet shield and Twitter
- byte mac[] { 0x90, 0xA2, 0xDA, 0x0D, 0x14, 0x79 };
- //This is where you enter the information for your Ethernet shield
- //You should find your code on a label on the bottom of the shield
- //You want to enter it 0x## where ## is the information found on your label.
- //If you want to specify a specific a IP address you can enter it like seen below
- // byte ip[] = { 10, 3, 19, 73 };
- //Specify your Token so it works with your Twitter
- //You can get your token from (http://arduino-tweet.appspot.com/)
- Twitter twitter("3196797122-2jESNtWDAvddc1zYJkhaQqUjokUpVwraJ1pAbiS");
- //this will set up a message which we will use later when we want to post to Twitter
- char msg[40];
- //Now we need to set up some variables
- const int powerButton = 7;
- const int sendButton = 6;
- //int x = 4;
- //int y = 3;
- const int maxSnake = 64; //this is the maximum length the snake can reach
- int snakeX[maxSnake]; // this is the x coordinate of the snake
- int snakeY[maxSnake]; // this is the y coordinate of the snake
- int snakeLength = 1; // this is the starting length of the snake
- int targetX; //this is the x coordinate of the item we are going after
- int targetY; //the y coordinate of the item we are going after
- unsigned long targetPrevTime = 0; //these will be used to help with the timing of the target
- unsigned long targetBlinkTime = 1000 / 250;
- int targetLED = LED_GREEN;
- unsigned long prevTime = 0;
- unsigned long delayTime = 500;
- int score = 0;
- // these two variables will be used to make our switch work properly
- int mode = 0;
- const int NUMMODES = 3;
- void setup() {
- //Set up our pins as inputs
- pinMode(powerButton, INPUT);
- pinMode(sendButton, INPUT);
- Serial.begin(9600);
- // Ethernet.begin(mac); // this will start our Ethernet shield, if
- // you specify an IP address you want to use the command, Ethernet.begin(mac, ip);
- matrix.begin(0x70); //pass inthe address, set up the matrix
- accel.init();
- randomSeed(analogRead(A0)); //we use this so that we will be able to
- //make a random pixel lite up that will serve as what our snake is after.
- makeSnake(); //this will call a function to set up where our snake will start on the matrix
- makeTarget(); // this will call a function to set up where our target will be on the matrix.
- }
- void loop() {
- // this will clear the display just in case something else was on it before
- matrix.clear(); // this will clear the matrix in case something was on it before
- matrix.writeDisplay();
- int powerButtonState = digitalRead(powerButton); //this will moniter the state of the button
- if ( powerButtonState == HIGH) {
- mode = (mode + 1) % NUMMODES;
- }
- switch (mode) {
- case 0: //if the button has not been pushed nothing happens
- Serial.println("OFF"); //debug to make sure switch works properly
- break;
- case 1: //if the button is pushed the game starts
- playGame();
- break;
- }
- }
- void playGame() { // this funciton will start up the game
- matrix.clear(); // this will clear the matrix just to ensure it is
- /* matrix.drawPixel(y, x, LED_GREEN);
- int targetX = random(8);
- int targetY = random(8);
- matrix.drawPixel(targetY, targetX, LED_RED); */
- unsigned long currentTime = millis();
- if (currentTime - prevTime >= delayTime) {
- nextstep();
- prevTime = currentTime;
- }
- //matrix.writeDisplay();
- draw(); // this will call the draw function which will draw the snake and target
- }
- void nextstep() {
- for (int i = snakeLength - 1; i > 0; i--) {
- snakeX[i] = snakeX[i - 1];
- snakeY[i] = snakeY[i - 1];
- }
- if (inPlayField(snakeX[0], snakeY[0])) { // if the snake is in the playing field(the 8x8 matrix)
- //then we will be able to continue to play
- int prevDirection;
- byte pl = accel.readPL(); //this will read the direction of the accelerometer
- switch (pl) {
- case PORTRAIT_U: // if the accelerometer is tilted right
- Serial.println("Portrait Up"); //it will print a message on the moniter
- snakeY[0] = snakeY[0] - 1; // and move the snake to the right.
- prevDirection = PORTRAIT_U;
- break;
- case PORTRAIT_D: //if the accelerometer is tilted left
- Serial.println("Portrait Down"); //it will print a message
- snakeY[0] = snakeY[0] + 1; // and move the snake to the left
- break;
- case LANDSCAPE_R: //if the accelerometer is tilted up
- Serial.println("Landscape Right"); //it will print a message
- snakeX[0] = snakeX[0] - 1; // and move the snake up
- break;
- case LANDSCAPE_L: //if the accelerometer is tilted down
- Serial.println("Landscape Left"); //it will print a message
- snakeX[0] = snakeX[0] + 1; // and move the snake down
- break;
- case LOCKOUT:
- Serial.println("Flat");
- break;
- }
- if ((snakeX[0] == targetX) && (snakeY[0] == targetY)) { //this will read if the snake is on the target
- snakeLength++; //if it is it will gain a length of one
- score++; // and the score will increase by one
- Serial.print("Score: "); // the score will then be written on the serial monitor
- Serial.println(score);
- if (snakeLength < maxSnake) { // if the snake is less than the maxium lenght
- makeTarget(); //then we will make a new target to go after
- }
- else {
- targetX = targetY = -1; //if it is not less than the max, then we will not
- //make any more target
- }
- }
- }
- else { // if it is not in the play field then the game is over.
- gameOver();
- }
- }
- void draw () { //this will call the functions to draw the snake and target
- matrix.clear();
- drawSnake();
- drawTarget();
- matrix.writeDisplay();
- }
- void drawSnake() { //this will draw the snake
- for ( int i = 0; i < snakeLength; i++) {
- matrix.drawPixel(snakeX[i], snakeY[i], LED_GREEN);
- }
- }
- void drawTarget() { //this will draw the target
- if (inPlayField(targetX, targetY)) {
- unsigned long currenttime = millis();
- if (currenttime - targetPrevTime >= targetBlinkTime) {
- targetLED = (targetLED == LED_RED) ? LED_OFF : LED_RED;
- targetPrevTime = currenttime;
- }
- matrix.drawPixel(targetX, targetY, targetLED);
- }
- }
- boolean inPlayField(int x, int y) { //this will set up the playing field for the target
- // and snake to spawn in.
- return (x >= 0) && (x < 8) && (y >= 0) && (y < 8);
- }
- void makeTarget() { //this will choose a random value and set up the target
- int x;
- int y;
- x = random(0, 8);
- y = random(0, 8);
- while (isPartOfSnake(x, y)) {
- x = random(0, 8);
- y = random(0, 8);
- }
- targetX = x;
- targetY = y;
- }
- boolean isPartOfSnake(int x, int y) { //this will read if the value is part of the snake or not.
- for (int i = 0; i < snakeLength - 1; i++) {
- if ((x == snakeX[i]) && (y == snakeY[i])) {
- return true;
- }
- }
- return false;
- }
- void makeSnake() { //this will set up the values of the snake
- snakeX[0] = 3;
- snakeY[0] = 4;
- for (int i = 1; i < maxSnake; i++) {
- snakeX[i] = snakeY[i] = -1;
- }
- }
- void gameOver() { //this will display the lightshow and score after the game is over
- Serial.println("Game Over");
- Serial.print("Final Score: ");
- Serial.println(score);
- matrix.clear();
- matrix.drawPixel(3, 4, LED_YELLOW); //start a spiral lightshow from the middle to edge
- matrix.writeDisplay();
- delay(100);
- matrix.drawLine(3, 4, 4, 4, LED_YELLOW);
- matrix.writeDisplay();
- delay(100);
- matrix.drawLine(4, 4, 4, 3, LED_YELLOW);
- matrix.writeDisplay();
- delay(100);
- matrix.drawLine(4, 3, 2, 3, LED_YELLOW);
- matrix.writeDisplay();
- delay(100);
- matrix.drawLine(2, 3, 2, 5, LED_YELLOW);
- matrix.writeDisplay();
- delay(100);
- matrix.drawLine(2, 5, 5, 5, LED_YELLOW);
- matrix.writeDisplay();
- delay(100);
- matrix.drawLine(5, 5, 5, 2, LED_YELLOW);
- matrix.writeDisplay();
- delay(100);
- matrix.drawLine(5, 2, 1, 2, LED_YELLOW);
- matrix.writeDisplay();
- delay(100);
- matrix.drawLine(1, 2, 1, 6, LED_YELLOW);
- matrix.writeDisplay();
- delay(100);
- matrix.drawLine(1, 6, 6, 6, LED_YELLOW);
- matrix.writeDisplay();
- delay(100);
- matrix.drawLine(6, 6, 6, 1, LED_YELLOW);
- matrix.writeDisplay();
- delay(100);
- matrix.drawLine(6, 1, 0, 1, LED_YELLOW);
- matrix.writeDisplay();
- delay(100);
- matrix.drawLine(0, 1, 0, 7, LED_YELLOW);
- matrix.writeDisplay();
- delay(100);
- matrix.drawLine(0, 7, 7, 7, LED_YELLOW);
- matrix.writeDisplay();
- delay(100);
- matrix.drawLine(7, 7, 7, 0, LED_YELLOW);
- matrix.writeDisplay();
- delay(100);
- matrix.drawLine(7, 0, 0, 0, LED_YELLOW);
- matrix.writeDisplay();
- delay(100);
- matrix.setTextWrap(false); //this will display our score on our matrix
- matrix.setTextSize(1);
- matrix.setTextColor(LED_YELLOW);
- for (int8_t x = 7; x >= -36; x--) {
- matrix.clear();
- matrix.setCursor(x, 0);
- matrix.print(score);
- matrix.writeDisplay();
- delay (100);
- }
- matrix.setRotation(3);
- int sendButtonState = digitalRead(sendButton); //if we want to post to Twitter push the button
- if (sendButtonState == HIGH) {
- Serial.println("button high");
- sendTweet();
- }
- int powerButtonState = digitalRead(powerButton);
- if (powerButtonState == HIGH) {
- Serial.print("reset");
- return(loop ());
- }
- }
- void sendTweet() { // this function will post the score of the game to Twitter
- Ethernet.begin(mac);
- sprintf(msg, "Snake Score: %d. Game Length: %d", score, (millis()));
- if (twitter.post(msg)) {
- int status = twitter.wait(&Serial);
- if (status == 200) {
- Serial.println("OK.");
- }
- else {
- Serial.print("failed : code ");
- Serial.print(status);
- }
- }
- else {
- Serial.println("connection failed");
- }
- delay(1000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement