Snake game for a 5x5 LED matrix

Dependencies:   MCP23S17 mbed

Committer:
dhamilton31
Date:
Thu Oct 17 04:32:58 2013 +0000
Revision:
1:5fcb94bb03db
Parent:
0:dc906408980e
Child:
2:9c075a0a6d4e
Changes to just about everything

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 1:5fcb94bb03db 4 #include "ledCube.h"
dhamilton31 0:dc906408980e 5
dhamilton31 1:5fcb94bb03db 6 // Constructor
dhamilton31 0:dc906408980e 7 snake::snake(char startRow, char startCol){
dhamilton31 0:dc906408980e 8 bodyPiece head = bodyPiece();
dhamilton31 0:dc906408980e 9 head.currRow = startRow;
dhamilton31 0:dc906408980e 10 head.currCol = startCol;
dhamilton31 0:dc906408980e 11 snakeBody.push_back(head);
dhamilton31 1:5fcb94bb03db 12 bodySize = 1;
dhamilton31 1:5fcb94bb03db 13 movementDirection = START_DIRECTION;
dhamilton31 1:5fcb94bb03db 14 movementSpeed = START_SPEED;
dhamilton31 0:dc906408980e 15 }
dhamilton31 0:dc906408980e 16
dhamilton31 1:5fcb94bb03db 17 // Movement method for the snake, will update LEDs and snake's body
dhamilton31 1:5fcb94bb03db 18 char snake::move(char newHeadRow, char newHeadCol, ledCube cube){
dhamilton31 0:dc906408980e 19
dhamilton31 0:dc906408980e 20 // Variables to hold the bodyPiece's X and Y positions on the grid
dhamilton31 0:dc906408980e 21 char prevRow, prevCol, tempRow, tempCol;
dhamilton31 0:dc906408980e 22
dhamilton31 0:dc906408980e 23 // Store the snake's head into the iterator
dhamilton31 0:dc906408980e 24 std::list<bodyPiece>::iterator it=snakeBody.begin();
dhamilton31 0:dc906408980e 25 // Save the head's current Row and Col position
dhamilton31 0:dc906408980e 26 prevRow = (*it).currRow;
dhamilton31 0:dc906408980e 27 prevCol = (*it).currCol;
dhamilton31 0:dc906408980e 28 // Update the head to the new Row and Col
dhamilton31 0:dc906408980e 29 (*it).currRow = newHeadRow;
dhamilton31 0:dc906408980e 30 (*it).currCol = newHeadCol;
dhamilton31 1:5fcb94bb03db 31 cube.turnOffLed(prevRow, prevCol, 0);
dhamilton31 1:5fcb94bb03db 32 cube.turnOnLed(newHeadRow, newHeadCol, 0);
dhamilton31 1:5fcb94bb03db 33 char bodyCount = 1;
dhamilton31 0:dc906408980e 34 // Check to see if we are at the tail with this while loop
dhamilton31 1:5fcb94bb03db 35 while(bodyCount < bodySize){
dhamilton31 0:dc906408980e 36 // Move to the next bodyPiece
dhamilton31 0:dc906408980e 37 it++;
dhamilton31 1:5fcb94bb03db 38 bodyCount++;
dhamilton31 0:dc906408980e 39 // Store the previous piece's location in the current piece
dhamilton31 0:dc906408980e 40 tempRow = (*it).currRow;
dhamilton31 0:dc906408980e 41 (*it).currRow = prevRow;
dhamilton31 0:dc906408980e 42 tempCol = (*it).currCol;
dhamilton31 0:dc906408980e 43 (*it).currCol = prevCol;
dhamilton31 0:dc906408980e 44 prevRow = tempRow;
dhamilton31 0:dc906408980e 45 prevCol = tempCol;
dhamilton31 1:5fcb94bb03db 46 // Check to see if the head has collided with this bodyPiece
dhamilton31 1:5fcb94bb03db 47 if(snakeBody.front().currRow == prevRow && snakeBody.front().currCol == prevCol){
dhamilton31 1:5fcb94bb03db 48 return 1;
dhamilton31 1:5fcb94bb03db 49 }
dhamilton31 1:5fcb94bb03db 50 if(prevRow < 5 && prevCol < 5){
dhamilton31 1:5fcb94bb03db 51 cube.turnOffLed(prevRow, prevCol, 0);
dhamilton31 1:5fcb94bb03db 52 }
dhamilton31 1:5fcb94bb03db 53
dhamilton31 1:5fcb94bb03db 54 cube.turnOnLed((*it).currRow, (*it).currCol, 0);
dhamilton31 1:5fcb94bb03db 55 }
dhamilton31 1:5fcb94bb03db 56 return 0;
dhamilton31 1:5fcb94bb03db 57 }
dhamilton31 1:5fcb94bb03db 58
dhamilton31 1:5fcb94bb03db 59 // Checks the current movement direction and decided where to move the snake head next
dhamilton31 1:5fcb94bb03db 60 // Will return 1 if out of bounds or if the head has hit a bodyPiece, 0 if still in the boundaries
dhamilton31 1:5fcb94bb03db 61 char snake::moveSnake(ledCube cube){
dhamilton31 1:5fcb94bb03db 62 bodyPiece head = snakeBody.front();
dhamilton31 1:5fcb94bb03db 63 char ret = 0;
dhamilton31 1:5fcb94bb03db 64 switch(movementDirection){
dhamilton31 1:5fcb94bb03db 65 case Down:
dhamilton31 1:5fcb94bb03db 66 if(head.currCol-1 < 0){
dhamilton31 1:5fcb94bb03db 67 ret = 1;
dhamilton31 1:5fcb94bb03db 68 }
dhamilton31 1:5fcb94bb03db 69 else{
dhamilton31 1:5fcb94bb03db 70 ret = move(head.currRow, head.currCol-1, cube);
dhamilton31 1:5fcb94bb03db 71 }
dhamilton31 1:5fcb94bb03db 72 break;
dhamilton31 1:5fcb94bb03db 73 case Up:
dhamilton31 1:5fcb94bb03db 74 if(head.currCol+1 > NUM_COLS){
dhamilton31 1:5fcb94bb03db 75 ret = 1;
dhamilton31 1:5fcb94bb03db 76 }
dhamilton31 1:5fcb94bb03db 77 else{
dhamilton31 1:5fcb94bb03db 78 ret = move(head.currRow, head.currCol+1, cube);
dhamilton31 1:5fcb94bb03db 79 }
dhamilton31 1:5fcb94bb03db 80 break;
dhamilton31 1:5fcb94bb03db 81 case Left:
dhamilton31 1:5fcb94bb03db 82 if(head.currRow-1 < 0){
dhamilton31 1:5fcb94bb03db 83 ret = 1;
dhamilton31 1:5fcb94bb03db 84 }
dhamilton31 1:5fcb94bb03db 85 else{
dhamilton31 1:5fcb94bb03db 86 ret = move(head.currRow-1, head.currCol, cube);
dhamilton31 1:5fcb94bb03db 87 }
dhamilton31 1:5fcb94bb03db 88 break;
dhamilton31 1:5fcb94bb03db 89 case Right:
dhamilton31 1:5fcb94bb03db 90 if(head.currRow+1 > NUM_ROWS){
dhamilton31 1:5fcb94bb03db 91 ret = 1;
dhamilton31 1:5fcb94bb03db 92 }
dhamilton31 1:5fcb94bb03db 93 else{
dhamilton31 1:5fcb94bb03db 94 ret = move(head.currRow+1, head.currCol, cube);
dhamilton31 1:5fcb94bb03db 95 }
dhamilton31 1:5fcb94bb03db 96 }
dhamilton31 1:5fcb94bb03db 97 return ret;
dhamilton31 1:5fcb94bb03db 98 }
dhamilton31 1:5fcb94bb03db 99
dhamilton31 1:5fcb94bb03db 100 // Adds a new piece on to snake's tail
dhamilton31 1:5fcb94bb03db 101 void snake::addPiece(){
dhamilton31 1:5fcb94bb03db 102 bodyPiece b;
dhamilton31 1:5fcb94bb03db 103 snakeBody.push_back(b);
dhamilton31 1:5fcb94bb03db 104 bodySize++;
dhamilton31 1:5fcb94bb03db 105 printf("piece added + bodySize: %d\n", bodySize);
dhamilton31 1:5fcb94bb03db 106 }
dhamilton31 1:5fcb94bb03db 107
dhamilton31 1:5fcb94bb03db 108 bool snake::isEating(char foodRow, char foodCol){
dhamilton31 1:5fcb94bb03db 109 if(snakeBody.front().currRow == foodRow && snakeBody.front().currCol == foodCol){
dhamilton31 1:5fcb94bb03db 110 addPiece();
dhamilton31 1:5fcb94bb03db 111 return 1;
dhamilton31 1:5fcb94bb03db 112 }
dhamilton31 1:5fcb94bb03db 113 else{
dhamilton31 1:5fcb94bb03db 114 return 0;
dhamilton31 0:dc906408980e 115 }
dhamilton31 0:dc906408980e 116 }
dhamilton31 0:dc906408980e 117
dhamilton31 1:5fcb94bb03db 118 // Constructor for the food class
dhamilton31 0:dc906408980e 119 food::food(char row, char col){
dhamilton31 0:dc906408980e 120 currRow = row;
dhamilton31 0:dc906408980e 121 currCol = col;
dhamilton31 0:dc906408980e 122 }
dhamilton31 0:dc906408980e 123
dhamilton31 1:5fcb94bb03db 124 // Moves food to a new row and column
dhamilton31 0:dc906408980e 125 void food::moveFood(char row, char col){
dhamilton31 0:dc906408980e 126 currRow = row;
dhamilton31 0:dc906408980e 127 currCol = col;
dhamilton31 1:5fcb94bb03db 128 }
dhamilton31 1:5fcb94bb03db 129