Snake Game

Dependencies:   N5110 mbed

Fork of ProjectSnake by William Smith

Revision:
2:3389133a2223
Parent:
1:44cc07feab7e
--- a/main.cpp	Wed May 04 18:24:37 2016 +0000
+++ b/main.cpp	Thu May 05 13:37:52 2016 +0000
@@ -1,139 +1,35 @@
-#include "mbed.h"
-#include "N5110.h"
-#define DIRECTION_TOLERANCE 0.05// change this to alter tolerance of joystick direction
-
-DigitalIn button(PTB11);// connections for joystick
-AnalogIn xPot(PTB2);
-AnalogIn yPot(PTB3);
-Ticker pollJoystick;// timer to regularly read the joystick
-// Serial for debug
-Serial serial(USBTX,USBRX);// Serial for debug
-// create enumerated type (0,1,2,3 etc. for direction)
-// could be extended for diagonals etc.
-enum DirectionName {
-    UP,
-    DOWN,
-    LEFT,
-    RIGHT,
-    CENTRE,
-    UNKNOWN
-};
-typedef struct JoyStick Joystick;// struct for 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
-};
-Joystick joystick;// create struct variable
-int printFlag = 0;
 
-//        VCC,    SCE,    RST,    D/C,    MOSI,   SCLK,   LED
-N5110 lcd( PTE26,     PTA0,   PTC4,   PTD0,   PTD2,   PTD1,   PTC3);
-DigitalIn buttonA(PTB18);
-int ni = 84;
-int x = 0;
-int x1 = 0;
-int x2 = 0;
-int x3 = 0;
-int x4 = 0;
-int x5 = 0;
-int x6 = 0;
-int x7 = 0;
-int x8 = 0;
-int x9 = 0;
-int x10 = 0;
-int x11 = 0;
-int x12 = 0;
-int x13 = 0;
-int x14 = 0;
-int x15 = 0;
-int x16 = 0;
-int x17 = 0;
-int x18 = 0;
-int x19 = 0;
-int x20 = 0;
-int x21 = 0;
-int x22 = 0;
-int x23 = 0;
-int x24 = 0;
-int y = 0;
-int y1 = 1;
-int y2 = 2;
-int y3 = 3;
-int y4 = 4;
-int y5 = 5;
-int y6 = 6;
-int y7 = 7;
-int y8 = 8;
-int y9 = 9;
-int y10 = 10;
-int y11 = 11;
-int y12 = 12;
-int y13 = 13;
-int y14 = 14;
-int y15 = 15;
-int y16 = 16;
-int y17 = 17;
-int y18 = 18;
-int y19 = 19;
-int y20 = 20;
-int y21 = 21;
-int y22 = 22;
-int y23 = 23;
-int y24 = 24;
-int change = 1;
-int n[84][48];
-int array[48][84];
-int nj = 48;
-int cells[84][48];
-int Score = 0;
-int randX;
-int randY;
-char str[64];
-int pause=0;
+/**@file main.cpp
+@brief C plus plus file containing functions prototypes,defines and global variables.
+@author William J Smith
+@date May 2016
+*/
 
-void Menu();  //Start screen
-void Welcomescreen(); //Prints Welcome to snake on screen
-void clearCells(); //Clears Screen
-void checkerBoard(); //produces checkers before game begins
-void DrawArray(); //Sets the pixel depending on whether 0 or higher than 1
-void ClearArray();//Checks over each individual pixel and if its 0 it will clear
-void GenerateFood();//Generates food at random places
-void MakeSnake(); //sets the pixels for the snake
-void SnakeGame(); // Game Rules
-void MoveSnake(); //moving the snake using the joy stick
-void calibrateJoystick();
-void updateJoystick();
-void SnakeGrowing();
-void GameOver();//Game ends
-void drawLine();
-void calibrateJoystick();
-void updateJoystick();
+#include "main.h"
 
 int main()
 {
-    buttonA.mode(PullDown);
+    buttonA.mode(PullDown);//set to pull down mode to enable use of button on PCB
     calibrateJoystick();  // get centred values of joystick
     pollJoystick.attach(&updateJoystick,1.0/10.0);  // read joystick 10 times per second
-    calibrateJoystick();
-    updateJoystick();
-    lcd.init();
-    Welcomescreen();
-    lcd.clear();
-    GenerateFood();
-    DrawArray();
-    Menu();
+    calibrateJoystick();//calibrate joystick prior to game beginning 
+    updateJoystick();//update joystick to reduce bugs
+    lcd.init();//enables screen 
+    Welcomescreen();//shows welcoming message when starting up 
+    lcd.clear();//clears screen 
+
+    GenerateFood();//Randomly generates food at the beginning of the game 
+    DrawArray();//draws food array 
+    Menu();//enables user to press start
     while(1) {
 
-        MakeSnake();
-        ClearArray();
-        DrawArray();
+        MakeSnake();//creates inital SNAKE
+        ClearArray();//clears all pixels accept food and snake 
+        DrawArray();//draws pixels for food and snake 
+
     }
 }
