Completed Snake Program

Dependencies:   N5110 PinDetect PowerControl mbed

Fork of DocTest by Craig Evans

main.cpp

Committer:
MBirney
Date:
2015-04-14
Revision:
4:551dea241d0a
Parent:
3:478b090b7e1b
Child:
5:1bfc306466db

File content as of revision 4:551dea241d0a:

#include "mbed.h"
#include "N5110.h"
#include <vector> 
#include <cstddef>      // std::size_t
#include <valarray>  
#include <iostream>

//#include "PinDetect.h"

// change this to alter tolerance of joystick direction
#define DIRECTION_TOLERANCE 0.1
//        VCC,SCE,RST,D/C,MOSI,SCLK,LED
N5110 lcd(p7,p8,p9,p10,p11,p13,p21);
BusOut myleds(LED1,LED2,LED3,LED4);
InterruptIn startButton(p16);
InterruptIn resetButton(p17);

// connections for joystick
DigitalIn button(p18);
AnalogIn xPot(p19);
AnalogIn yPot(p20);
Serial serial(USBTX,USBRX);
int snake[84][47];
bool gamePlaying =0;

vector<int> snakeX (5);
vector<int> snakeY (5,27);




//snakeX[4]=21;

//int snakeX[4]={21,22,23,24}; //5 pixels start position 21 22 23 24 25
//int snakeY[4]={27,27,27,27}; //5 pixels y position     27 27 27 27 27 


void startingSnake(){
    
    for (int i=0; i<5; i++){
        lcd.setPixel(snakeX[i],snakeY[i]);
       // lcd.drawRect(snakeX[i],snakeY[i],1,1,1);
    }
    }
    


// timer to regularly read the joystick
Ticker pollJoystick;

Ticker startGame;


void Boundary(){

    
    for(int x = 0; x< 84; x++) {
        lcd.setPixel(x,0);
    }
    for(int x = 0; x< 84; x++) {
        lcd.setPixel(x,47);
    }
    for(int y = 0; y< 48; y++) {
        lcd.setPixel(0,y);
    }
    for(int y = 0; y< 48; y++) {
        lcd.setPixel(83,y);
    }
   
    lcd.refresh();
    

}
enum Difficulty {
    EASY,
    MEDIUM,
    HARD,
};

int gameSpeed;//change depending on difficulty selected

Difficulty currentDifficulty=EASY;

// create enumerated type (0,1,2,3 etc. for direction)
// could be extended for diagonals etc.
enum DirectionName {
    UP,
    DOWN,
    LEFT,
    RIGHT,
    CENTRE,
    UNKNOWN
};

// struct for Joystick
typedef struct JoyStick Joystick;
struct JoyStick {
    float x;    // current x value
    float x0;   // 'centred' x value
    float y;    // current y value
    float y0;   // 'centred' y value
    int button; // button state (assume pull-down used, so 1 = pressed, 0 = unpressed)
    DirectionName direction;  // current direction
};
// create struct variable
Joystick joystick;


DirectionName previousDirection =RIGHT;

// read default positions of the joystick to calibrate later readings
void calibrateJoystick()
{
    button.mode(PullDown);
    // must not move during calibration
    joystick.x0 = xPot;  // initial positions in the range 0.0 to 1.0 (0.5 if centred exactly)
    joystick.y0 = yPot;
}
void updateJoystick()
{
    // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred)
    joystick.x = xPot - joystick.x0;
    joystick.y = yPot - joystick.y0;
    // read button state
    joystick.button = button;

    // calculate direction depending on x,y values
    // tolerance allows a little lee-way in case joystick not exactly in the stated direction
    if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
        joystick.direction = CENTRE;
    } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
        joystick.direction = UP;
    } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
        joystick.direction = DOWN;
    } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
        joystick.direction = LEFT; // remember switched this with right
    } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
        joystick.direction = RIGHT;
    } else {
        joystick.direction = UNKNOWN;
    }
}


