Snake vs Block Game to be run upon K64F.

Dependencies:   mbed

Revision:
66:e47333ffc6ca
Parent:
65:2872ca289b49
Child:
67:39b9ba6019b0
--- a/MenuClasses/StartScreen/StartScreen.cpp	Fri May 03 19:16:02 2019 +0000
+++ b/MenuClasses/StartScreen/StartScreen.cpp	Fri May 03 22:39:38 2019 +0000
@@ -145,7 +145,7 @@
     controlSensitivity = 5; //sensitivity of control speed of menu options.
 }
 
-void StartScreen::screen_saver(N5110 &lcd, Gamepad &pad)
+void StartScreen::titleScreen(N5110 &lcd, Gamepad &pad)
 {
     //this saves the Theme song I want to play at the start.
     int song[18] = {441,494,439,372,392,370,330,246,329,331,370,555,555,495,440,392,392,370};
@@ -158,14 +158,14 @@
         pad.tone(song[(ani+48)/3],0.1);
 
         //This Allows us to skip/play the animation before the menu screen
-        Start_Pressed = 0; //by default, animmation is not skipped, as StartScreen::instruct(lcd,pad) requires Start_Pressed to be 0 to be displayed
+        _startPressed = false; //by default, animmation is not skipped, as StartScreen::instruct(lcd,pad) requires Start_Pressed to be 0 to be displayed
         if((pad.check_event(Gamepad::START_PRESSED) == true)||(pad.check_event(Gamepad::A_PRESSED) == true)) { //to allow the gamer to skip the long starting animation if they are not a fan.
-            Start_Pressed = 1;
+            _startPressed = true;
             break;
         }
         wait(0.2);
     }
