Snake vs Block Game to be run upon K64F.

Dependencies:   mbed

Revision:
83:329da564799a
Parent:
80:51ca38c5dcdf
Child:
84:9950d561fdf8
diff -r c51ae8a501d1 -r 329da564799a MenuClasses/StartScreen/StartScreen.cpp
--- a/MenuClasses/StartScreen/StartScreen.cpp	Mon May 06 10:49:50 2019 +0000
+++ b/MenuClasses/StartScreen/StartScreen.cpp	Mon May 06 14:28:35 2019 +0000
@@ -135,8 +135,13 @@
 
 }
 
-void StartScreen::init()
+void StartScreen::init(N5110 *lcd, Gamepad *pad)
 {
+    _lcd = lcd;
+    _pad = pad;
+    _stats.init(_lcd, _pad);
+    _settings.init(_lcd, _pad);
+    _tutorial.init(_lcd, _pad);
     fps = 15; //sets the default frames per second to a certain value.
     menu_index = 16; //main menu selection arrow position
     g_mode = 1; //game mode selection (joystick/motion control).
@@ -145,21 +150,21 @@
     controlSensitivity = 5; //sensitivity of control speed of menu options.
 }
 
-void StartScreen::titleScreen(N5110 &lcd, Gamepad &pad)
+void StartScreen::titleScreen()
 {
     //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};
-    pad.leds_on();
+    _pad->leds_on();
 
     for(int ani = -51; ani <= 0; ani+=3)  {
-        lcd.clear();
-        lcd.drawSprite(0,ani,48,84,(int *)titleSprite); //Function used to draw the sprite
-        lcd.refresh();
-        pad.tone(song[(ani+48)/3],0.1);
+        _lcd->clear();
+        _lcd->drawSprite(0,ani,48,84,(int *)titleSprite); //Function used to draw the sprite
+        _lcd->refresh();
+        _pad->tone(song[(ani+48)/3],0.1);
 
         //This Allows us to skip/play the animation before the menu screen
         _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.
+        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.
             _startPressed = true;
             break;
         }
@@ -168,44 +173,44 @@
     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();
-    lcd.clear();
+    _pad->leds_off();
+    _lcd->clear();
 
-    StartScreen::instruct(lcd,pad); //after the screen_saver is displayed we need to send in instructions of how to preoceed to menu.
+    StartScreen::instruct(); //after the screen_saver is displayed we need to send in instructions of how to preoceed to menu.
 }
 
-void StartScreen::instruct(N5110 &lcd, Gamepad &pad)
+void StartScreen::instruct()
 {
-    //pad.init();
+    //_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)&&(_startPressed == false)) {
+    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 in titleScreen();
-        lcd.printString("Please Press",5,1); //Function used to promt the user to start.
-        lcd.printString("Start",25,3);
-        lcd.printString("to Begin",15,5);
-        lcd.refresh();
-        pad.leds_on();
+        _lcd->printString("Please Press",5,1); //Function used to promt the user to start.
+        _lcd->printString("Start",25,3);
+        _lcd->printString("to Begin",15,5);
+        _lcd->refresh();
+        _pad->leds_on();
         wait(0.1);
-        pad.leds_off();
+        _pad->leds_off();
         wait(0.1);
     }
-    pad.tone(1000.0,0.1);
-    lcd.clear();
-    pad.leds_off();
+    _pad->tone(1000.0,0.1);
+    _lcd->clear();
+    _pad->leds_off();
 }
 
-void StartScreen::credits(N5110 &lcd)
+void StartScreen::credits()
 {
-    lcd.clear();
+    _lcd->clear();
     if(showCredits)  {
-        lcd.printString("Created BY",8,1);
-        lcd.printString("Ahmed",24,3);
-        lcd.printString("Adamjee",20,4);
-        lcd.printString("201161436",15,5);
-        lcd.refresh();
+        _lcd->printString("Created BY",8,1);
+        _lcd->printString("Ahmed",24,3);
+        _lcd->printString("Adamjee",20,4);
+        _lcd->printString("201161436",15,5);
+        _lcd->refresh();
         wait(2);
     }
-    lcd.clear();
+    _lcd->clear();
 }
 
 void StartScreen::read_stats(SDFileSystem &sd) //to read the data from the SD card at the start of the game to be able to display before starting game.
@@ -213,11 +218,11 @@
     _stats.read(sd);
 }
 
