Snake game for a 5x5 LED matrix

Dependencies:   MCP23S17 mbed

main.cpp

Committer:
dhamilton31
Date:
2013-10-17
Revision:
1:5fcb94bb03db
Parent:
0:dc906408980e
Child:
2:9c075a0a6d4e

File content as of revision 1:5fcb94bb03db:

#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);

int main()
{
    printf("Start\n");
    int snakeUpdateCounter = 0;
    cube.turnOnLed(snakeStartRow, snakeStartCol, 0);
    updateFoodLed();
    printf("Setup Complete\n");

    while(1) {
        // Update snake position if we are greater than the set movement speed
        if(snakeUpdateCounter++ >= mySnake.movementSpeed) {
            snakeUpdateCounter = 0;
            if(mySnake.moveSnake(cube)) {
                cube.blink();
            }
            if(checkForSnakeEating()) {
                myFood.moveFood(rand() % 5, rand() % 5);
                updateFoodLed();
            }
        }
        updateDirectionInput();
        wait(.1);
    }
    
}

bool checkForSnakeEating()
{
    return mySnake.isEating(myFood.currRow, myFood.currCol);
}

void updateFoodLed()
{
    cube.turnOnLed(myFood.currRow, myFood.currCol, 0);
}

void updateDirectionInput(){
   float verValue, horValue;
    int pushed;
    select.mode(PullUp);
        verValue = joyVer;
        horValue = joyHor;
        pushed = select;
        if(horValue < .4){
            mySnake.movementDirection = Left;
            printf("Left\n");
        }
        else if(horValue > .6){
            mySnake.movementDirection = Right;
            printf("Right\n");
        }
        if(verValue < .4){
            mySnake.movementDirection = Down;
            printf("Down\n");
        }
        else if(verValue > .6){
            mySnake.movementDirection = Up;
            printf("Up\n");
        }
}