void updateSnakeArray(){
        
        if (joystick.direction==LEFT && previousDirection==RIGHT){
            joystick.direction=RIGHT;
            }
            
        if (joystick.direction==RIGHT && previousDirection==LEFT){
            joystick.direction=LEFT;
            }
            
            if (joystick.direction==UP && previousDirection==DOWN){
            joystick.direction=DOWN;
            }
            
            if (joystick.direction==DOWN && previousDirection==UP){
            joystick.direction=UP;
            }
            
             if (joystick.direction==UNKNOWN || joystick.direction==CENTRE){
                 joystick.direction= previousDirection;
            }
        
    
       lcd.clearPixel(snakeX[0],snakeY[0]);//delete tail 
    //  lcd.drawRect(snakeX[0],snakeY[0],1,1,2);
        
             for (int i =0; i<snakeX.size(); i++){   // shift elements 
                snakeX[i]=snakeX[i + 1]; // apart from head 
                snakeY[i]=snakeY[i+ 1];
                } 
                
                 switch(joystick.direction){
                      
                       case UP:snakeX[4]=snakeX[3];
                    snakeY[4]=snakeY[3]-1;
                    break;
                    
                      case DOWN:
                    snakeX[4]=snakeX[3];
                    snakeY[4]=snakeY[3]+1;
                    break;
                    
                      case LEFT:
                    snakeX[4]=snakeX[3]-1;
                    snakeY[4]=snakeY[3];
                    break;
                    
                      case RIGHT:
                    snakeX[4]=snakeX[3]+1;
                    snakeY[4]=snakeY[3];
                    break;
                    
                    case CENTRE:
                    snakeX[4]=snakeX[3]+1;
                    snakeY[4]=snakeY[3];
                    break;
                     case UNKNOWN:
                    snakeX[4]=snakeX[3]+1;
                    snakeY[4]=snakeY[3];
                    break;
                        
                    
                    }
                   
                 /* snakeX[0]=snakeX[1];
                snakeX[1]=snakeX[2];
                snakeX[2]=snakeX[3];
                snakeX[3]=snakeX[4];
                snakeY[0]=snakeY[1];
                snakeY[1]=snakeY[2];
                snakeY[2]=snakeY[3];
                snakeY[3]=snakeY[4];
                
                
               */
                  
}

void checkForCollision(){
    
    if (snakeX.back()==0 || snakeX.back()==83 || snakeY.back()==0 ||snakeY.back()==47){
        startGame.detach();
        lcd.clear();
        
        
        
        }
    
    
    }
void startButtonPressed(){
    gamePlaying=1;
    myleds=15;
    }
    
    void resetButtonPressed(){
    gamePlaying=0;
    myleds=0;
    }
void displaySplash()
{
    // these are default settings so not strictly needed
    lcd.inverseMode();      // normal colour mode
    lcd.setBrightness(0.5); // put LED backlight on 50%

    //Draw S
    lcd.drawRect(28,10,2,5,1);
    lcd.drawRect(15,10,15,2,1);
    lcd.drawRect(15,10,2,10,1);
    lcd.drawRect(15,20,15,2,1);
    lcd.drawRect(28,20,2,10,1);
    lcd.drawRect(15,28,15,2,1);
    lcd.drawRect(15,25,2,3,1);

    lcd.printString("NAKE ",34,3);
    lcd.printString("By M.Birney",10,5);
    lcd.drawRect(10,5,65,30,0);

    // need to refresh display after setting pixels

    lcd.refresh();
}