-    if (Start_Pressed == 0) {
+    if (_startPressed == false) {
         wait(0.3);    //dont wait anymore if start is pressed. (this is to allow for quick skip towards menu)
     }
     pad.leds_off();
@@ -178,7 +178,7 @@
 {
     //pad.init();
     wait(1/controlSensitivity); //to avoid race condition of the following while loop being skipped.
-    while ((pad.check_event(Gamepad::START_PRESSED) == false)&&(pad.check_event(Gamepad::A_PRESSED) == false)&&(Start_Pressed == 0)) {
+    while ((pad.check_event(Gamepad::START_PRESSED) == false)&&(pad.check_event(Gamepad::A_PRESSED) == false)&&(_startPressed == false)) {
         //Start_Pressed is 1 if start is already pressed during animation
         lcd.printString("Please Press",5,1); //Function used to promt the user to start.
         lcd.printString("Start",25,3);
@@ -213,20 +213,32 @@
     _stats.read(sd);
 }
 
-void StartScreen::main_menu(N5110 &lcd, Gamepad &pad)
+bool StartScreen::checkBackPressed(Gamepad &pad) //to read the data from the SD card at the start of the game to be able to display before starting game.
+{
+    //This is a very important process, this helps the code to not get stuck in  menu loop but also allow to toggle between diffrent menu screens.
+    if (pad.check_event(Gamepad::BACK_PRESSED) == true) {
+        pad.tone(1000.0,0.1);
+        _backPressed = true;
+    } else {
+        _backPressed = false;
+    }
+    return _backPressed; //returns 1 if back is pressed, returns 0 otherwise.
+}
+
+void StartScreen::menuScreen(N5110 &lcd, Gamepad &pad)
 {
     lcd.clear();
     //StartScreen::init();
     //pad.init();
     while ((pad.check_event(Gamepad::A_PRESSED) == false)&&(pad.check_event(Gamepad::START_PRESSED) == false)) {  //Change this to if button pressed.
-        StartScreen::ImplementMenu(lcd, pad);
+        StartScreen::mainMenu(lcd, pad);
     }
     pad.tone(1000.0,0.1);
     wait(1/controlSensitivity);
-    StartScreen::NextMenu(lcd, pad); //after all main menu operations have been completed, this function allows us to get to the next menu.
+    StartScreen::nextMenu(lcd, pad); //after all main menu operations have been completed, this function allows us to get to the next menu.
 }
 
-void StartScreen::ImplementMenu(N5110 &lcd, Gamepad &pad)
+void StartScreen::mainMenu(N5110 &lcd, Gamepad &pad)
 {
     Direction d = pad.get_direction();
     if(((d==N)||(pad.check_event(Gamepad::L_PRESSED) == true))&&(i>16))  {
@@ -246,7 +258,7 @@
     lcd.clear();
 }
 
-void StartScreen::NextMenu(N5110 &lcd, Gamepad &pad)
+void StartScreen::nextMenu(N5110 &lcd, Gamepad &pad)
 {
     switch (i) { //this i saves the value from the coordinates of arrow in main menu, and allows us to select the next menu accordingly.
         case 16:
@@ -257,15 +269,15 @@
             cs_sc = _settings.Implement(lcd, pad, controlSensitivity);
             controlSensitivity = cs_sc[0]; //saves the number in controlSensitivity which controls the control speed of the menu oftions (basically the wait time of between reading joystick input).
             showCredits = cs_sc[1]; //saves the number in sc which decides weather or not to show credits when game starts.
-            main_menu(lcd, pad); //to return to main_menu if back is pressed
+            menuScreen(lcd, pad); //to return to main_menu if back is pressed
             break;
         case 32:
             _stats.StatsDisplay(lcd, pad);
-            main_menu(lcd, pad); //to return to main_menu if back is pressed
+            menuScreen(lcd, pad); //to return to main_menu if back is pressed
             break;
         case 40:
             _tutorial.Implement(lcd, pad);
-            main_menu(lcd, pad); //to return to main_menu if back is pressed
+            menuScreen(lcd, pad); //to return to main_menu if back is pressed
             break;
     }
 }
@@ -279,18 +291,12 @@
         StartScreen::ModeOptions(lcd); //displays to the user what are the mode options
         StartScreen::SelectMode(pad);  //the mode is selected by moving joystick in this function.
 
-        //This is a very important process, this helps the code to not get stuck in  menu loop.
-        if (pad.check_event(Gamepad::BACK_PRESSED) == true) {
-            pad.tone(1000.0,0.1);
-            back_pressed = 1;
-            break; //if back is pressed, the while loop is escaped.
-        } else {
-            back_pressed = 0;
-        }
+        if (StartScreen::checkBackPressed(pad)) { break; }   //if back is pressed, the while loop is escaped.
+
     }
     pad.tone(1000.0,0.1);
-    if(back_pressed) { //if back was pressed in the while loop, the previous menu is called.
-        main_menu(lcd, pad);
+    if(_backPressed) { //if back was pressed in the while loop, the previous menu is called.
+        menuScreen(lcd, pad);
     }
 
     else {
@@ -356,18 +362,11 @@
     while ((pad.check_event(Gamepad::A_PRESSED) == false)&&(pad.check_event(Gamepad::START_PRESSED) == false)) {  //Change this to if button pressed.
         StartScreen::selectSpeedPage(pad);
         StartScreen::showSpeedOptions(lcd);
-
-        //This is a very important process, this helps the code to not get stuck in  menu loop.
-        if (pad.check_event(Gamepad::BACK_PRESSED) == true) {
-            pad.tone(1000.0,0.1);
-            back_pressed = 1;
-            break; //if back is pressed, the while loop is escaped.
-        } else {
-            back_pressed = 0;
-        }
+        
+        if (StartScreen::checkBackPressed(pad)) { break; }   //if back is pressed, the while loop is escaped.
     }
 
-    if(back_pressed) { //if back was pressed in the while loop, the previous menu is called.
+    if(_backPressed) { //if back was pressed in the while loop, the previous menu is called.
         game_mode(lcd, pad);
     }