A retro gaming programme, designed for use on a portable embedded system. Incorporates power saving techniques.

Dependencies:   ConfigFile N5110 PowerControl beep mbed

Revision:
35:9f55326c1b0a
Parent:
34:9dc844bdc776
Child:
36:b02b7935475a
--- a/tower.h	Tue Apr 21 14:34:45 2015 +0000
+++ b/tower.h	Tue Apr 21 21:17:05 2015 +0000
@@ -1,72 +1,68 @@
 
-// change this to alter tolerance of joystick direction
+//change this to alter tolerance of joystick direction
 #define DIRECTION_TOLERANCE 0.05
 
 #include <string>
 #include "mbed.h"
 
-//using namespace :: std;
-
-// VCC,SCE,RST,D/C,MOSI,SCLK,LED - set pins for LCD
+//VCC,SCE,RST,D/C,MOSI,SCLK,LED - set pins for LCD
 N5110 lcd(p7,p8,p9,p10,p11,p13,p22);
 
-// timers to check state of buttons
-Ticker timerA;//buttonA
-Ticker timerB;//buttonB
+//timers to check state of buttons
+Ticker timerA;//for buttonA
+Ticker timerB;//for buttonB
 
-// create buzzer objecct
+//create buzzer objecct
 Beep buzzer(p21);
 
-// create local file system
-//LocalFileSytem local("local");
+//navigation/action buttons
+DigitalIn buttonA(p19);//buttonA
+DigitalIn buttonB(p20);//buttonB
 
-// navigation/action buttons
-DigitalIn buttonA(p19);
-DigitalIn buttonB(p20);
+//LED indicators
+AnalogOut ledR(p18);//red LED
+DigitalOut ledY(p24);//yellow LED
 
-// LED indicators
-AnalogOut ledR(p18);// RED LED
-DigitalOut ledY(p24);// YELLOW LED
-
-// connections for joystick
+//connections for joystick
 InterruptIn joyButton(p17);//Interrupt for ISR
-AnalogIn xPot(p15);
-AnalogIn yPot(p16);
+AnalogIn xPot(p15);//left/right
+AnalogIn yPot(p16);//up/down
 
-// Globabl Variables /////////////////////////
+//Globabl Variables /////////////////////////
 
-// sound FX toggle
+//sound FX toggle
 int FX = 0;
 
+//previous Direction
 int preDir;
 
-// timer flags to check state of buttons
+//timer flags to check state of the buttons
 int buttonFlagA = 0;
 int buttonFlagB = 0;
 
-// flag for joystick reading
+//flag for joystick reading
 int printFlag = 0;
 
-// boundary conditions
+//boundary conditions
 int cells [84][48];
 
-// player's score (in game)
+//real time score
 int score = 0;
 
-//high score details
-int highScore1 ;
-int highScore2 ;
-int highScore3 ;
+//stored high score variables
+int highScore1;
+int highScore2;
+int highScore3;
 
-//player initials
+//global char arrays to store initials/score
 char player1initials[14] = {"1.AAA.....00"};
 char player2initials[14] = {"2.BBB.....00"};
 char player3initials[14] = {"3.CCC.....00"};
 
-// difficulty - number of pixels hazards incrememnt by
+//difficulty variable - hazards fall at 2 pixels per refresh
 int fall = 2;
 
-// global variables for movement (pixelNinja)
+//global variables for movement (pixelNinja)
 int a1 = 22;
 int a2 = 24;
 int a3 = 23;
@@ -76,7 +72,7 @@
 int a7 = 19;
 int a8 = 21;
 
-//global variable for random X co-ordinates
+//global variable for hazard X co-ordinates
 int randX1;
 int randX2;
 int randX3;
@@ -84,7 +80,7 @@
 int randX5;
 int randX6;
 
-// global variable for Y co-ordinates
+//global variable for hazard Y co-ordinates
 int randY1 = 0;
 int randY2 = 0;
 int randY3 = 0;
@@ -92,24 +88,59 @@
 int randY5 = 0;
 int randY6 = 0;
 
