| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • Want to organize your cloud files? Sign up for a free webinar to see how Dokkio (a new product from PBworks) can help you find, organize, and collaborate on your Drive, Gmail, Dropbox, and Slack files: Weds, May 27 at 2PM Eastern / 11AM Pacific
View
 

Snake Code

Page history last edited by Kyle Moore 9 years, 1 month ago

/*

 I will add comments later... Good luck.

*/  

 

// Anlalog input pins for joystick pots

#define xPin 0

#define yPin 1

 

// Size of LED grid (these may actually be backwards)

#define numRows 5

#define numCols 7

 

#define maxSnakeLength 10 //Snakes over 10 dots long are unwieldy 

 

// Pins drivng the LED grid (this may be backwards as well...)

// The columns (as defined here) are ground pins, and the

// rows are Vcc pins.

const int rowPins[numRows] = {9,10,11,12,13};

const int colPins[numCols] = {2,3,4,5,6,7,8};

 

int currentSnakeLength = 3; // Start off with a 3-dot snake

 

int currentRows[maxSnakeLength+1]; // Indicies of the row pins currently on

int currentCols[maxSnakeLength+1]; // Indicies of the coumn pins currently on

 

// Keeps track of which LED is on (the program alternates between

// all of the LEDs that are supposed to be on (because only one

// LED can be powered on at a time) to give the appearance that

// all of the specified LEDs are on.

int currentlyBlinking = 0;

 

int score = 0; // Stores score

int alive = 1; // Stores game state, if alive == 0, you are dead

 

void setup() {

  // These next two for() loops set all of the digital pins to OUTPUT

  // and turns off all of the LEDs (by reverse voltaging them, not ideal,

  // I know, but it's the only way it works...)

 

  // Set all of the Vcc pins to output and set them LOW

  for(int i = 0; i < numRows; ++i){

    pinMode(rowPins[i], OUTPUT);

    digitalWrite(rowPins[i], LOW);

  }

  // Set all of the Ground pins to output and set them HIGH

  for(int i = 0; i < numCols; ++i){

    pinMode(colPins[i], OUTPUT);

    digitalWrite(colPins[i], LOW);

  }

 

  // Set all LED off in the arrays

  for(int i = 0; i < maxSnakeLength; ++i){

    currentRows[i] = -1;

    currentCols[i] = -1;

  }

 

  // Choose a random LED to be the "food" (always stored in the last array slot)

  currentRows[maxSnakeLength] = random(0, numRows);

  currentCols[maxSnakeLength] = random(0, numCols);

 

  // Setup serial

  Serial.begin(9600);

}

 

void loop() {

  if(alive == 0){

    // If the game is over, turn on all of the LEDs and bail

    for(int i = 0; i < numRows; ++i){

      digitalWrite(rowPins[i], HIGH);

    }

 

    for(int i = 0; i < numCols; ++i){

      digitalWrite(colPins[i], LOW);

    }

    delay(100);

    return;

  }

  int xPos = analogRead(xPin);

  int yPos = analogRead(yPin);

 

  int row = xPos/205;

  int col = yPos/147;

 

  if(row != currentRows[0] || col != currentCols[0]){

 

    for(int i = currentSnakeLength-1; i >= 1; --i){

      currentRows[i] = currentRows[i-1];

      currentCols[i] = currentCols[i-1];

    }

    currentRows[0] = row;

    currentCols[0] = col;

 

    if(currentSnakeLength > 3){

      for(int i = 4; i < currentSnakeLength; ++i){

        if(row == currentRows[i] && col == currentCols[i]){

          Serial.println("You Lose!");

          alive = 0;

          return;

        }

      }

    }

 

    if(row == currentRows[maxSnakeLength] && col == currentCols[maxSnakeLength]){

      currentRows[maxSnakeLength] = random(0, numRows);

      currentCols[maxSnakeLength] = random(0, numCols);

      ++score;

      if(currentSnakeLength < maxSnakeLength){

        ++currentSnakeLength;

      }

      Serial.print("You just scored!");

      Serial.print(" Your score is: ");

      Serial.println(score);

    }

  }

 

  int rPin, cPin;

 

  if(currentlyBlinking == currentSnakeLength){

    rPin = currentRows[maxSnakeLength];

    cPin = currentCols[maxSnakeLength];

  }else{

    rPin = currentRows[currentlyBlinking];

    cPin = currentCols[currentlyBlinking];

  }

 

  for(int i = 0; i < numRows; ++i){

    digitalWrite(rowPins[i], (i == rPin) ? HIGH : LOW);

  }

 

  for(int i = 0; i < numCols; ++i){

    digitalWrite(colPins[i], (i == cPin) ? LOW : HIGH);

  }

 

  if(currentlyBlinking < currentSnakeLength){

    ++currentlyBlinking;

  }else{

    currentlyBlinking = 0;

  }

  delay(1);

}

 

Comments (0)

You don't have permission to comment on this page.