Snake vs Block Game to be run upon K64F.

Dependencies:   mbed

Revision:
62:ebf6ecf8a6d5
Parent:
61:f3c402bc2ad0
Child:
63:205f0ca48473
--- a/MenuClasses/StartScreen/StartScreen.cpp	Tue Apr 30 08:32:30 2019 +0000
+++ b/MenuClasses/StartScreen/StartScreen.cpp	Tue Apr 30 14:53:38 2019 +0000
@@ -51,7 +51,7 @@
     {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
 
 };
-    
+
 int arrow[7][9] = {
     {0,0,0,0,1,0,0,0,0},
     {0,0,0,1,1,0,0,0,0},
@@ -61,7 +61,7 @@
     {0,0,0,1,1,0,0,0,0},
     {0,0,0,0,1,0,0,0,0},
 };
-    
+
 int motionControl[12][21] = {
     {0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0},
     {0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0},
@@ -127,22 +127,23 @@
 
 StartScreen::StartScreen()
 {
-    
+
 }
 
 StartScreen::~StartScreen()
 {
-    
+
 }
 
 void StartScreen::init()
 {
+    fps = 40; //sets the default frames per second to a certain value.
     i = 16; //main menu selection arrow position
     g_mode = 1; //game mode selection (joystick/motion control).
     gs = 2; //game speed page selection.
     st = 1; //this is the sr num of settings pages.
     sc = 1; //this helps in wether ot not to show credits to the user.
-    cs = 4; //sensitivity of control speed of menu options.
+    cs = 5; //sensitivity of control speed of menu options.
 }
 
 void StartScreen::screen_saver(N5110 &lcd, Gamepad &pad)
@@ -150,7 +151,7 @@
     //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();
-    
+
     for(int ani = -51; ani <= 0; ani+=3)  {
         lcd.clear();
         lcd.drawSprite(0,ani,48,84,(int *)sprite); //Function used to draw the sprite
@@ -163,10 +164,12 @@
         }
         wait(0.2);
     }
-    if (Start_Pressed == 0) { wait(0.3); } //dont wait anymore if start is pressed.
+    if (Start_Pressed == 0) {
+        wait(0.3);    //dont wait anymore if start is pressed.
+    }
     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.
 }
 
@@ -187,9 +190,9 @@
     lcd.clear();
     pad.leds_off();
 }
-    
+
 void StartScreen::credits(N5110 &lcd)
-{   
+{
     lcd.clear();
     if(sc)  {
         lcd.printString("Created BY",8,1);
@@ -221,7 +224,7 @@
 }
 
 void StartScreen::ImplementMenu(N5110 &lcd, Gamepad &pad)
-{   
+{
     lcd.printString("SnakeVSBlock",5,0);
     lcd.printString("Play",0,2);
     lcd.printString("Settings",0,3);
@@ -229,10 +232,10 @@
     lcd.printString("Tutorial",0,5);
     Direction d = pad.get_direction();
     if(((d==N)||(pad.check_event(Gamepad::L_PRESSED) == true))&&(i>16))  {
-            i=i-8;
+        i=i-8;
     }
     if(((d==S)||(pad.check_event(Gamepad::R_PRESSED) == true))&&(i<40))  {
-            i=i+8;
+        i=i+8;
     }
     lcd.drawSprite(50,i,7,9,(int *)arrow); //Function used to draw the sprite.
     lcd.refresh();
@@ -243,24 +246,24 @@
 void StartScreen::NextMenu(N5110 &lcd, Gamepad &pad)
 {
     switch (i) {
-    case 16:
-        game_mode(lcd, pad);
-        break;
-    case 24:
-        float *cs_sc;
-        cs_sc = _settings.Implement(lcd, pad, cs);
-        cs = cs_sc[0]; //saves the number in cs which controls the control speed of the menu oftions (basically the wait time of between reading joystick input).
-        sc = 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
-        break;
-    case 32:
-        _stats.StatsDisplay(lcd, pad);
-        main_menu(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
-        break;
+        case 16:
+            game_mode(lcd, pad);
+            break;
+        case 24:
+            float *cs_sc;
+            cs_sc = _settings.Implement(lcd, pad, cs);
+            cs = cs_sc[0]; //saves the number in cs which controls the control speed of the menu oftions (basically the wait time of between reading joystick input).
+            sc = 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
+            break;
+        case 32:
+            _stats.StatsDisplay(lcd, pad);
+            main_menu(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
+            break;
     }
 }
 
@@ -276,14 +279,19 @@
             pad.tone(1000.0,0.1);
             back_pressed = 1;
             break;
+        } else {
+            back_pressed = 0;
         }
-        else {back_pressed = 0;}    
     }
     pad.tone(1000.0,0.1);
-    if(back_pressed) { main_menu(lcd, pad); }
-    else {
-        if (g_mode == 2) { StartScreen::motionControlInstructions(lcd); }
-        else { pad.tone(1000.0,0.1); }
+    if(back_pressed) {
+        main_menu(lcd, pad);
+    } else {
+        if (g_mode == 2) {
+            StartScreen::motionControlInstructions(lcd);
+        } else {
+            pad.tone(1000.0,0.1);
+        }
         lcd.clear();
         game_speed(lcd, pad);
     }
@@ -293,37 +301,37 @@
 {
     lcd.clear();
     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();
-        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.
-        break;
+        case 1:
+            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.
+            break;
     }
-        
+
     lcd.refresh();
 }
 
 void StartScreen::SelectMode(Gamepad &pad)
-{   
+{
     Direction d = pad.get_direction();
     wait(1/cs);
     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))  {
         g_mode=1;
     }
 }
 
 void StartScreen::motionControlInstructions(N5110 &lcd)
-{   
+{
     lcd.clear();
     lcd.printString("While playing the",5,0); //Function used to promt the user to start.
     lcd.printString("game, press",9,1);
@@ -346,11 +354,13 @@
             pad.tone(1000.0,0.1);
             back_pressed = 1;
             break;
+        } else {
+            back_pressed = 0;
         }
-        else {back_pressed = 0;}
     }
-    if(back_pressed) { game_mode(lcd, pad); }
-    else {
+    if(back_pressed) {
+        game_mode(lcd, pad);
+    } else {
         pad.tone(1000.0,0.1);
         lcd.clear();
         wait(1/cs);
@@ -358,46 +368,43 @@
 }
 
 void StartScreen::selectSpeedPage(Gamepad &pad)
-{   
+{
     Direction d = pad.get_direction();
     wait(1/cs);
     if(((d==E)||(pad.check_event(Gamepad::R_PRESSED) == true))&&(gs==4))  {
         gs=1;
-    }
-    else if(((d==W)||(pad.check_event(Gamepad::L_PRESSED) == true))&&(gs==1))  {
+    } else if(((d==W)||(pad.check_event(Gamepad::L_PRESSED) == true))&&(gs==1))  {
         gs=4;
-    }
-    else if(((d==E)||(pad.check_event(Gamepad::R_PRESSED) == true))&&(1<=gs<=3))  {
+    } else if(((d==E)||(pad.check_event(Gamepad::R_PRESSED) == true))&&(1<=gs<=3))  {
         gs=gs+1;
-    }
-    else if(((d==W)||(pad.check_event(Gamepad::L_PRESSED) == true))&&(2<=gs<=4))  {
+    } else if(((d==W)||(pad.check_event(Gamepad::L_PRESSED) == true))&&(2<=gs<=4))  {
         gs=gs-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 (gs) {
-    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;
+        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