-// struct for choosing initials
+//integers for changing struct ouput states
+int state1 = 5;
+int state2 = 3;
+int state3 = 1;
+
+//prototypes
+void calibrateJoystick();
+void updateJoystick();
+void timerExpiredA();
+void timerExpiredB();
+void actionButton();
+void randomise();
+void resetGame();
+void startrek();
+void refreshCursor1();
+void refreshCursor2();
+void refreshCursor3();
+void ninjaBoundaries();
+void hazardFall();
+void newScore();
+
+void mainMenu();
+void exitMenu();
+void optionsMenu();
+void game();
+void difficultyMenu();
+void soundFXMenu();
+void scores();
+
+void drawNinja();
+void drawHazards();
+void drawWelcome();
+void drawBackground();
+void drawSoundFXMenu();
+void drawDifficultyMenu();
+void drawMainMenu();
+void drawOptionsMenu();
+void drawExitMenu();
+
+//struct for choosing initials
 struct State {
     char output1;
     char output2;
     char output3;
     int nextState[2];
 };
+
+//assigns new identifier to struct
 typedef const struct State STYP;
 
-int state1 = 5;//integer for changing state 1
-int state2 = 3;//integer for changing state 2
-int state3 = 1;//integer for changing state 3
-
-// array for Alphabet
+//output array for struct Alphabet
 STYP fsm[27] = {
-
     //output//nextstate//previousstate//
-
     {'A','A','A',{1,26}},
     {'B','B','B',{2,0}},
     {'C','C','C',{3,1}},
@@ -139,18 +170,14 @@
     {'.','.','.',{0,25}},
 };
 
-// function prototypes
-void calibrateJoystick();
-void updateJoystick();
-
-// timer to regularly read the joystick
+//timer to read the joystick
 Ticker pollJoystick;
 
-// serial for debug
+//serial out for debug
 Serial serial(USBTX,USBRX);
 
-// create enumerated type (0,1,2,3 etc. for direction)
-// could be extended for diagonals etc.
+//create enumerated type (0,1,2,3 etc. for direction)
+//could be extended for diagonals etc.
 enum DirectionName {
     UP,
     DOWN,
@@ -160,21 +187,21 @@
     UNKNOWN
 };
 
-// struct for Joystick
+//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
+    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
+//create struct variable
 Joystick joystick;
 
-// if buttonA set flag A
+//if buttonA set flag A
 void timerExpiredA()
 {
     if(buttonA == 1) {
@@ -183,7 +210,7 @@
     }
 }
 
-// if buttonB set flag B
+//if buttonB set flag B
 void timerExpiredB()
 {
     if(buttonB == 1) {
@@ -192,14 +219,14 @@
     }
 }
 
-// set seed/randomise initial co-Ordinates
+//set seed/randomise initial co-Ordinates
 void randomise()
 {
     srand (time(NULL));//initial seed for randomisation
 
-    // initial random x co-ordinates
-    // for falling hazards
-    // (values between 3 and 76)
+    //initial random x co-ordinates
+    //for falling hazards
+    //(values between 3 and 76)
     randX1 = rand() % 74 + 5;
     randX2 = rand() % 74 + 5;
     randX3 = rand() % 74 + 5;
@@ -208,27 +235,27 @@
     randX6 = rand() % 74 + 5;
 }
 
-// static background
-void backGround()
+//static background
+void drawBackground()
 {
-    // x, y, w, h, fill - draw ground
+    //x, y, w, h, fill - draw ground
     lcd.drawRect(0,47,84,0,1);
 
-    // x, y, w, h, fill - draw left wall
+    //x, y, w, h, fill - draw left wall
     lcd.drawRect(2,0,0,47,1);
-    // left wall - brick line 1
+    //left wall - brick line 1
     for(int x=0; x<47; x+=4)
         lcd.drawLine(1,1,1,48,2);
-    // left wall - brick line 2
+    //left wall - brick line 2
     for(int x=0; x<47; x+=4)
         lcd.drawLine(0,0,0,48,2);
 
-    // x, y, w, h, fill - draw right wall
+    //x, y, w, h, fill - draw right wall
     lcd.drawRect(81,0,0,47,1);
-    // right wall - brick line 1
+    //right wall - brick line 1
     for(int x=0; x<WIDTH; x+=4)
         lcd.drawLine(82,0,82,48,2);
-    // right wall - brick line 2
+    //right wall - brick line 2
     for(int x=0; x<WIDTH; x+=4)
         lcd.drawLine(83,1,83,48,2);
 
@@ -236,7 +263,7 @@
 }
 
 //intro screen
-void welcome()
+void drawWelcome()
 {
     //bottom border
     lcd.drawRect(0,44,84,2,1);
@@ -282,7 +309,7 @@
 }
 
 //pixel ninja character
-void pixelNinja()
+void drawNinja()
 {
     //x, y, w, h, fill - left leg
     lcd.drawRect(a1,39,0,7,1);
@@ -316,7 +343,7 @@
     lcd.drawRect(a8,38,0,0,1);
 }
 
