Snake game for a 5x5 LED matrix
Diff: snake.cpp
- Revision:
- 0:dc906408980e
- Child:
- 1:5fcb94bb03db
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/snake.cpp Wed Oct 09 16:23:20 2013 +0000
@@ -0,0 +1,51 @@
+#include "snake.h"
+#include "bodyPiece.h"
+#include <list>
+
+snake::snake(char startRow, char startCol){
+ bodyPiece head = bodyPiece();
+ head.currRow = startRow;
+ head.currCol = startCol;
+ snakeBody.push_back(head);
+}
+
+void snake::move(char newHeadRow, char newHeadCol){
+
+ // Variables to hold the bodyPiece's X and Y positions on the grid
+ char prevRow, prevCol, tempRow, tempCol;
+
+ // Store the snake's head into the iterator
+ std::list<bodyPiece>::iterator it=snakeBody.begin();
+ // Save the head's current Row and Col position
+ prevRow = (*it).currRow;
+ prevCol = (*it).currCol;
+ // Update the head to the new Row and Col
+ (*it).currRow = newHeadRow;
+ (*it).currCol = newHeadCol;
+ // Check to see if we are at the tail with this while loop
+ while(it != snakeBody.end()){
+ // Move to the next bodyPiece
+ it++;
+ // Store the previous piece's location in the current piece
+ tempRow = (*it).currRow;
+ (*it).currRow = prevRow;
+ tempCol = (*it).currCol;
+ (*it).currCol = prevCol;
+ prevRow = tempRow;
+ prevCol = tempCol;
+ }
+}
+
+void snake::addPiece(){
+ snakeBody.push_back(bodyPiece());
+}
+
+food::food(char row, char col){
+ currRow = row;
+ currCol = col;
+}
+
+void food::moveFood(char row, char col){
+ currRow = row;
+ currCol = col;
+}
\ No newline at end of file