Snake game for a 5x5 LED matrix

Dependencies:   MCP23S17 mbed

Committer:
dhamilton31
Date:
Thu Oct 17 22:09:22 2013 +0000
Revision:
2:9c075a0a6d4e
Parent:
1:5fcb94bb03db
Changed one stupid thing (cube) to (*cube) and it worked :P

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