-// stops ninja going through walls
+//stops ninja going through walls
 void ninjaBoundaries()
 {
     if(a6 > 79 )
@@ -354,7 +381,7 @@
         a7 = 4;
 }
 
-// resets back to initial values
+//resets variables initial values when game ends or is exited
 void resetGame()
 {
     score = 0;
@@ -368,9 +395,9 @@
     a7 = 19;
     a8 = 21;
 
-    // in this case the X values are given a
-    // new random variable each time the player
-    // dies or exits and starts a new game
+    //in this case the X values are given a
+    //new random variable each time the player
+    //dies, exits or starts a new game
     randX1 = rand() % 74 + 5;
     randX2 = rand() % 74 + 5;
     randX3 = rand() % 74 + 5;
@@ -387,10 +414,10 @@
     lcd.clear();
 }
 
-// draws falling hazards
-void hazards()
+//draws falling hazards
+void drawHazards()
 {
-    // X, Y, radius, fill
+    //X, Y, radius, fill
     lcd.drawCircle(randX1,randY1,2,1);
     lcd.drawCircle(randX2,randY2,2,1);
     lcd.drawCircle(randX3,randY3,2,1);
@@ -401,11 +428,11 @@
     lcd.refresh();
 }
 
-// makes hazards fall - randomises X axis co-ordinates
+//makes hazards fall - randomises X axis co-ordinates
 void hazardFall()
 {
-    // increments randY1 variables
-    // appearing to make them fall
+    //increments randY1 variables
+    //appearing to make them fall
     randY1 = randY1 += fall;
     randY2 = randY2 += fall;
     randY3 = randY3 += fall;
@@ -413,8 +440,8 @@
     randY5 = randY5 += fall;
     randY6 = randY6 += fall;
 
-    // loops the objects once they 'hit the floor'
-    // this imitates a new set of objects falling
+    //loops the objects once they 'hit the floor'
+    //this imitates a new set of objects falling
 
     if (randY1>=48)
         randY1=0;
@@ -431,17 +458,17 @@
     if (randY5>=48)
         randY5=0;
 
-    // each time the objects loop, a new pseudo random value
-    // is assigned to the global variables (randX) to
-    // randomise their positions
+    //each time the objects loop, a new pseudo random value
+    //is assigned to the global variables (randX) to
+    //randomise their positions
 
     if (randY6>=48) {
         randY6=0;
 
         score = score++;//increment score by 1 after each wave of hazards
 
-        randX1 = rand() % 74 + 5;// psuedo random number
-        randX2 = rand() % 74 + 5;// between 5 and 74
+        randX1 = rand() % 74 + 5;//psuedo random number
+        randX2 = rand() % 74 + 5;//between 5 and 74
         randX3 = rand() % 74 + 5;
         randX4 = rand() % 74 + 5;
         randX5 = rand() % 74 + 5;
@@ -449,59 +476,59 @@
     }
 }
 