-bool StartScreen::checkBackPressed(Gamepad &pad)
+bool StartScreen::checkBackPressed()
 {
     //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);
+    if (_pad->check_event(Gamepad::BACK_PRESSED) == true) {
+        _pad->tone(1000.0,0.1);
         _backPressed = true;
     } else {
         _backPressed = false;
@@ -225,69 +230,69 @@
     return _backPressed; //returns true if back is pressed, returns false otherwise.
 }
 
-void StartScreen::menu(N5110 &lcd, Gamepad &pad)
+void StartScreen::menu()
 {
-    lcd.clear();
+    _lcd->clear();
     //StartScreen::init();
-    //pad.init();
-    while ((pad.check_event(Gamepad::A_PRESSED) == false)&&(pad.check_event(Gamepad::START_PRESSED) == false)) {
+    //_pad->init();
+    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();
+        _lcd->clear();
+        StartScreen::mainMenu();
+        _lcd->refresh();
         
     }
-    pad.tone(1000.0,0.1);
-    StartScreen::nextMenu(lcd, pad); //after all main menu operations have been completed, this function allows us to get to the next menu.
+    _pad->tone(1000.0,0.1);
+    StartScreen::nextMenu(); //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) //Displays the main menu contents, and then allows the user to select the next menu using the selection arrow.
+void StartScreen::mainMenu() //Displays the main menu contents, and then allows the user to select the next menu using the selection arrow.
 {
-    StartScreen::drawSelectionArrow(lcd, pad); //draws arrow to select menu option.
+    StartScreen::drawSelectionArrow(); //draws arrow to select menu option.
 
-    lcd.printString("Snake vs Block",0,0);
-    lcd.printString("Play",0,2);
-    lcd.printString("Settings",0,3);
-    lcd.printString("Stats",0,4);
-    lcd.printString("Tutorial",0,5);
+    _lcd->printString("Snake vs Block",0,0);
+    _lcd->printString("Play",0,2);
+    _lcd->printString("Settings",0,3);
+    _lcd->printString("Stats",0,4);
+    _lcd->printString("Tutorial",0,5);
 }
 
-void StartScreen::drawSelectionArrow(N5110 &lcd, Gamepad &pad)
+void StartScreen::drawSelectionArrow()
 {
     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).
+    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().
     }
-    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).
+    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.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.
+    _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)
+void StartScreen::nextMenu()
 {
     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 after play has been pressed.
-            game_speed(lcd, pad); //calls game speed after the game mode has been selected.
+            game_mode(); //calls game mode after play has been pressed.
+            game_speed(); //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;
-            cs_sc = _settings.Implement(lcd, pad, controlSensitivity);
+            cs_sc = _settings.Implement(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 logic in showCredits which decides weather or not to show credits when game starts.
-            menu(lcd, pad); //to return to main_menu if back is pressed
+            menu(); //to return to main_menu if back is pressed
             break;
         case 32:
-            _stats.StatsDisplay(lcd, pad);
-            menu(lcd, pad); //to return to main_menu if back is pressed
+            _stats.StatsDisplay();
+            menu(); //to return to main_menu if back is pressed
             break;
         case 40:
-            _tutorial.Implement(lcd, pad);
-            menu(lcd, pad); //to return to main_menu if back is pressed
+            _tutorial.Implement();
+            menu(); //to return to main_menu if back is pressed
             break;
     }
 }
@@ -295,136 +300,136 @@
 
 //////////////////////////////GAME MODE SELECTION//////////////////////////////
 
