Snake game for a 5x5 LED matrix

Dependencies:   MCP23S17 mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers snake.cpp Source File

snake.cpp

00001 #include "snake.h"
00002 #include "bodyPiece.h"
00003 #include <list>
00004 #include "ledCube.h"
00005 
00006 // Constructor
00007 snake::snake(char startRow, char startCol)
00008 {
00009     bodyPiece head = bodyPiece();
00010     head.currRow = startRow;
00011     head.currCol = startCol;
00012     snakeBody.push_back(head);
00013     bodySize = 1;
00014     movementDirection = START_DIRECTION;
00015     movementSpeed = START_SPEED;
00016 }
00017 
00018 // Movement method for the snake, will update LEDs and snake's body
00019 char snake::move(char newHeadRow, char newHeadCol, ledCube *cube)
00020 {
00021 
00022     // Variables to hold the bodyPiece's X and Y positions on the grid
00023     char prevRow, prevCol, tempRow, tempCol;
00024 
00025     // Store the snake's head into the iterator
00026     std::list<bodyPiece>::iterator it=snakeBody.begin();
00027     // Save the head's current Row and Col position
00028     prevRow = (*it).currRow;
00029     prevCol = (*it).currCol;
00030     // Update the head to the new Row and Col
00031     (*it).currRow = newHeadRow;
00032     (*it).currCol = newHeadCol;
00033     (*cube).turnOffLed(prevRow, prevCol, 0);
00034     (*cube).turnOnLed((*it).currRow, (*it).currCol, 0);
00035     char bodyCount = 1;
00036     // Check to see if we are at the tail with this while loop
00037     while(bodyCount < bodySize) {
00038         // Move to the next bodyPiece
00039         it++;
00040         bodyCount++;
00041         // Store the previous piece's location in the current piece
00042         tempRow = (*it).currRow;
00043         (*it).currRow = prevRow;
00044         tempCol = (*it).currCol;
00045         (*it).currCol = prevCol;
00046         prevRow = tempRow;
00047         prevCol = tempCol;
00048         // Check to see if the head has collided with this bodyPiece
00049         if(snakeBody.front().currRow == prevRow && snakeBody.front().currCol == prevCol) {
00050             return 1;
00051         }
00052         if(prevRow < 5 && prevCol < 5) {
00053             (*cube).turnOffLed(prevRow, prevCol, 0);
00054         }
00055         (*cube).turnOnLed((*it).currRow, (*it).currCol, 0);
00056     }
00057     return 0;
00058 }
00059 
00060 // Checks the current movement direction and decided where to move the snake head next
00061 // Will return 1 if out of bounds or if the head has hit a bodyPiece, 0 if still in the boundaries
00062 char snake::moveSnake(ledCube *cube)
00063 {
00064     bodyPiece head = snakeBody.front();
00065     char ret = 0;
00066     switch(movementDirection) {
00067         case Down:
00068             if(head.currCol+1 > NUM_COLS) {
00069                 ret =  1;
00070             } else {
00071                 ret = move(head.currRow, head.currCol+1, cube);
00072             }
00073             break;
00074         case Up:
00075             if(head.currCol-1 < 0) {
00076                 ret = 1;
00077             } else {
00078                 ret = move(head.currRow, head.currCol-1, cube);
00079             }
00080             break;
00081         case Left:
00082             if(head.currRow-1 < 0) {
00083                 ret = 1;
00084             } else {
00085                 ret = move(head.currRow-1, head.currCol, cube);
00086             }
00087             break;
00088         case Right:
00089             if(head.currRow+1 > NUM_ROWS) {
00090                 ret = 1;
00091             } else {
00092                 ret = move(head.currRow+1, head.currCol, cube);
00093             }
00094     }
00095     return ret;
00096 }
00097 
00098 // Adds a new piece on to snake's tail
00099 void snake::addPiece()
00100 {
00101     bodyPiece b;
00102     snakeBody.push_back(b);
00103     bodySize++;
00104     printf("piece added + bodySize: %d\n", bodySize);
00105 }
00106 
00107 bool snake::isEating(char foodRow, char foodCol)
00108 {
00109     if(snakeBody.front().currRow == foodRow && snakeBody.front().currCol == foodCol) {
00110         addPiece();
00111         return 1;
00112     } else {
00113         return 0;
00114     }
00115 }
00116 
00117 // Constructor for the food class
00118 food::food(char row, char col)
00119 {
00120     currRow = row;
00121     currCol = col;
00122 }
00123 
00124 // Moves food to a new row and column
00125 void food::moveFood(char row, char col)
00126 {
00127     currRow = row;
00128     currCol = col;
00129 }
00130