Snake game for a 5x5 LED matrix
Diff: main.cpp
- Revision:
- 2:9c075a0a6d4e
- Parent:
- 1:5fcb94bb03db
--- 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