Snake game for a 5x5 LED matrix

Dependencies:   MCP23S17 mbed

Committer:
dhamilton31
Date:
Wed Oct 09 16:23:20 2013 +0000
Revision:
0:dc906408980e
Child:
1:5fcb94bb03db
Snake movement completed, along with options to turn LEDs on and off with ledCube class.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dhamilton31 0:dc906408980e 1 #include "snake.h"
dhamilton31 0:dc906408980e 2 #include "bodyPiece.h"
dhamilton31 0:dc906408980e 3 #include <list>
dhamilton31 0:dc906408980e 4
dhamilton31 0:dc906408980e 5 snake::snake(char startRow, char startCol){
dhamilton31 0:dc906408980e 6 bodyPiece head = bodyPiece();
dhamilton31 0:dc906408980e 7 head.currRow = startRow;
dhamilton31 0:dc906408980e 8 head.currCol = startCol;
dhamilton31 0:dc906408980e 9 snakeBody.push_back(head);
dhamilton31 0:dc906408980e 10 }
dhamilton31 0:dc906408980e 11
dhamilton31 0:dc906408980e 12 void snake::move(char newHeadRow, char newHeadCol){
dhamilton31 0:dc906408980e 13
dhamilton31 0:dc906408980e 14 // Variables to hold the bodyPiece's X and Y positions on the grid
dhamilton31 0:dc906408980e 15 char prevRow, prevCol, tempRow, tempCol;
dhamilton31 0:dc906408980e 16
dhamilton31 0:dc906408980e 17 // Store the snake's head into the iterator
dhamilton31 0:dc906408980e 18 std::list<bodyPiece>::iterator it=snakeBody.begin();
dhamilton31 0:dc906408980e 19 // Save the head's current Row and Col position
dhamilton31 0:dc906408980e 20 prevRow = (*it).currRow;
dhamilton31 0:dc906408980e 21 prevCol = (*it).currCol;
dhamilton31 0:dc906408980e 22 // Update the head to the new Row and Col
dhamilton31 0:dc906408980e 23 (*it).currRow = newHeadRow;
dhamilton31 0:dc906408980e 24 (*it).currCol = newHeadCol;
dhamilton31 0:dc906408980e 25 // Check to see if we are at the tail with this while loop
dhamilton31 0:dc906408980e 26 while(it != snakeBody.end()){
dhamilton31 0:dc906408980e 27 // Move to the next bodyPiece
dhamilton31 0:dc906408980e 28 it++;
dhamilton31 0:dc906408980e 29 // Store the previous piece's location in the current piece
dhamilton31 0:dc906408980e 30 tempRow = (*it).currRow;
dhamilton31 0:dc906408980e 31 (*it).currRow = prevRow;
dhamilton31 0:dc906408980e 32 tempCol = (*it).currCol;
dhamilton31 0:dc906408980e 33 (*it).currCol = prevCol;
dhamilton31 0:dc906408980e 34 prevRow = tempRow;
dhamilton31 0:dc906408980e 35 prevCol = tempCol;
dhamilton31 0:dc906408980e 36 }
dhamilton31 0:dc906408980e 37 }
dhamilton31 0:dc906408980e 38
dhamilton31 0:dc906408980e 39 void snake::addPiece(){
dhamilton31 0:dc906408980e 40 snakeBody.push_back(bodyPiece());
dhamilton31 0:dc906408980e 41 }
dhamilton31 0:dc906408980e 42
dhamilton31 0:dc906408980e 43 food::food(char row, char col){
dhamilton31 0:dc906408980e 44 currRow = row;
dhamilton31 0:dc906408980e 45 currCol = col;
dhamilton31 0:dc906408980e 46 }
dhamilton31 0:dc906408980e 47
dhamilton31 0:dc906408980e 48 void food::moveFood(char row, char col){
dhamilton31 0:dc906408980e 49 currRow = row;
dhamilton31 0:dc906408980e 50 currCol = col;
dhamilton31 0:dc906408980e 51 }