-// clears old pixels and keeps set pixels
+//clears old pixels and keeps set pixels
 void startrek()
 {
-    for (int i=3; i<81; i++)// loops through rows
+    for (int i=3; i<81; i++)//loops through rows
         for (int j=0; j<47; j++)
-            if (cells[i][j]) {// if there's a pixel then keep it
+            if (cells[i][j]) {//if there's a pixel then keep it
                 lcd.setPixel(i,j);
             } else {
-                lcd.clearPixel(i,j);// else remove the old ones
+                lcd.clearPixel(i,j);//else remove the old ones
             }
     lcd.refresh();
 }
 
-// clears old pixels and keeps set pixels
+//clears old pixels and keeps set pixels
 void refreshCursor1()
 {
-    for (int i=70; i<80; i++)// loops through rows
+    for (int i=70; i<80; i++)//loops through rows
         for (int j=17; j<25; j++)
-            if (cells[i][j]) {// if there's a pixel then keep it
+            if (cells[i][j]) {//if there's a pixel then keep it
                 lcd.setPixel(i,j);
             } else {
-                lcd.clearPixel(i,j);// else remove the old ones
+                lcd.clearPixel(i,j);//else remove the old ones
             }
     lcd.refresh();
 }
 
-// clears old pixels and keeps set pixels
+//clears old pixels and keeps set pixels
 void refreshCursor2()
 {
-    for (int i=70; i<80; i++)// loops through rows
+    for (int i=70; i<80; i++)//loops through rows
         for (int j=25; j<32; j++)
-            if (cells[i][j]) {// if there's a pixel then keep it
+            if (cells[i][j]) {//if there's a pixel then keep it
                 lcd.setPixel(i,j);
             } else {
-                lcd.clearPixel(i,j);// else remove the old ones
+                lcd.clearPixel(i,j);//else remove the old ones
             }
     lcd.refresh();
 }
 
-// clears old pixels and keeps set pixels
+//clears old pixels and keeps set pixels
 void refreshCursor3()
 {
-    for (int i=70; i<80; i++)// loops through rows
+    for (int i=70; i<80; i++)//loops through rows
         for (int j=32; j<40; j++)
-            if (cells[i][j]) {// if there's a pixel then keep it
+            if (cells[i][j]) {//if there's a pixel then keep it
                 lcd.setPixel(i,j);
             } else {
-                lcd.clearPixel(i,j);// else remove the old ones
+                lcd.clearPixel(i,j);//else remove the old ones
             }
     lcd.refresh();
 }
 
-// beep/light when buttons are closed
+//beep/light when buttons are closed
 void actionButtons()
 {
     if((FX == 0)&&(buttonA||buttonB)) {
@@ -515,49 +542,49 @@
     }
 }
 
-// presents main menu options
+//presents main menu options
 void mainMenu(int& mainOption)
 {
     actionButtons();//set audible/light for button
 
-    // joystick selection
+    //joystick selection
     if (printFlag) {//if flag set, clear flag and print joystick values to serial port
         printFlag = 0;
 
-        // option up
+        //option up
         if (joystick.direction == UP) {
             serial.printf(" UP\n");
             mainOption = mainOption--;
             if (mainOption < 0)mainOption = 0;
         }
-        // option down
+        //option down
         if (joystick.direction == DOWN) {
             serial.printf(" DOWN\n");
             mainOption = mainOption++;
             if (mainOption > 2)mainOption = 2;
         }
-        // Centre / Unknown orientation
+        //Centre / Unknown orientation
         if (joystick.direction == CENTRE)
             serial.printf(" CENTRE\n");
         if (joystick.direction == UNKNOWN)
             serial.printf(" Unsupported direction\n");
 
-        // 'Play Game' option 1
+        //'Play Game' option 1
         if (mainOption == 0) {
             lcd.printString("Play Game",3,4);
         }
-        // 'High Scores' option 2
+        //'High Scores' option 2
         if (mainOption == 1) {
             lcd.printString("  Scores ",3,4);
         }
-        // 'Options' option 3
+        //'Options' option 3
         if (mainOption == 2) {
             lcd.printString(" Options ",3,4);
         }
     }
 }
 
-// draws main menu
+//draws main menu
 void drawMainMenu()
 {
     //bottom border
@@ -625,14 +652,14 @@
     lcd.drawRect(29,27,0,0,1);
 }
 
-// presents exit menu options
+//presents exit menu options
 void exitMenu(int& exitOption)
 {
     actionButtons();
     if (printFlag) {//if flag set, clear flag and print joystick values to serial port
         printFlag = 0;
 
-        // check joystick direction
+        //check joystick direction
         if (joystick.direction == LEFT) {
             serial.printf(" LEFT\n");
             exitOption--;
@@ -648,7 +675,7 @@
         if (joystick.direction == UNKNOWN)
             serial.printf(" Unsupported direction\n");
     }
-    // draws option cursor
+    //draws option cursor
     if(exitOption == 0) {
         lcd.printString("YES",29,27);
     }
@@ -657,57 +684,57 @@
     }
 }
 
-// draws exit menu
+//draws exit menu
 void drawExitMenu()
 {
-    // set exit menu
+    //set exit menu
     lcd.clear();
     lcd.drawRect(8,6,70,30,0);//title outline
     lcd.printString("Exit Game?",10,25);
-    backGround();
+    drawBackground();
 
-    // option arrow - right
+    //option arrow - right
     lcd.drawRect(55,25,0,4,1);
     lcd.drawRect(56,26,0,2,1);
     lcd.drawRect(57,27,0,0,1);
 
-    // option arrow - left
+    //option arrow - left
     lcd.drawRect(27,25,0,4,1);
     lcd.drawRect(26,26,0,2,1);
     lcd.drawRect(25,27,0,0,1);
 }
 
-// presents the options
+//presents the options
 void optionsMenu(int& option)
 {
-    // joystick selection
+    //joystick selection
     if (printFlag) {  //if flag set, clear flag and print joystick values to serial port
         printFlag = 0;
 
-        // option up
+        //option up
         if (joystick.direction == UP) {
             serial.printf(" UP\n");
             option = option--;
             if (option < 0)option = 0;
         }
-        // option down
+        //option down
         if (joystick.direction == DOWN) {
             serial.printf(" DOWN\n");
             option = option++;
             if (option > 1)option = 1;
         }
-        // Centre / Unknown orientation
+        //Centre / Unknown orientation
         if (joystick.direction == CENTRE)
             serial.printf(" CENTRE\n");
         if (joystick.direction == UNKNOWN)
             serial.printf(" Unsupported direction\n");
 
-        // 'Difficulty' option 1
+        //'Difficulty' option 1
         if (option == 0) {
             lcd.drawCircle(72,27,2,1);
             refreshCursor3();
         }
-        // 'Sound FX' option 2
+        //'Sound FX' option 2
         if (option == 1) {
             lcd.drawCircle(72,35,2,1);
             refreshCursor2();
@@ -715,11 +742,11 @@
     }
 }
 
-// draws options menu
+//draws options menu
 void drawOptionsMenu()
 {
     lcd.clear();//clear screen
-    backGround();
+    drawBackground();
     lcd.drawRect(3,6,77,10,0);//title outline
     lcd.drawRect(0,47,84,0,1);//bottom border
     lcd.drawRect(0,0,84,2,1);//top border
@@ -728,34 +755,34 @@
     lcd.printString("Sound FX",3,10);
 }
 
-// present difficulty options
+//present difficulty options
 void difficultyMenu(int& subOption)
 {
     actionButtons();
 
-    // joystick selection
+    //joystick selection
     if (printFlag) {//if flag set, clear flag,print joystick values
         printFlag = 0;
 
-        // option up
+        //option up
         if (joystick.direction == UP) {
             serial.printf(" UP\n");
             subOption = subOption--;
             if (subOption < 0)subOption = 0;
         }
-        // option down
+        //option down
         if (joystick.direction == DOWN) {
             serial.printf(" DOWN\n");
             subOption = subOption++;
             if (subOption > 2)subOption = 2;
         }
-        // Centre / Unknown orientation
+        //Centre / Unknown orientation
         if (joystick.direction == CENTRE)
             serial.printf(" CENTRE\n");
         if (joystick.direction == UNKNOWN)
             serial.printf(" Unsupported direction\n");
 
-        // 'Easy' option 1
+        //'Easy' option 1
         if (subOption == 0) {
             lcd.drawCircle(72,19,2,1);
             refreshCursor2();
@@ -765,7 +792,7 @@
                 fall = 1;
             }
         }
-        // 'Normal' option 2
+        //'Normal' option 2
         if (subOption == 1) {
             lcd.drawCircle(72,27,2,1);
             refreshCursor1();
@@ -776,7 +803,7 @@
                 fall = 2;
             }
         }
-        // 'Forget It' option 3
+        //'Forget It' option 3
         if (subOption == 2) {
             lcd.drawCircle(72,35,2,1);
             refreshCursor1();
@@ -790,11 +817,11 @@
     }
 }
 
-// draw difficulty settings
+//draw difficulty settings
 void drawDifficultyMenu()
 {
     lcd.clear();
-    backGround();
+    drawBackground();
     lcd.drawRect(0,47,84,0,1);//bottom border
     lcd.drawRect(0,0,84,2,1);//top border
     lcd.printString("*Difficulty*",5,7);//title
@@ -803,35 +830,35 @@
     lcd.printString("Forget It",5,10);//title
 }
 
-// present sound FX options
+//present sound FX options
 void soundFXMenu(int& fxOption)
 {
     actionButtons();
 
-    // joystick selection
+    //joystick selection
     if (printFlag) {//if flag set, clear flag,print joystick values
         printFlag = 0;
 
-        // option up
+        //option up
         if (joystick.direction == UP) {
             serial.printf(" UP\n");
             fxOption = fxOption--;
             if (fxOption < 0)fxOption = 0;
         }
-        // option down
+        //option down
         if (joystick.direction == DOWN) {
             serial.printf(" DOWN\n");
             fxOption = fxOption++;
             if (fxOption > 1)fxOption = 1;
         }
-        // Centre / Unknown orientation
+        //Centre / Unknown orientation
         if (joystick.direction == CENTRE)
             serial.printf(" CENTRE\n");
         if (joystick.direction == UNKNOWN)
             serial.printf(" Unsupported direction\n");
     }
 
-    // 'ON' option 1
+    //'ON' option 1
     if (fxOption == 0) {
         lcd.drawCircle(72,27,2,1);//draw cursor 'ON'
         refreshCursor1();
@@ -843,7 +870,7 @@
             serial.printf("FX = %d\n",FX);
         }
     }
-    // 'OFF' option 2
+    //'OFF' option 2
     if (fxOption == 1) {
         lcd.drawCircle(72,35,2,1);//draw cursor 'OFF'
         refreshCursor1();
@@ -857,11 +884,11 @@
     }
 }
 
-// draw Sound FX settings
+//draw Sound FX settings
 void drawSoundFXMenu()
 {
     lcd.clear();
-    backGround();
+    drawBackground();
     lcd.drawRect(0,47,84,0,1);//bottom border
     lcd.drawRect(0,0,84,2,1);//top border
     lcd.printString("*Sound FX*",10,7);//title
@@ -869,30 +896,27 @@
     lcd.printString("OFF",33,10);//title
 }
 
-// if any of the high scores are beaten, they are replaced.
-// player enters initials using struct
+//if any of the high scores are beaten, they are replaced.
+//player enters initials using struct
 void newScore()
 {
     char x,y,z;
     if(score >= highScore3) {//entry condition
-        buttonFlagA = 0;
+        buttonFlagA = 0;//reset flags
         buttonFlagB = 0;
-
-        lcd.clear();
-        backGround();
+        lcd.clear();//clears screen
+        drawBackground();//draws background
         lcd.printString("High Score!!",7,0);//title
         lcd.printString("Enter ID",19,4);//title
+
         int n;//local variable used for storing temporary global variable
         int initial = 0;//used for isolating which initial is being selected
 
-        //put conditions here
-
         while(1) {
 
-            // joystick selection
+            //joystick selection
             if (printFlag) {//if flag set, clear flag,print joystick values
                 printFlag = 0;
-                // option up
 
                 if(joystick.direction==CENTRE) {
                     preDir=0;
@@ -907,7 +931,7 @@
                     if (state3 < 0)state3 = 0;
                     preDir=1;
                 }
-                // option down
+                //option down
                 if (joystick.direction == DOWN && preDir==0) {
                     serial.printf(" DOWN\n");
                     state1 = state1++;
@@ -929,45 +953,37 @@
                     initial = initial++;
                     if (initial > 2)initial = 2;
                     preDir=1;
-
                 }
-                // Centre / Unknown orientation
+                //Centre / Unknown orientation
                 if (joystick.direction == CENTRE)
                     serial.printf(" CENTRE\n");
                 if (joystick.direction == UNKNOWN)
                     serial.printf(" Unsupported direction\n");
-
-
             }
-
-//            char x[1];//create storage
-//            char y[1];//for initials
-//            char z[1];
-
-
-            // if initial 3 display selected character
-            if (initial == 0) {
-                x=fsm[state1].output1;
-                lcd.printChar(x,25,26);
-//                int initial1 = sprintf(x,"%c",fsm[state1].output1);//insert into letter in x
-//                lcd.printString(x,25,26);//print x buffer
-            }
-            // if initial 1 display selected character
-            if(initial == 1) {
-                y=fsm[state2].output2;
-                lcd.printChar(y,37,26);
-
-//                int initial2 = sprintf(y,"%c",fsm[state2].output2);//insert into letter in y
-//                lcd.printString(y,37,26);//print y buffer
-            }
-            // if initial 2 display selected character
-            if(initial == 2) {
-                z=fsm[state3].output3;
-                lcd.printChar(z,49,26);
-
-//                              int initial3 = sprintf(z,"%c",fsm[state3].output3);//insert into letter z
-//                lcd.printString(z,49,26);//print z buffer
-            }
+            
+            x=fsm[state1].output1;
+            lcd.printChar(x,25,26);
+            y=fsm[state1].output1;
+            lcd.printChar(y,37,26);
+            z=fsm[state1].output1;
+            lcd.printChar(z,49,26);
+            
+            ////if initial 3 display selected character
+//            if (initial == 0) {
+//                char x;
+//                x=fsm[state1].output1;
+//                lcd.printChar(x,25,26);
+//            }
+//            //if initial 1 display selected character
+//            if(initial == 1) {
+//                y=fsm[state2].output2;
+//                lcd.printChar(y,37,26);
+//            }
+//            //if initial 2 display selected character
+//            if(initial == 2) {
+//                z=fsm[state3].output3;
+//                lcd.printChar(z,49,26);
+//            }
 
             if(buttonFlagA) {
                 buttonFlagA = 0;
@@ -975,67 +991,59 @@
                 break;
             }
         }
-        // if player beats High Score 3, replace it with new score
+        //if player beats High Score 3, replace it with new score
         if(score >= highScore3 && score<highScore2 ) {
             //n = score;
             highScore3 = score;
             //
-            int insert3 = sprintf (player3initials, "3.%c%c%c.....%i",x,y,z,highScore3);
+            sprintf (player3initials, "3.%c%c%c.....%i",x,y,z,highScore3);
         }
-        // if player beats High Score 3 and 2, replace HighScore2 with new score
+        //if player beats High Score 3 and 2, replace HighScore2 with new score
         if(score >= highScore2 && score< highScore1) {
-            int insert2;
             highScore3 = highScore2;
             n = score;
             highScore2 = n;
-            insert2=sprintf (player3initials, "1.%c%c%c.....%i",player2initials[2],player3initials[3],player3initials[4],highScore3);
-            insert2 = sprintf (player2initials, "2.%c%c%c.....%i",x,y,z,highScore2);
+            sprintf (player3initials, "1.%c%c%c.....%i",player2initials[2],player3initials[3],player3initials[4],highScore3);
+            sprintf (player2initials, "2.%c%c%c.....%i",x,y,z,highScore2);
         }
-        // if player beats High Score 1, 2 and 3, replace highScore1 with new score
+        //if player beats High Score 1, 2 and 3, replace highScore1 with new score
         if(score >= highScore1 ) {
-            int insert1;
             highScore3 = highScore2;
             highScore2 = highScore1;
             n = score;
             highScore1 = n;
-            insert1=sprintf (player3initials, "1.%c%c%c.....%i",player2initials[2],player3initials[3],player3initials[4],highScore3);
-            insert1=sprintf (player2initials, "1.%c%c%c.....%i",player1initials[2],player1initials[3],player1initials[4],highScore2);
-            insert1 = sprintf (player1initials, "1.%c%c%c.....%i",x,y,z,highScore1);
+            sprintf (player3initials, "1.%c%c%c.....%i",player2initials[2],player3initials[3],player3initials[4],highScore3);
+            sprintf (player2initials, "1.%c%c%c.....%i",player1initials[2],player1initials[3],player1initials[4],highScore2);
+            sprintf (player1initials, "1.%c%c%c.....%i",x,y,z,highScore1);
         }
-        // goes back to main menu after storing initials
-//            if(buttonFlagA) {
-//                buttonFlagA = 0;
-//                buttonFlagB = 0;
-//                break;
-//            }
     }
 }
 
 
 
-// actual game
+//actual game
 void game(int& exitFlag, int& exitOption)
 {
     actionButtons();
     lcd.clear();//clears screen
-    backGround();//draw background
+    drawBackground();//draw background
 
     ///game///
     while(1) {
-        // print score - top left of display
+        //print score - top left of display
         char buffer[14];//create buffer for string
         int length = sprintf(buffer,"Level:%d",score);//insert buffer
         lcd.printString(buffer,3,0);//display
 
         actionButtons();
-        pixelNinja();//set character
-        hazards();//initiates hazards
+        drawNinja();//set character
+        drawHazards();//initiates hazards
         hazardFall();//increments hazards towards floor
 
         if (printFlag) {  //if flag set, clear flag and print joystick values to serial port
             printFlag = 0;
 
-            // check joystick direction
+            //check joystick direction
             if (joystick.direction == LEFT) {
                 serial.printf(" LEFT\n");
                 a1 = a1-=2;
@@ -1068,11 +1076,11 @@
             if (joystick.direction == UNKNOWN)
                 serial.printf(" Unsupported direction\n");
 
-            // integer to represent character being
-            // struck by falling object
+            //integer to represent character being
+            //struck by falling object
             int contactPoint = 0;
 
-            // contact points
+            //contact points
             if(lcd.getPixel((a1+4),32))
                 contactPoint++;
             if(lcd.getPixel((a1),32))
@@ -1080,9 +1088,9 @@
             if(lcd.getPixel((a7),32))
                 contactPoint++;
 
-            // if contact point is not zero
-            // character has been hit
-            // and the game ends
+            //if contact point is not zero
+            //character has been hit
+            //and the game ends
             if ( contactPoint !=0) {
                 lcd.printString("Game Over",17,2);
                 lcd.inverseMode();
@@ -1105,7 +1113,7 @@
             }
             startrek();//clears unset pixels, keeps set pixels
 
-/// Exit Menu (Back button pressed)///
+///Exit Menu (Back button pressed)///
             if(buttonFlagB) {
                 buttonFlagB = 0;//reset flags
                 buttonFlagA = 0;
@@ -1115,7 +1123,7 @@
                 while(1) {
                     exitMenu(exitOption);//presents exit options
 
-                    // 'exit' option YES
+                    //'exit' option YES
                     if((buttonFlagA)&&(exitOption == 0)) { //returns to menu
                         buttonFlagA = 0;//reset flags
                         buttonFlagB = 0;
@@ -1125,16 +1133,16 @@
                         exitFlag = 1;//sets exit flag
                         break;
                     }
-                    // 'exit' option NO - returns to game
+                    //'exit' option NO - returns to game
                     if((buttonFlagA)&&(exitOption == 1)) {
                         buttonFlagA = 0;//resets flags
                         buttonFlagB = 0;
                         break;
                     }
-                    //sleep();//put while to sleep
+                    sleep();//put while to sleep
                 }
-                // if 'exit' option YES, resets
-                // game values returns to main menu
+                //if 'exit' option YES, resets
+                //game values returns to main menu
                 if (exitFlag!=0) { //if exit flag set
                     exitFlag = 0;//reset flag
                     break;//break to main menu
@@ -1145,15 +1153,15 @@
     }
 }
 
-// high scores screen
+//high scores screen
 void scores()
 {
     actionButtons();
     lcd.clear();//clear screen
-    backGround();//set background
+    drawBackground();//set background
     lcd.printString("High Scores",10,0);//title
 
-    // prints scores with names
+    //prints scores with names
     lcd.printString(player1initials,5,2);//display
 
     lcd.printString(player2initials,5,3);//display
@@ -1163,18 +1171,18 @@
     while(1) {
         actionButtons();//select
 
-        // back to menu
+        //back to menu
         if(buttonFlagB) {
             buttonFlagA = 0;//reset flags
             buttonFlagB = 0;
             lcd.clear();
             break;
         }
-        //sleep();//put while to sleep
+        sleep();//put while to sleep
     }
 }
 
-// options menu
+//options menu
 void optionsMenu()
 {
     int option = 0;
@@ -1182,13 +1190,13 @@
     int fxOption = 0;
     actionButtons();
     drawOptionsMenu();//draws options menu
-    // counters for navigation
+    //counters for navigation
 
     while(1) {
         actionButtons();
         optionsMenu(option);//presents options
 
-////////////////////// difficulty menu ////////////////////////////////////
+//////////////////////difficulty menu ////////////////////////////////////
         if ((option == 0)&&(buttonFlagA)) {
             buttonFlagA = 0;//reset flag
             actionButtons();
@@ -1204,11 +1212,11 @@
                     lcd.clear();//clear screen
                     break;//return back
                 }
-                //sleep();//put while to sleep
+                sleep();//put while to sleep
             }
             drawOptionsMenu();
         }
-///////////////////// sound FX menu //////////////////////////////////////
+/////////////////////sound FX menu //////////////////////////////////////
         if((option ==1)&&(buttonFlagA)) {
             buttonFlagA = 0;//reset flags
             buttonFlagB = 0;
@@ -1219,33 +1227,33 @@
                 actionButtons();
                 soundFXMenu(fxOption);//presents options
 
-                // back to options menu
+                //back to options menu
                 if(buttonFlagB) {
                     buttonFlagB = 0;//reset flags
                     buttonFlagA = 0;
                     lcd.clear();//clear screen
                     break;//return back
                 }
-                //sleep();//put while to sleep
+                sleep();//put while to sleep
             }
             drawOptionsMenu();
         }
-        // back to mainmenu
+        //back to mainmenu
         if(buttonFlagB) {
             buttonFlagB = 0;//reset flags
             buttonFlagA = 0;
             lcd.clear();//clear
             break;//return back
         }
-        //sleep();//put while to sleep
+        sleep();//put while to sleep
     }
 }
 
-// read default positions of the joystick to calibrate later readings
+//read default positions of the joystick to calibrate later readings
 void calibrateJoystick()
 {
     joyButton.mode(PullDown);
-    // must not move during calibration
+    //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;
 }
@@ -1253,14 +1261,14 @@
 //reads and updates position of joystick according to voltage readings
 void updateJoystick()
 {
-    // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred)
+    //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
+    //read button state
     joystick.button = joyButton;
 
-    // calculate direction depending on x,y values
-    // tolerance allows a little lee-way in case joystick not exactly in the stated direction
+    //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) {
@@ -1275,6 +1283,6 @@
         joystick.direction = UNKNOWN;
     }
 
-    // set flag for printing
+    //set flag for printing
     printFlag = 1;
 }
\ No newline at end of file