void easySelected() // display when easy is selected
{
    currentDifficulty=EASY;
    lcd.clear();
    lcd.printString("Please Select",2,0);
    lcd.printString("Difficulty:",2,1);
    lcd.printString("Easy",20,3);
    lcd.printString("Medium",20,4);
    lcd.printString("Hard",20,5);
    lcd.refresh();
    lcd.drawCircle(10,27,2,1);
    lcd.drawCircle(10,35,2,0);
    lcd.drawCircle(10,43,2,0);

    gameSpeed= 1.0;
}
void mediumSelected() // display when medium is selected
{
    currentDifficulty=MEDIUM;
    lcd.clear();
    lcd.printString("Please Select",2,0);
    lcd.printString("Difficulty:",2,1);
    lcd.printString("Easy",20,3);
    lcd.printString("Medium",20,4);
    lcd.printString("Hard",20,5);
    lcd.refresh();
    lcd.drawCircle(10,27,2,0);
    lcd.drawCircle(10,35,2,1);
    lcd.drawCircle(10,43,2,0);
    gameSpeed=1.0/4;
}

void hardSelected() // display when hard is selected
{
    currentDifficulty=HARD;
    lcd.clear();
    lcd.printString("Please Select",2,0);
    lcd.printString("Difficulty:",2,1);
    lcd.printString("Easy",20,3);
    lcd.printString("Medium",20,4);
    lcd.printString("Hard",20,5);
    lcd.refresh();
    lcd.drawCircle(10,27,2,0);
    lcd.drawCircle(10,35,2,0);
    lcd.drawCircle(10,43,2,1);
    gameSpeed=1.0/8;
}
void drawSnake() {  
 
    for(int x =10; x<15; x++) {
        lcd.setPixel(x,25);
       // lcd.drawRect(x,25,2,2,1);
        snake[x][25]=1;
    }
}


void checkSelectedDifficulty() {

    switch(currentDifficulty) {
        case EASY:

            switch (joystick.direction) {

                case UP:
                    hardSelected();
                    break;


                case DOWN:
                    mediumSelected();
                    break;
            }
            break;


        case MEDIUM:

            switch (joystick.direction) {

                case UP:
                    easySelected();
                    break;


                case DOWN:
                    hardSelected();
                    break;
            }



            break;
        case HARD:
            switch (joystick.direction) {

                case UP:
                    mediumSelected();
                    break;


                case DOWN:
                    easySelected();
                    break;
            }
            break;
    }
    wait(0.2);
}

int updateGameFlag=0;
void updateGameISR(){
    updateGameFlag=1;
    }

int main()
{

    // first need to initialise display
    lcd.init();
    
    displaySplash();
    wait(4);
    easySelected();
    joystick.direction=UNKNOWN;
    calibrateJoystick();  // get centred values of joystick
    pollJoystick.attach(&updateJoystick,1.0/5.0); 
    startGame.attach(&updateGameISR,gameSpeed); // read joystick 10 times per second
    startButton.mode(PullUp);
    startButton.rise(&startButtonPressed);
    resetButton.rise(&resetButtonPressed);
  //  startGame.attach(&startButtonPressed,10);
    
    while(1) {
        if (gamePlaying==0){
        checkSelectedDifficulty();
    }
    else if (gamePlaying==1){
     lcd.clear();
     lcd.normalMode();      // normal colour mode
     Boundary();
     snakeX[0]=21;
    snakeX[1]=22;
    snakeX[2]=23;
    snakeX[3]=24;
    snakeX[4]=25;
 pollJoystick.detach();
    startGame.attach(&updateGameISR,0.1);
     startingSnake();
         lcd.refresh();
     //init game start time back food
     while (1){
       if(updateGameFlag==1){
         //updateJoystick();
         updateGameFlag=0;
         updateJoystick();
         // lcd.clearPixel(snakeX[0],snakeY[0]);
          // updateSnakeDirection();
          updateSnakeArray();
          for (int i=0;i<snakeX.size();i++){
             // lcd.drawRect(snakeX[i],snakeY[i],1,1,1);
             lcd.setPixel(snakeX[i],snakeY[i]);
              }
              lcd.refresh(); 
              previousDirection=joystick.direction;  
              checkForCollision();      
    }
                  
         }
 
         }
     }
     
    

}