Snake game for a 5x5 LED matrix
Revision 2:9c075a0a6d4e, committed 2013-10-17
- Comitter:
- dhamilton31
- Date:
- Thu Oct 17 22:09:22 2013 +0000
- Parent:
- 1:5fcb94bb03db
- Commit message:
- Changed one stupid thing (cube) to (*cube) and it worked :P
Changed in this revision
diff -r 5fcb94bb03db -r 9c075a0a6d4e ledCube.cpp --- a/ledCube.cpp Thu Oct 17 04:32:58 2013 +0000 +++ b/ledCube.cpp Thu Oct 17 22:09:22 2013 +0000 @@ -35,6 +35,7 @@ ledArray[4][2] = new DigitalOut(P042); ledArray[4][3] = new DigitalOut(P043); ledArray[4][4] = new DigitalOut(P044); + spiCurrentlyLit = 0; // Set all 8 Port A bits to output direction (*chip).direction(PORT_A, 0x00); // Set all 8 Port B bits to output direction @@ -48,7 +49,9 @@ if(ledArray[row][column] != NULL){ *ledArray[row][column] = 1; } + // This else handles cases where the LEDs are handled by the I/O extender else{ + //printf("before on-spiCurrentlyLit: %d turn on Row: %d Col: %d \n", spiCurrentlyLit, row, column); if( row == 1 && column == 3){ spiCurrentlyLit |= 0x01; } @@ -77,12 +80,15 @@ } } +// Turns off the LED at the specified row and column void ledCube::turnOffLed(char row, char column, char layer) { if(ledArray[row][column] != NULL){ *ledArray[row][column] = 0; } + // This else handles cases where the LEDs are handled by the I/O extender else{ + //printf("before: off-spiCurrentlyLit: %d turn off Row: %d Col: %d \n", spiCurrentlyLit, row, column); if( row == 1 && column == 3){ spiCurrentlyLit &= ~(0x01); } @@ -111,6 +117,7 @@ } } +// Blinks all the LEDs to indicate losing void ledCube::blink() { for(char i = 0; i < 5; i++) { @@ -118,10 +125,11 @@ turnOnLed(i,j,0); } } - wait(.3); + wait(.3); for(char i = 0; i < 5; i++) { for(char j = 0; j < 5; j++) { turnOffLed(i,j,0); } } + wait(.3); }
diff -r 5fcb94bb03db -r 9c075a0a6d4e ledCube.h --- a/ledCube.h Thu Oct 17 04:32:58 2013 +0000 +++ b/ledCube.h Thu Oct 17 22:09:22 2013 +0000 @@ -3,7 +3,9 @@ #ifndef LEDCUBE_H #define LEDCUBE_H - +/* + Class to represent the LED cube as a whole. Can light and select LEDs +*/ class ledCube { public: @@ -15,10 +17,10 @@ void turnOnLed(char row, char column, char layer); void turnOffLed(char row, char column, char layer); void blink(); + unsigned char spiCurrentlyLit; private: DigitalOut *lastLedLit; - int spiCurrentlyLit; }; #endif //LEDCUBE_H \ No newline at end of file
diff -r 5fcb94bb03db -r 9c075a0a6d4e main.cpp --- a/main.cpp Thu Oct 17 04:32:58 2013 +0000 +++ b/main.cpp Thu Oct 17 22:09:22 2013 +0000 @@ -1,30 +1,40 @@ +/* +* 5x5 LED Snake Game +* Author: Daniel Hamilton +* ECE 4180 Lab 3 +* +*/ + #include "mbed.h" #include "ledCube.h" #include "snake.h" #include "main.h" -snake mySnake(snakeStartRow,snakeStartCol); -food myFood(foodStartRow, foodStartCol); -ledCube cube; -AnalogIn joyVer(p19); -AnalogIn joyHor(p18); -DigitalIn select(p17); +snake mySnake(snakeStartRow,snakeStartCol); // Snake represents the coordinates making up the snake +food myFood(foodStartRow, foodStartCol); // food pellet the snake is trying to eat +ledCube cube; // Currently a square, but represents and controls the physical LEDs +AnalogIn joyVer(p19); // vertical analog joystick input pin +AnalogIn joyHor(p18); // Horizontal analog joystick input pin +DigitalIn select(p17); // Pushbutton on the joystick (currently unused) int main() { printf("Start\n"); - int snakeUpdateCounter = 0; - cube.turnOnLed(snakeStartRow, snakeStartCol, 0); - updateFoodLed(); + int snakeUpdateCounter = 0; // keeps track of when we should move Snake + cube.turnOnLed(snakeStartRow, snakeStartCol, 0); // Starts the snake at Row 0 Col 0 + updateFoodLed(); // Lights up the food LED printf("Setup Complete\n"); + bool gameover = false; // Is set to true when the game is over and keeps the LEDs blinking while(1) { // Update snake position if we are greater than the set movement speed if(snakeUpdateCounter++ >= mySnake.movementSpeed) { snakeUpdateCounter = 0; - if(mySnake.moveSnake(cube)) { + if(mySnake.moveSnake(&cube) || gameover) { + gameover = true; cube.blink(); } + // See if the snake is on the Food's LED if(checkForSnakeEating()) { myFood.moveFood(rand() % 5, rand() % 5); updateFoodLed(); @@ -36,16 +46,23 @@ } +// Return true if the snake is on a food, false otherwise bool checkForSnakeEating() { return mySnake.isEating(myFood.currRow, myFood.currCol); } +// Update the food's loction void updateFoodLed() { cube.turnOnLed(myFood.currRow, myFood.currCol, 0); + printf("FOOD: Row: %d Col: %d\n", myFood.currRow, myFood.currCol); + if(mySnake.movementSpeed > 0){ + mySnake.movementSpeed--; + } } +// Updates the direction the snake is traveling based on the analog controller's input void updateDirectionInput(){ float verValue, horValue; int pushed; @@ -55,18 +72,18 @@ pushed = select; if(horValue < .4){ mySnake.movementDirection = Left; - printf("Left\n"); + //printf("Left\n"); } else if(horValue > .6){ mySnake.movementDirection = Right; - printf("Right\n"); + //printf("Right\n"); } if(verValue < .4){ mySnake.movementDirection = Down; - printf("Down\n"); + //printf("Down\n"); } else if(verValue > .6){ mySnake.movementDirection = Up; - printf("Up\n"); + //printf("Up\n"); } } \ No newline at end of file
diff -r 5fcb94bb03db -r 9c075a0a6d4e mbed.bld --- a/mbed.bld Thu Oct 17 04:32:58 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/a9913a65894f \ No newline at end of file
diff -r 5fcb94bb03db -r 9c075a0a6d4e mbed.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.lib Thu Oct 17 22:09:22 2013 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/#a9913a65894f
diff -r 5fcb94bb03db -r 9c075a0a6d4e snake.cpp --- a/snake.cpp Thu Oct 17 04:32:58 2013 +0000 +++ b/snake.cpp Thu Oct 17 22:09:22 2013 +0000 @@ -4,7 +4,8 @@ #include "ledCube.h" // Constructor -snake::snake(char startRow, char startCol){ +snake::snake(char startRow, char startCol) +{ bodyPiece head = bodyPiece(); head.currRow = startRow; head.currCol = startCol; @@ -15,8 +16,9 @@ } // Movement method for the snake, will update LEDs and snake's body -char snake::move(char newHeadRow, char newHeadCol, ledCube cube){ - +char snake::move(char newHeadRow, char newHeadCol, ledCube *cube) +{ + // Variables to hold the bodyPiece's X and Y positions on the grid char prevRow, prevCol, tempRow, tempCol; @@ -24,15 +26,15 @@ std::list<bodyPiece>::iterator it=snakeBody.begin(); // Save the head's current Row and Col position prevRow = (*it).currRow; - prevCol = (*it).currCol; + prevCol = (*it).currCol; // Update the head to the new Row and Col (*it).currRow = newHeadRow; (*it).currCol = newHeadCol; - cube.turnOffLed(prevRow, prevCol, 0); - cube.turnOnLed(newHeadRow, newHeadCol, 0); + (*cube).turnOffLed(prevRow, prevCol, 0); + (*cube).turnOnLed((*it).currRow, (*it).currCol, 0); char bodyCount = 1; // Check to see if we are at the tail with this while loop - while(bodyCount < bodySize){ + while(bodyCount < bodySize) { // Move to the next bodyPiece it++; bodyCount++; @@ -44,85 +46,84 @@ prevRow = tempRow; prevCol = tempCol; // Check to see if the head has collided with this bodyPiece - if(snakeBody.front().currRow == prevRow && snakeBody.front().currCol == prevCol){ + if(snakeBody.front().currRow == prevRow && snakeBody.front().currCol == prevCol) { return 1; } - if(prevRow < 5 && prevCol < 5){ - cube.turnOffLed(prevRow, prevCol, 0); + if(prevRow < 5 && prevCol < 5) { + (*cube).turnOffLed(prevRow, prevCol, 0); } - - cube.turnOnLed((*it).currRow, (*it).currCol, 0); + (*cube).turnOnLed((*it).currRow, (*it).currCol, 0); } return 0; } // Checks the current movement direction and decided where to move the snake head next // Will return 1 if out of bounds or if the head has hit a bodyPiece, 0 if still in the boundaries -char snake::moveSnake(ledCube cube){ +char snake::moveSnake(ledCube *cube) +{ bodyPiece head = snakeBody.front(); char ret = 0; - switch(movementDirection){ - case Down: - if(head.currCol-1 < 0){ - ret = 1; - } - else{ - ret = move(head.currRow, head.currCol-1, cube); - } - break; - case Up: - if(head.currCol+1 > NUM_COLS){ - ret = 1; - } - else{ - ret = move(head.currRow, head.currCol+1, cube); - } - break; - case Left: - if(head.currRow-1 < 0){ - ret = 1; - } - else{ - ret = move(head.currRow-1, head.currCol, cube); - } - break; - case Right: - if(head.currRow+1 > NUM_ROWS){ - ret = 1; - } - else{ - ret = move(head.currRow+1, head.currCol, cube); - } + switch(movementDirection) { + case Down: + if(head.currCol+1 > NUM_COLS) { + ret = 1; + } else { + ret = move(head.currRow, head.currCol+1, cube); + } + break; + case Up: + if(head.currCol-1 < 0) { + ret = 1; + } else { + ret = move(head.currRow, head.currCol-1, cube); + } + break; + case Left: + if(head.currRow-1 < 0) { + ret = 1; + } else { + ret = move(head.currRow-1, head.currCol, cube); + } + break; + case Right: + if(head.currRow+1 > NUM_ROWS) { + ret = 1; + } else { + ret = move(head.currRow+1, head.currCol, cube); + } } return ret; } // Adds a new piece on to snake's tail -void snake::addPiece(){ +void snake::addPiece() +{ bodyPiece b; snakeBody.push_back(b); bodySize++; printf("piece added + bodySize: %d\n", bodySize); } -bool snake::isEating(char foodRow, char foodCol){ - if(snakeBody.front().currRow == foodRow && snakeBody.front().currCol == foodCol){ +bool snake::isEating(char foodRow, char foodCol) +{ + if(snakeBody.front().currRow == foodRow && snakeBody.front().currCol == foodCol) { addPiece(); return 1; - } - else{ + } else { return 0; } } // Constructor for the food class -food::food(char row, char col){ +food::food(char row, char col) +{ currRow = row; currCol = col; } // Moves food to a new row and column -void food::moveFood(char row, char col){ +void food::moveFood(char row, char col) +{ currRow = row; currCol = col; }
diff -r 5fcb94bb03db -r 9c075a0a6d4e snake.h --- a/snake.h Thu Oct 17 04:32:58 2013 +0000 +++ b/snake.h Thu Oct 17 22:09:22 2013 +0000 @@ -7,7 +7,7 @@ #define SNAKE_H // Macros -#define START_DIRECTION Up +#define START_DIRECTION Down #define START_SPEED 10 // Zero indexed number of columns and rows #define NUM_COLS 4 @@ -31,13 +31,13 @@ std::list<bodyPiece> snakeBody; // Function Prototypes - char moveSnake(ledCube cube); // Moves the snake by one in the direction of movementDirection + char moveSnake(ledCube *cube); // Moves the snake by one in the direction of movementDirection // Will return 1 if out of bounds or if the head has hit a bodyPiece, 0 if still in the boundaries void addPiece(); // Adds a bodyPiece to the tail bool isEating(char foodRow, char foodCol); private: - char move(char newHeadRow, char newHeadCol, ledCube cube); + char move(char newHeadRow, char newHeadCol, ledCube *cube); }; class food