-void StartScreen::game_mode(N5110 &lcd, Gamepad &pad)
+void StartScreen::game_mode()
 {
-    lcd.clear();
-    //pad.init();
-    while ((pad.check_event(Gamepad::A_PRESSED) == false)&&(pad.check_event(Gamepad::START_PRESSED) == false)) {  //Change this to if button pressed.
+    _lcd->clear();
+    //_pad->init();
+    while ((_pad->check_event(Gamepad::A_PRESSED) == false)&&(_pad->check_event(Gamepad::START_PRESSED) == false)) {  //Change this to if button pressed.
 
-        lcd.clear();
-        StartScreen::selectMode(lcd, pad); //displays to the user what are the mode options
-        lcd.refresh();
+        _lcd->clear();
+        StartScreen::selectMode(); //displays to the user what are the mode options
+        _lcd->refresh();
 
-        if (StartScreen::checkBackPressed(pad)) {
+        if (StartScreen::checkBackPressed()) {
             break;    //if back is pressed, the while loop is escaped.
         }
 
     }
-    pad.tone(1000.0,0.1);
+    _pad->tone(1000.0,0.1);
 
     if(_backPressed) { //if back was pressed in the while loop, the previous menu is called.
-        StartScreen::menu(lcd, pad);
+        StartScreen::menu();
     }
 }
 
-void StartScreen::selectMode(N5110 &lcd, Gamepad &pad)
+void StartScreen::selectMode()
 {
-    StartScreen::scrollModeSelection(pad);  //the mode is selected by moving joystick in this function.
+    StartScreen::scrollModeSelection();  //the mode is selected by moving joystick in this function.
     switch (g_mode) {
         case 1:
-            lcd.printString("Joystick",18,1);
-            lcd.drawSprite(17,22,13,48,(int *)joystick); //Function used to draw the sprite.
-            lcd.refresh();
+            _lcd->printString("Joystick",18,1);
+            _lcd->drawSprite(17,22,13,48,(int *)joystick); //Function used to draw the sprite.
+            _lcd->refresh();
             break;
         case 2:
-            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("Motion Control",1,1);
-            lcd.drawSprite(32,22,12,21,(int *)motionControl); //Function used to draw the sprite.
+            _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("Motion Control",1,1);
+            _lcd->drawSprite(32,22,12,21,(int *)motionControl); //Function used to draw the sprite.
             break;
     }
 
 }
 
-void StartScreen::scrollModeSelection(Gamepad &pad)
+void StartScreen::scrollModeSelection()
 {
-    Direction d = pad.get_direction();
+    Direction d = _pad->get_direction();
     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))  {
+    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;
     }
 
-    else if(((d==E)||(d==W)||(pad.check_event(Gamepad::R_PRESSED) == true)||(pad.check_event(Gamepad::L_PRESSED) == true))&&(g_mode==2))  {
+    else if(((d==E)||(d==W)||(_pad->check_event(Gamepad::R_PRESSED) == true)||(_pad->check_event(Gamepad::L_PRESSED) == true))&&(g_mode==2))  {
         g_mode=1;
     }
 }
 
 //////////////////////////////INSTRUCTIONS FOR MOTION CONTROL//////////////////////////////
 
-void StartScreen::motionControlInstructions(N5110 &lcd)
+void StartScreen::motionControlInstructions()
 {
-    lcd.clear();
-    lcd.printString("While playing the",5,0); //Function used to promt the user to start.
-    lcd.printString("game, press",9,1);
-    lcd.printString("A to centre",8,2);
-    lcd.printString("the snake at",9,3);
-    lcd.printString("prefered angle",0,4);
-    lcd.refresh();
+    _lcd->clear();
+    _lcd->printString("While playing the",5,0); //Function used to promt the user to start.
+    _lcd->printString("game, press",9,1);
+    _lcd->printString("A to centre",8,2);
+    _lcd->printString("the snake at",9,3);
+    _lcd->printString("prefered angle",0,4);
+    _lcd->refresh();
     wait(2);
 }
 
 
 //////////////////////////////GAME SPEED SELECTION//////////////////////////////
 
-void StartScreen::game_speed(N5110 &lcd, Gamepad &pad)
+void StartScreen::game_speed()
 {
-    lcd.clear();
-    //pad.init();
-    while ((pad.check_event(Gamepad::A_PRESSED) == false)&&(pad.check_event(Gamepad::START_PRESSED) == false)) {  //Change this to if button pressed.
+    _lcd->clear();
+    //_pad->init();
+    while ((_pad->check_event(Gamepad::A_PRESSED) == false)&&(_pad->check_event(Gamepad::START_PRESSED) == false)) {  //Change this to if button pressed.
 
-        lcd.clear();
-        StartScreen::selectSpeed(lcd, pad); //this displays all the speed options and saves the data obtained (fps) for use in main().
-        lcd.refresh();
+        _lcd->clear();
+        StartScreen::selectSpeed(); //this displays all the speed options and saves the data obtained (fps) for use in main().
+        _lcd->refresh();
 
-        if (StartScreen::checkBackPressed(pad)) {
+        if (StartScreen::checkBackPressed()) {
             break;    //if back is pressed, the while loop is escaped.
         }
     }
-    pad.tone(1000.0,0.1);
+    _pad->tone(1000.0,0.1);
 
     if(_backPressed) { //if back was pressed in the while loop, the previous menu is called.
-        StartScreen::menu(lcd, pad); //To go back to Menu if back is pressed.
+        StartScreen::menu(); //To go back to Menu if back is pressed.
     }
 
 }
 
-void StartScreen::selectSpeed(N5110 &lcd, Gamepad &pad) //decrease code size.
+void StartScreen::selectSpeed() //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);
+    StartScreen::scrollSpeedSelection(); //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);
+            _lcd->printString("SNAKE",28,3);
             fps = 15;
             break;
         case 2:
-            lcd.printString("HUMAN",28,3);
+            _lcd->printString("HUMAN",28,3);
             fps = 22;
             break;
         case 3:
-            lcd.printString("HORSE",28,3);
+            _lcd->printString("HORSE",28,3);
             fps = 26;
             break;
         case 4:
-            lcd.printString("CHEETHA",21,3);
+            _lcd->printString("CHEETHA",21,3);
             fps = 30;
     }
 
 }
 
-void StartScreen::scrollSpeedSelection(Gamepad &pad)
+void StartScreen::scrollSpeedSelection()
 {
-    Direction d = pad.get_direction();
+    Direction d = _pad->get_direction();
     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))  {
+    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))  {
+    } else if(((d==W)||(_pad->check_event(Gamepad::L_PRESSED) == true))&&(speed_index==1))  {
         speed_index=4;
-    } else if(((d==E)||(pad.check_event(Gamepad::R_PRESSED) == true))&&(1<=speed_index<=3))  {
+    } else if(((d==E)||(_pad->check_event(Gamepad::R_PRESSED) == true))&&(1<=speed_index<=3))  {
         speed_index=speed_index+1;
-    } else if(((d==W)||(pad.check_event(Gamepad::L_PRESSED) == true))&&(2<=speed_index<=4))  {
+    } else if(((d==W)||(_pad->check_event(Gamepad::L_PRESSED) == true))&&(2<=speed_index<=4))  {
         speed_index=speed_index-1;
     }
 }