Snake vs Block Game to be run upon K64F.

Dependencies:   mbed

Revision:
76:7fa91122907f
Parent:
68:b9cfd27987ac
Child:
80:51ca38c5dcdf
--- a/MenuClasses/StartScreen/StartScreen.cpp	Sun May 05 13:04:09 2019 +0000
+++ b/MenuClasses/StartScreen/StartScreen.cpp	Sun May 05 15:00:34 2019 +0000
@@ -230,18 +230,19 @@
     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.
+    while ((pad.check_event(Gamepad::A_PRESSED) == false)&&(pad.check_event(Gamepad::START_PRESSED) == false)) {
+        //This menu loop won't be skipped unless the player decides to press START/A.
+        lcd.clear();
         StartScreen::mainMenu(lcd, pad);
+        lcd.refresh();
+        
     }
     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.
 }
 
-void StartScreen::mainMenu(N5110 &lcd, Gamepad &pad)
+void StartScreen::mainMenu(N5110 &lcd, Gamepad &pad) //Displays the main menu contents, and then allows the user to select the next menu using the selection arrow.
 {
-    lcd.clear();
-    
     StartScreen::drawSelectionArrow(lcd, pad); //draws arrow to select menu option.
 
     lcd.printString("Snake vs Block",0,0);
@@ -249,12 +250,11 @@
     lcd.printString("Settings",0,3);
     lcd.printString("Stats",0,4);
     lcd.printString("Tutorial",0,5);
-    lcd.refresh();
-    wait(1/controlSensitivity);
 }
 
 void StartScreen::drawSelectionArrow(N5110 &lcd, Gamepad &pad)
 {
+    wait(1/controlSensitivity);
     Direction d = pad.get_direction();
     if(((d==N)||(pad.check_event(Gamepad::L_PRESSED) == true))&&(menu_index>16))  {
         menu_index=menu_index-8; // menu_index is the position of the selection arrow in menu and also the index of the case structure to select the next menu in StartScreen::NextMenu(lcd, pad).
@@ -262,15 +262,17 @@
     if(((d==S)||(pad.check_event(Gamepad::R_PRESSED) == true))&&(menu_index<40))  {
         menu_index=menu_index+8; // menu_index is the position of the selection arrow in menu and also the index of the case structure to select the next menu in StartScreen::NextMenu(lcd, pad).
     }
-    lcd.drawSprite(65,menu_index,7,9,(int *)arrow); // menu_index is the postion of where the arrow will be drawn and is determined by the above if command using joystick.
+    lcd.drawSprite(60,menu_index,7,9,(int *)arrow); // menu_index is the postion of where the arrow will be drawn and is determined by the above if command using joystick.
 }
 
+//////////////////////////////DECIDES THE NEXT MENU PAGE//////////////////////////////
+
 void StartScreen::nextMenu(N5110 &lcd, Gamepad &pad)
 {
     switch (menu_index) { //this menu_index saves the value from the coordinates of arrow in main menu, and allows us to select the next menu accordingly.
         case 16:
-            game_mode(lcd, pad); //calls game mode
-            game_speed(lcd, pad); //calls game speed
+            game_mode(lcd, pad); //calls game mode after play has been pressed.
+            game_speed(lcd, pad); //calls game speed after the game mode has been selected.
             return; //This forces us out of the menu class and back into int main().
         case 24:
             float *cs_sc;
@@ -290,14 +292,18 @@
     }
 }
 
+
+//////////////////////////////GAME MODE SELECTION//////////////////////////////
+
 void StartScreen::game_mode(N5110 &lcd, Gamepad &pad)
 {
     lcd.clear();
-    wait(2/controlSensitivity);
     //pad.init();
     while ((pad.check_event(Gamepad::A_PRESSED) == false)&&(pad.check_event(Gamepad::START_PRESSED) == false)) {  //Change this to if button pressed.
-        StartScreen::showModeOptions(lcd); //displays to the user what are the mode options
-        StartScreen::selectMode(pad);  //the mode is selected by moving joystick in this function.
+
+        lcd.clear();
+        StartScreen::selectMode(lcd, pad); //displays to the user what are the mode options
+        lcd.refresh();
 
         if (StartScreen::checkBackPressed(pad)) {
             break;    //if back is pressed, the while loop is escaped.
@@ -305,15 +311,15 @@
 
     }
     pad.tone(1000.0,0.1);
-    
+
     if(_backPressed) { //if back was pressed in the while loop, the previous menu is called.
         menu(lcd, pad);
     }
 }
 
-void StartScreen::showModeOptions(N5110 &lcd)
+void StartScreen::selectMode(N5110 &lcd, Gamepad &pad)
 {
-    lcd.clear();
+    StartScreen::scrollModeSelection(pad);  //the mode is selected by moving joystick in this function.
     switch (g_mode) {
         case 1:
             lcd.printString("Joystick",18,1);
@@ -328,13 +334,12 @@
             break;
     }
 
-    lcd.refresh();
 }
 
-void StartScreen::selectMode(Gamepad &pad)
+void StartScreen::scrollModeSelection(Gamepad &pad)
 {
     Direction d = pad.get_direction();
-    wait(1/controlSensitivity);
+    wait(1/controlSensitivity);//to make the joystick selection's speed inversely proportional to controlSensitivity.
     if(((d==E)||(d==W)||(pad.check_event(Gamepad::R_PRESSED) == true)||(pad.check_event(Gamepad::L_PRESSED) == true))&&(g_mode==1))  {
         g_mode=2;
     }
@@ -344,6 +349,8 @@
     }
 }
 
+//////////////////////////////INSTRUCTIONS FOR MOTION CONTROL//////////////////////////////
+
 void StartScreen::motionControlInstructions(N5110 &lcd)
 {
     lcd.clear();
@@ -356,14 +363,18 @@
     wait(2);
 }
 
+
+//////////////////////////////GAME SPEED SELECTION//////////////////////////////
+
 void StartScreen::game_speed(N5110 &lcd, Gamepad &pad)
 {
     lcd.clear();
-    wait(2/controlSensitivity);
     //pad.init();
     while ((pad.check_event(Gamepad::A_PRESSED) == false)&&(pad.check_event(Gamepad::START_PRESSED) == false)) {  //Change this to if button pressed.
-        StartScreen::selectSpeedPage(pad); //this swipes through the speed pages, dependent on the joystick input.
-        StartScreen::showSpeedOptions(lcd); //this displays all the speed options and saves the data obtained (fps) for use in main().
+
+        lcd.clear();
+        StartScreen::selectSpeed(lcd, pad); //this displays all the speed options and saves the data obtained (fps) for use in main().
+        lcd.refresh();
 
         if (StartScreen::checkBackPressed(pad)) {
             break;    //if back is pressed, the while loop is escaped.
@@ -377,10 +388,36 @@
 
 }
 
-void StartScreen::selectSpeedPage(Gamepad &pad)
+void StartScreen::selectSpeed(N5110 &lcd, Gamepad &pad) //decrease code size.
+{
+    StartScreen::scrollSpeedSelection(pad); //this swipes through the speed pages, dependent on the joystick input.
+    lcd.drawSprite(10,22,13,8,(int *)navL); //Function used to draw the sprite.
+    lcd.drawSprite(65,22,13,8,(int *)navR); //Function used to draw the sprite.
+    lcd.printString("Game Speed",10,1);
+    switch (speed_index) {
+        case 1:
+            lcd.printString("SNAKE",28,3);
+            fps = 15;
+            break;
+        case 2:
+            lcd.printString("HUMAN",28,3);
+            fps = 22;
+            break;
+        case 3:
+            lcd.printString("HORSE",28,3);
+            fps = 26;
+            break;
+        case 4:
+            lcd.printString("CHEETHA",21,3);
+            fps = 30;
+    }
+
+}
+
+void StartScreen::scrollSpeedSelection(Gamepad &pad)
 {
     Direction d = pad.get_direction();
-    wait(1/controlSensitivity);
+    wait(1/controlSensitivity);//to make the joystick selection's speed inversely proportional to controlSensitivity.
     if(((d==E)||(pad.check_event(Gamepad::R_PRESSED) == true))&&(speed_index==4))  {
         speed_index=1;
     } else if(((d==W)||(pad.check_event(Gamepad::L_PRESSED) == true))&&(speed_index==1))  {
@@ -391,30 +428,3 @@
         speed_index=speed_index-1;
     }
 }
-
-void StartScreen::showSpeedOptions(N5110 &lcd) //decrease code size.
-{
-    lcd.clear();
-    lcd.drawSprite(10,22,13,8,(int *)navL); //Function used to draw the sprite.
-    lcd.drawSprite(65,22,13,8,(int *)navR); //Function used to draw the sprite.
-    lcd.printString("Game Speed",10,1);
-    switch (speed_index) {
-        case 1:
-            lcd.printString("SNAKE",28,3);
-            fps = 12;
-            break;
-        case 2:
-            lcd.printString("HUMAN",28,3);
-            fps = 18;
-            break;
-        case 3:
-            lcd.printString("HORSE",28,3);
-            fps = 22;
-            break;
-        case 4:
-            lcd.printString("CHEETHA",21,3);
-            fps = 27;
-    }
-
-    lcd.refresh();
-}
\ No newline at end of file