-//menu screen
+//menu screen show a mesdsage telling the user to press the button on the PCB in order to start the game
 void Menu()
 {
     // first need to initialise display
@@ -145,7 +41,7 @@
     wait(0.3);
 
     while(1) {
-        if (buttonA==1) {
+        if (buttonA==1) {  // whenm the button is pressed a checkerboard effect is created and the game will start 
             lcd.clear();
             checkerBoard();
             wait(0.5);
@@ -155,16 +51,17 @@
             while(1) {
 
                 wait(0.005);
-                SnakeGame();
+                SnakeGame(); //Snake rules implemented 
+
                 if (buttonA==1) {
                     pause=0;
                     Menu();
                 }
 
-                if (button==1) {
+                if (button==1) { //if at any time in the game the joystick button is pressed then the game will pause and show the score in the centre
                     pause=0;
                     lcd.clear();
-                    sprintf(str,"%d",Score);
+                    sprintf(str,"%d",Score); //enables string number to be printed on the screen 
                     lcd.printString(str,37,2);
                     wait(1);
                 }
@@ -172,7 +69,7 @@
         }
     }
 }
-
+//void displays a welcome message "Welcome to Snake" when the device is turned on
 void Welcomescreen()   // Display welcome screen
 {
     lcd.printString("Welcome",20,1); // display Welcome
@@ -182,35 +79,35 @@
     lcd.printString("SNAKE",27,5);  // display Snake
     wait(1);
 }
-
+//Void produces checkerboard effect to make the game look like it is loading 
 void checkerBoard()
 {
 //
-    for (int i = 0; i < ni ; i+=2) {
+    for (int i = 0; i < ni ; i+=2) {//checks array values and sets every other pixel as 1
         for (int j = 0; j < nj ; j+=2) {
             lcd.setPixel(i,j);
         }
     }
     lcd.refresh();
 }
-
+//void enables the pixels that are set within the make snake void and the generate food void to be set as 1
 void DrawArray()
 {
-    for(int x=0; x<84; x++) {
+    for(int x=0; x<84; x++) { //checks all of the array[x][y], if any of them are named as the snake or food it will set pixel 
         for (int y=0; y<48; y++) {
             if(array[y][x]>=1) {
                 lcd.setPixel(x,y);
             } else {
-                lcd.clearPixel(x,y);
+                lcd.clearPixel(x,y); //Clears any pixels that may of previously been set to 1 
             }
         }
     }
     lcd.refresh();
 }
-
+//void to make sure there are no left over pixels when void DrawArray is initalised 
 void ClearArray()
 {
-    for(int x=0; x<84; x++) {
+    for(int x=0; x<84; x++) {//checks all of the array[x][y], if any of them are NOT named as the snake or food it will set pixel 
         for(int y=0; y<48; y++) {
             if (array[y][x]==0) {
                 lcd.clearPixel(x,y);
@@ -221,13 +118,14 @@
     }
     lcd.refresh();
 }
-
+//void to calibrate joystick so that the readings taken by the mbed are correct
 void calibrateJoystick()// read default positions of the joystick to calibrate later readings
 {
     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 so that when calibration is completed updates any changes to its position 
 void updateJoystick()
 {
     joystick.x = xPot - joystick.x0;// read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred)
@@ -248,17 +146,18 @@
     }
     printFlag = 1;// set flag for printing
 }
-
+//void to generate food from a random array that is created. this array is smaller so that the food is not direcvtly next to the wall on the screen 
 void GenerateFood()
 {
-    randX = rand() %83; //make random number between 0 - 84
-    randY = rand() %47; //make random number between 0 - 48
+    randX = (rand() %80)+2; //make random number between 2-82
+    randY = (rand() %43)+2; //make random number between 2 - 45 
     while(array[randY][randX]) {
-        randX= rand() %83;
-        randY= rand() %47;
+        randX= (rand() %80)+2;
+        randY= (rand() %44)+2;
     }
     array[randY][randX]= 5;
 }
+//Void to set the individual starting pixels of the snake and also shows the correct location of the pixels as the snake grows 
 void MakeSnake()
 {
     array[y+36][x+5]= 1;
@@ -274,7 +173,7 @@
     array[y10+36][x10+5]= 0;
     array[y11+36][x11+5]= 0;
     array[y12+36][x12+5]= 0;
-    array[y13+36][x13+5]= 0;
+    array[y13+36][x13+5]= 0;          //SETS POSITION OF SNAKE ORGINALLY 
     array[y14+36][x14+5]= 0;
     array[y15+36][x15+5]= 0;
     array[y16+36][x16+5]= 0;
@@ -287,23 +186,34 @@
     array[y23+36][x23+5]= 0;
     array[y24+36][x24+5]= 0;
 }
-
+//Void that actually runs throuhg the ruls of snake 
 void SnakeGame()
 {
-    MakeSnake();
-    ClearArray();
+    MakeSnake(); // SNAKES CREATED 
+    ClearArray(); //CLEARS EVERYTHING BAR SNAKE AND FOOD 
     DrawArray();
-    SnakeGrowing();
+    SnakeGrowing(); //SNAKE GROWS AS EATS FOOD 
+    lcd.drawRect(0,0,83,46,0); //Draws a rectangle around the outside of the snake game in order to creat a WALL
+    lcd.refresh();
     if(pause==0) {
-        MoveSnake();
+        MoveSnake(); //SNAKES MOVES AROUND
+        EndGame(); //When snake hits itself or wall it ENDS GAME 
+        Winner(); //When maximum points are scored shows message saying YOU WINNN
     }
     if(y+36==randY && x+5==randX) {
-        GenerateFood();
+        GenerateFood(); // When food is eaten create new food 
+      
 
-        Score= Score+10;
+        Score= Score+10; //When food is eaten add 10 onto the score
+
+    }
+    if(y+36==randY && x+5==randX) { 
+        LED=!LED; //switch on when food is eaten
     }
 
+
 }
+//void that enables snake to move from pixel to pixel whilst following the head 
 void MoveSnake()
 {
     array[y+36][x+5]= 0;
@@ -328,7 +238,7 @@
 
     array[y10+36][x10+5]= 0;
 
-    array[y11+36][x11+5]= 0;
+    array[y11+36][x11+5]= 0;         //DIFFERENT VALUES OF SNAKE BODY FOLLOWING THE SNAKE HEAD
 
     array[y12+36][x12+5]= 0;
 
@@ -367,7 +277,7 @@
     x20=x19;
     y20=y19;
     x19=x18;
-    y19=y18;
+    y19=y18;       //MAKES THE PIXELS FOLLOWING 
     x18=x17;
     y18=y17;
     x17=x16;
@@ -382,7 +292,7 @@
     y13=y12;
     x12=x11;
     y12=y11;
-    x11=x10;
+    x11=x10;  
     y11=y10;
     x10=x9;
     y10=y9;
@@ -404,136 +314,108 @@
     y2=y1;
     x1 = x;
     y1 = y;
-    if (joystick.direction == CENTRE) {
-        //lcd.printString("CENTRE",20,1); // display welcome
-        wait(0.05);
-    } else if (joystick.direction == UP) {
-        //lcd.printString("UP",20,1); // display welcome
+    if (joystick.direction == CENTRE) { //when joystick is at the centre the snake stays the same
+
         wait(0.05);
-        //y--;
+    } else if (joystick.direction == UP) { //when joystick is in the up position it minus the y value from the array 
+
+        wait(0.05);
+        //y--;                           
         change = 1;
-        if
-    } else if (joystick.direction == DOWN) {
-        //lcd.printString("DOWN",20,1); // display welcome
+
+    } else if (joystick.direction == DOWN) {//when joystick is in the up position it adds the y value from the array
         wait(0.05);
         change = 2;
         //y++;
-    } else if (joystick.direction == LEFT) {
-        //lcd.printString("LEFT",20,1); // display welcome
+    } else if (joystick.direction == LEFT) {//when joystick is in the up position it minus the x value from the array
+
         wait(0.05);
         //x--;
         change = 3;
-    } else if (joystick.direction == RIGHT) {
-        //lcd.printString("RIGHT",20,1); // display welcome
+    } else if (joystick.direction == RIGHT) {//when joystick is in the up position it adds the x value from the array
         wait(0.05);
         //x++;
         change = 4;
     }
-     if (change == 1) {
-         y--;
-     } else if (change == 2) {
-         y++;
-     } else if (change == 3) {
-         x--;
-     } else if (change == 4) {
-         x++;
-     }
+    if (change == 1) { //change one makes snake go up
+        y--;
+
+    } else if (change == 2) { //change 2 makes snake go down 
+        y++;
 
+    } else if (change == 3) { //change 3 makes snake go left
+        x--;
+    } else if (change == 4) { //change 4 makes snake go right
+        x++;
     }
-    void SnakeGrowing() {
+
+}//void makes snake grow depending on what the score is 
+void SnakeGrowing()
+{
 
 
-        if(Score>=10) {
-            array[y+36][x+5]= 1;
+    if(Score>=10) {
+        array[y+36][x+5]= 1;
 
-            array[y1+36][x1+5]= 1;
+        array[y1+36][x1+5]= 1;
 
-            array[y2+36][x2+5]= 1;
+        array[y2+36][x2+5]= 1;  //Snake grows to this length when scores 10
 
-            array[y3+36][x3+5]= 1;
+        array[y3+36][x3+5]= 1;
 
-            array[y4+36][x4+5]= 1;
+        array[y4+36][x4+5]= 1;
 
-            array[y5+36][x5+5]= 1;
+        array[y5+36][x5+5]= 1;
 
-            array[y6+36][x6+5]= 1;
+        array[y6+36][x6+5]= 1;
 
-        }
+    }
 
 
-        if(Score>=20) {
-            array[y+36][x+5]= 1;
+    if(Score>=20) {
+        array[y+36][x+5]= 1;
 
-            array[y1+36][x1+5]= 1;
-
-            array[y2+36][x2+5]= 1;
+        array[y1+36][x1+5]= 1;
 
-            array[y3+36][x3+5]= 1;
-
-            array[y4+36][x4+5]= 1;
+        array[y2+36][x2+5]= 1;
 
-            array[y5+36][x5+5]= 1;
+        array[y3+36][x3+5]= 1;
 
-            array[y6+36][x6+5]= 1;
-
-            array[y7+36][x7+5]= 1;
+        array[y4+36][x4+5]= 1; //Snake grows to this length when scores 20
 
-            array[y8+36][x8+5]= 1;
-        }
-        if(Score>=30) {
-            array[y+36][x+5]= 1;
+        array[y5+36][x5+5]= 1;
 
-            array[y1+36][x1+5]= 1;
-
-            array[y2+36][x2+5]= 1;
+        array[y6+36][x6+5]= 1;
 
-            array[y3+36][x3+5]= 1;
-
-            array[y4+36][x4+5]= 1;
+        array[y7+36][x7+5]= 2;
 
-            array[y5+36][x5+5]= 1;
-
-            array[y6+36][x6+5]= 1;
-
-            array[y7+36][x7+5]= 1;
+        array[y8+36][x8+5]= 2;
+    }
+    if(Score>=30) {
+        array[y+36][x+5]= 1;
 
-            array[y8+36][x8+5]= 1;
+        array[y1+36][x1+5]= 1;
 
-            array[y9+36][x9+5]= 1;
-
-            array[y10+36][x10+5]= 1;
+        array[y2+36][x2+5]= 1;
 
-        }
-        if(Score>=40) {
-            array[y+36][x+5]= 1;
-
-            array[y1+36][x1+5]= 1;
+        array[y3+36][x3+5]= 1;
 
-            array[y2+36][x2+5]= 1;
+        array[y4+36][x4+5]= 1;
 
-            array[y3+36][x3+5]= 1;
-
-            array[y4+36][x4+5]= 1;
+        array[y5+36][x5+5]= 1;//Snake grows to this length when scores 30
 
-            array[y5+36][x5+5]= 1;
-
-            array[y6+36][x6+5]= 1;
+        array[y6+36][x6+5]= 1;
 
-            array[y7+36][x7+5]= 1;
+        array[y7+36][x7+5]= 2;
 
-            array[y8+36][x8+5]= 1;
-
-            array[y9+36][x9+5]= 1;
+        array[y8+36][x8+5]= 2;
 
-            array[y10+36][x10+5]= 1;
-
-            array[y11+36][x11+5]= 1;
+        array[y9+36][x9+5]= 2;
 
-            array[y12+36][x12+5]= 1;
+        array[y10+36][x10+5]= 2;
 
-        }
-    
-    if(Score>=50) {
+    }
+    if(Score>=40) {
         array[y+36][x+5]= 1;
 
         array[y1+36][x1+5]= 1;
@@ -546,23 +428,52 @@
 
         array[y5+36][x5+5]= 1;
 
+        array[y6+36][x6+5]= 1;//Snake grows to this length when scores 40
+
+        array[y7+36][x7+5]= 2;
+
+        array[y8+36][x8+5]= 2;
+
+        array[y9+36][x9+5]= 2;
+
+        array[y10+36][x10+5]= 2;
+
+        array[y11+36][x11+5]= 2;
+
+        array[y12+36][x12+5]= 2;
+
+    }
+
+    if(Score>=50) {
+        array[y+36][x+5]= 1;
+
+        array[y1+36][x1+5]= 1;
+
+        array[y2+36][x2+5]= 1;
+
+        array[y3+36][x3+5]= 1;
+
+        array[y4+36][x4+5]= 1;
+
+        array[y5+36][x5+5]= 1;  //Snake grows to this length when scores 50
+
         array[y6+36][x6+5]= 1;
 
-        array[y7+36][x7+5]= 1;
+        array[y7+36][x7+5]= 2;
 
-        array[y8+36][x8+5]= 1;
+        array[y8+36][x8+5]= 2;
 
-        array[y9+36][x9+5]= 1;
+        array[y9+36][x9+5]= 2;
 
-        array[y10+36][x10+5]= 1;
+        array[y10+36][x10+5]= 2;
 
-        array[y11+36][x11+5]= 1;
+        array[y11+36][x11+5]= 2;
 
-        array[y12+36][x12+5]= 1;
+        array[y12+36][x12+5]= 2;
 
-        array[y13+36][x13+5]= 1;
+        array[y13+36][x13+5]= 2;
 
-        array[y14+36][x14+5]= 1;
+        array[y14+36][x14+5]= 2;
 
     }
     if(Score>=60) {
@@ -580,25 +491,25 @@
 
         array[y6+36][x6+5]= 1;
 
-        array[y7+36][x7+5]= 1;
+        array[y7+36][x7+5]= 2;  //Snake grows to this length when scores 60
 
-        array[y8+36][x8+5]= 1;
+        array[y8+36][x8+5]= 2;
 
-        array[y9+36][x9+5]= 1;
+        array[y9+36][x9+5]= 2;
 
-        array[y10+36][x10+5]= 1;
+        array[y10+36][x10+5]= 2;
 
-        array[y11+36][x11+5]= 1;
+        array[y11+36][x11+5]= 2;
 
-        array[y12+36][x12+5]= 1;
+        array[y12+36][x12+5]= 2;
 
-        array[y13+36][x13+5]= 1;
+        array[y13+36][x13+5]= 2;
 
-        array[y14+36][x14+5]= 1;
+        array[y14+36][x14+5]= 2;
 
-        array[y15+36][x15+5]= 1;
+        array[y15+36][x15+5]= 2;
 
-        array[y16+36][x16+5]= 1;
+        array[y16+36][x16+5]= 2;
 
     }
     if(Score>=70) {
@@ -614,31 +525,31 @@
 
         array[y5+36][x5+5]= 1;
 
-        array[y6+36][x6+5]= 1;
+        array[y6+36][x6+5]= 1;  //Snake grows to this length when scores 70
 
-        array[y7+36][x7+5]= 1;
+        array[y7+36][x7+5]= 2;
 
-        array[y8+36][x8+5]= 1;
+        array[y8+36][x8+5]= 2;
 
-        array[y9+36][x9+5]= 1;
+        array[y9+36][x9+5]= 2;
 
-        array[y10+36][x10+5]= 1;
+        array[y10+36][x10+5]= 2;
 
-        array[y11+36][x11+5]= 1;
+        array[y11+36][x11+5]= 2;
 
-        array[y12+36][x12+5]= 1;
+        array[y12+36][x12+5]= 2;
 
-        array[y13+36][x13+5]= 1;
+        array[y13+36][x13+5]= 2;
 
-        array[y14+36][x14+5]= 1;
+        array[y14+36][x14+5]= 2;
 
-        array[y15+36][x15+5]= 1;
+        array[y15+36][x15+5]= 2;
 
-        array[y16+36][x16+5]= 1;
+        array[y16+36][x16+5]= 2;
 
-        array[y17+36][x17+5]= 1;
+        array[y17+36][x17+5]= 2;
 
-        array[y18+36][x18+5]= 1;
+        array[y18+36][x18+5]= 2;
 
     }
     if(Score>=80) {
@@ -656,33 +567,33 @@
 
         array[y6+36][x6+5]= 1;
 
-        array[y7+36][x7+5]= 1;
+        array[y7+36][x7+5]= 2;
 
-        array[y8+36][x8+5]= 1;
+        array[y8+36][x8+5]= 2;
 
-        array[y9+36][x9+5]= 1;
+        array[y9+36][x9+5]= 2;
 
-        array[y10+36][x10+5]= 1;
+        array[y10+36][x10+5]= 2;  //Snake grows to this length when scores 80
 
-        array[y11+36][x11+5]= 1;
+        array[y11+36][x11+5]= 2;
 
-        array[y12+36][x12+5]= 1;
+        array[y12+36][x12+5]= 2;
 
-        array[y13+36][x13+5]= 1;
+        array[y13+36][x13+5]= 2;
 
-        array[y14+36][x14+5]= 1;
+        array[y14+36][x14+5]= 2;
 
-        array[y15+36][x15+5]= 1;
+        array[y15+36][x15+5]= 2;
 
-        array[y16+36][x16+5]= 1;
+        array[y16+36][x16+5]= 2;
 
-        array[y17+36][x17+5]= 1;
+        array[y17+36][x17+5]= 2;
 
-        array[y18+36][x18+5]= 1;
+        array[y18+36][x18+5]= 2;
 
-        array[y19+36][x19+5]= 1;
+        array[y19+36][x19+5]= 2;
 
-        array[y20+36][x20+5]= 1;
+        array[y20+36][x20+5]= 2;
 
     }
     if(Score>=90) {
@@ -700,37 +611,36 @@
 
         array[y6+36][x6+5]= 1;
 
-        array[y7+36][x7+5]= 1;
-
-        array[y8+36][x8+5]= 1;
+        array[y7+36][x7+5]= 2;
 
-        array[y9+36][x9+5]= 1;
+        array[y8+36][x8+5]= 2;    //Snake grows to this length when scores 90
 
-        array[y10+36][x10+5]= 1;
+        array[y9+36][x9+5]= 2;
 
-        array[y11+36][x11+5]= 1;
+        array[y10+36][x10+5]= 2;
 
-        array[y12+36][x12+5]= 1;
+        array[y11+36][x11+5]= 2;
 
-        array[y13+36][x13+5]= 1;
+        array[y12+36][x12+5]= 2;
 
-        array[y14+36][x14+5]= 1;
+        array[y13+36][x13+5]= 2;
 
-        array[y15+36][x15+5]= 1;
+        array[y14+36][x14+5]= 2;
 
-        array[y16+36][x16+5]= 1;
+        array[y15+36][x15+5]= 2;
 
-        array[y17+36][x17+5]= 1;
+        array[y16+36][x16+5]= 2;
 
-        array[y18+36][x18+5]= 1;
+        array[y17+36][x17+5]= 2;
 
-        array[y19+36][x19+5]= 1;
+        array[y18+36][x18+5]= 2;
 
-        array[y20+36][x20+5]= 1;
+        array[y19+36][x19+5]= 2;
 
-        array[y21+36][x21+5]= 1;
+        array[y20+36][x20+5]= 2;
 
-        array[y22+36][x22+5]= 1;
+        array[y21+36][x21+5]= 2;
+        array[y22+36][x22+5]= 2;
 
     }
     if(Score>=100) {
@@ -748,76 +658,208 @@
 
         array[y6+36][x6+5]= 1;
 
-        array[y7+36][x7+5]= 1;
+        array[y7+36][x7+5]= 2;
 
-        array[y8+36][x8+5]= 1;
+        array[y8+36][x8+5]= 2;
 
-        array[y9+36][x9+5]= 1;
+        array[y9+36][x9+5]= 2;
 
-        array[y10+36][x10+5]= 1;
+        array[y10+36][x10+5]= 2;
 
-        array[y11+36][x11+5]= 1;
+        array[y11+36][x11+5]= 2;
 
-        array[y12+36][x12+5]= 1;
+        array[y12+36][x12+5]= 2;  //Snake grows to this length when scores 100
 
-        array[y13+36][x13+5]= 1;
+        array[y13+36][x13+5]= 2;
 
-        array[y14+36][x14+5]= 1;
+        array[y14+36][x14+5]= 2;
 
-        array[y15+36][x15+5]= 1;
+        array[y15+36][x15+5]= 2;
 
-        array[y16+36][x16+5]= 1;
+        array[y16+36][x16+5]= 2;
 
-        array[y17+36][x17+5]= 1;
+        array[y17+36][x17+5]= 2;
 
-        array[y18+36][x18+5]= 1;
+        array[y18+36][x18+5]= 2;
 
-        array[y19+36][x19+5]= 1;
+        array[y19+36][x19+5]= 2;
 
-        array[y20+36][x20+5]= 1;
+        array[y20+36][x20+5]= 2;
 
-        array[y21+36][x21+5]= 1;
+        array[y21+36][x21+5]= 2;
 
-        array[y22+36][x22+5]= 1;
+        array[y22+36][x22+5]= 2;
 
-        array[y23+36][x23+5]= 1;
+        array[y23+36][x23+5]= 2;
 
-        array[y24+36][x24+5]= 1;
+        array[y24+36][x24+5]= 2;
 
     }
-    DrawArray();
+    DrawArray(); // draws snake 
 
 
 
 }
 
-
-/*void GameOver()
+//void that ends game when snake hits the wall or snake eats itself 
+void EndGame()
 {
-if (joystick.direction == CENTRE) {
-        //lcd.printString("CENTRE",20,1); // display welcome
-        wait(0.05);
-    } else if (joystick.direction == UP) {
-        lcd.printString("UP",20,1); // display welcome
-        wait(0.05);
-        if(y+36==2 && x+5==2) {
-        GenerateFood();
-    } else if (joystick.direction == DOWN) {
-        lcd.printString("DOWN",20,1); // display welcome
-        wait(0.05);
-        change = 2;
-        if(y+36==randY && x+5==randX) {
-        GenerateFood();
-    } else if (joystick.direction == LEFT) {
-        //lcd.printString("LEFT",20,1); // display welcome
-        wait(0.05);
-        change = 3;
-    } else if (joystick.direction == RIGHT) {
-        //lcd.printString("RIGHT",20,1); // display welcome
-        wait(0.05);
-        change = 4;
-    }
+
+    if(y+36==46||y+36==0||x+5==83||x+5==0) { //checks if the snake head is equal to any pixels that are out of bounds 
+        GameOver();
+        wait(3);
 
     }
 }
-*/
+//void to show when game is ended. shows GAMEOVER and the score that the user has 
+void GameOver()
+{
+
+    // first need to initialise display
+
+    lcd.clear();
+    lcd.printString("GAME OVER",15,1); // print GAME OVER
+    sprintf(str,"%d",Score);
+    lcd.printString(str,37,3);    // shows score in centre of screen 
+    lcd.printString("Press Button",6,5); // shows press button to return to menu
+    while(1) {
+        if (buttonA==1) {
+            lcd.clear();
+            checkerBoard();
+            wait(0.5);
+            lcd.clear();
+            x = 0;
+            x1 = 0;
+            x2 = 0;
+            x3 = 0;
+            x4 = 0;
+            x5 = 0;
+            x6 = 0;
+            x7 = 0;
+            x8 = 0;
+            x9 = 0;
+            x10 = 0;
+            x11 = 0;
+            x12 = 0;
+            x13 = 0;
+            x14 = 0;
+            x15 = 0;      // SETS ALL VALUES BACK TO ORGINAL SO GAME CAN START AGAIN 
+            x16 = 0;
+            x17 = 0;
+            x18 = 0;
+            x19 = 0;
+            x20 = 0;
+            x21 = 0;
+            x22 = 0;
+            x23 = 0;
+            x24 = 0;
+            y = 0;
+            y1 = 1;
+            y2 = 2;
+            y3 = 3;
+            y4 = 4;
+            y5 = 5;
+            y6 = 6;
+            y7 = 7;
+            y8 = 8;
+            y9 = 9;
+            y10 = 10;
+            y11 = 11;
+            y12 = 12;
+            y13 = 13;
+            y14 = 14;
+            y15 = 15;
+            y16 = 16;
+            y17 = 17;
+            y18 = 18;
+            y19 = 19;
+            y20 = 20;
+            y21 = 21;
+            y22 = 22;
+            y23 = 23;
+            y24 = 24;
+            change = 1;
+            pause=0;
+            Score=0;
+
+            Menu(); // returns to menu so game can start again
+
+        }
+    }
+}
+//void that comes up when the maximum score is achieved shows "YOU WIN" and shows the user the score
+void Winner()
+{
+    if(Score==110) { // when the score is equal to 110 
+
+        lcd.clear();
+        lcd.printString("YOU WIN!!!",10,1); // Clear screen and show YOU WIN
+        sprintf(str,"%d",Score);     // shows score in the centre of the page
+        lcd.printString(str,34,3);
+        lcd.printString("Press Button",6,5);
+        while(1) {
+            if (buttonA==1) { // when the button on the PCB the screen checkers and the Menu displayed  
+                lcd.clear();
+                checkerBoard();
+                wait(0.5);
+                lcd.clear();
+                x = 0;
+                x1 = 0;
+                x2 = 0;
+                x3 = 0;
+                x4 = 0;
+                x5 = 0;
+                x6 = 0;
+                x7 = 0;
+                x8 = 0;
+                x9 = 0;
+                x10 = 0;
+                x11 = 0;
+                x12 = 0;
+                x13 = 0;
+                x14 = 0;
+                x15 = 0;
+                x16 = 0;
+                x17 = 0;
+                x18 = 0;
+                x19 = 0;
+                x20 = 0;
+                x21 = 0;
+                x22 = 0;
+                x23 = 0;  // SET ALL VALUES BACK TO ORIGINAL VALUE 
+                x24 = 0;
+                y = 0;
+                y1 = 1;
+                y2 = 2;
+                y3 = 3;
+                y4 = 4;
+                y5 = 5;
+                y6 = 6;
+                y7 = 7;
+                y8 = 8;
+                y9 = 9;
+                y10 = 10;
+                y11 = 11;
+                y12 = 12;
+                y13 = 13;
+                y14 = 14;
+                y15 = 15;
+                y16 = 16;
+                y17 = 17;
+                y18 = 18;
+                y19 = 19;
+                y20 = 20;
+                y21 = 21;
+                y22 = 22;
+                y23 = 23;
+                y24 = 24;
+                change = 1;
+                pause=0;
+                Score=0;
+
+                Menu(); //SHOW MENU
+
+            }
+        }
+    }
+}