Snake vs Block Game to be run upon K64F.

Dependencies:   mbed

Revision:
56:142e9fdb77a8
Parent:
55:df0825049171
Child:
57:845781aea5ec
--- a/MenuClasses/StartScreen/StartScreen.cpp	Sat Apr 27 18:56:30 2019 +0000
+++ b/MenuClasses/StartScreen/StartScreen.cpp	Sun Apr 28 16:56:08 2019 +0000
@@ -63,21 +63,37 @@
     {0,0,0,0,1,0,0,0,0},
 };
     
-int bullet[13][5] = {
-    {1,0,0,0,1},
-    {1,0,0,0,1},
-    {0,1,0,1,0},
-    {0,0,1,0,0},
-    {0,0,0,0,0},
-    {0,0,1,0,0},
-    {0,0,1,0,0},
-    {0,0,0,0,0},
-    {0,0,0,0,0},
-    {0,0,0,0,0},
-    {0,0,0,0,0},
-    {0,0,1,0,0},
-    {0,0,1,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},
+    {0,0,0,0,0,1,0,0,0,1,1,1,0,0,0,1,0,0,0,0,0},
+    {0,0,0,0,0,1,0,0,0,1,1,1,0,0,0,1,0,0,0,0,0},
+    {0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0},
+    {0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0},
+    {1,1,1,0,1,0,0,1,1,1,1,1,1,1,0,0,1,0,1,1,1},
+    {1,1,0,0,1,0,1,0,0,0,0,0,0,0,1,0,1,0,0,1,1},
+    {1,0,1,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,1,0,1},
+    {0,0,0,1,0,0,0,1,0,0,0,0,0,1,0,0,0,1,0,0,0},
+    {0,0,0,0,1,0,1,0,0,0,0,0,0,0,1,0,1,0,0,0,0},
+    {0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0},
 };
+
+int joystick[13][37] = {
+    {0,0,0,0,0,0,1,1,0,0,0,1,1,1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0,0,0,0,1,1,0},
@@ -85,7 +101,7 @@
     {0,0,0,1,1,0,0,0},
     {0,0,1,1,0,0,0,0},
     {0,1,1,0,0,0,0,0},
-    {1,1,0,0,0,0,0,0},
+    {1,1,1,1,1,1,1,1},
     {0,1,1,0,0,0,0,0},
     {0,0,1,1,0,0,0,0},
     {0,0,0,1,1,0,0,0},
@@ -101,7 +117,7 @@
     {0,0,0,1,1,0,0,0},
     {0,0,0,0,1,1,0,0},
     {0,0,0,0,0,1,1,0},
-    {0,0,0,0,0,0,1,1},
+    {1,1,1,1,1,1,1,1},
     {0,0,0,0,0,1,1,0},
     {0,0,0,0,1,1,0,0},
     {0,0,0,1,1,0,0,0},
@@ -123,7 +139,7 @@
 void StartScreen::init()
 {
     i = 16; //main menu selection arrow position
-    lr = 1; //game mode page selection.
+    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.
@@ -147,6 +163,8 @@
     wait(0.3);
     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.
 }
 
 void StartScreen::instruct(N5110 &lcd, Gamepad &pad)
@@ -192,14 +210,14 @@
     //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::DisplayMMContents(lcd, pad);
+        StartScreen::ImplementMenu(lcd, pad);
     }
     pad.tone(1000.0,0.1);
     wait(1/cs);
-    StartScreen::ImplementMM(lcd, pad);
+    StartScreen::NextMenu(lcd, pad);
 }
 
-void StartScreen::DisplayMMContents(N5110 &lcd, Gamepad &pad)
+void StartScreen::ImplementMenu(N5110 &lcd, Gamepad &pad)
 {   
     lcd.printString("SnakeVSBlock",5,0);
     lcd.printString("Play",0,2);
@@ -219,7 +237,7 @@
     lcd.clear();
 }
 
-void StartScreen::ImplementMM(N5110 &lcd, Gamepad &pad)
+void StartScreen::NextMenu(N5110 &lcd, Gamepad &pad)
 {
     switch (i) {
     case 16:
@@ -228,19 +246,18 @@
     case 24:
         float *cs_sc;
         cs_sc = settings.Implement(lcd, pad, cs);
-        cs = cs_sc[0];
-        sc = cs_sc[1];
-        main_menu(lcd, pad);
+        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);
+        main_menu(lcd, pad); //to return to main_menu if back is pressed
         break;
     default:
-        main_menu(lcd, pad);
-        lr=1;
+        main_menu(lcd, pad); //to return to main_menu if back is pressed
         break;
-  }
+    }
 }
 
 void StartScreen::game_mode(N5110 &lcd, Gamepad &pad)
@@ -249,8 +266,8 @@
     wait(2/cs);
     pad.init();
     while ((pad.check_event(Gamepad::A_PRESSED) == false)&&(pad.check_event(Gamepad::START_PRESSED) == false)) {  //Change this to if button pressed.
-        StartScreen::ImplementGM(pad);
-        StartScreen::DisplayGMContents(lcd);
+        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.
         if (pad.check_event(Gamepad::BACK_PRESSED) == true) {  //This is a very important process, this helps the code to not get stuck in  menu loop.
             pad.tone(1000.0,0.1);
             back_pressed = 1;
@@ -268,41 +285,39 @@
     }
 }
 
-void StartScreen::ImplementGM(Gamepad &pad)
-{   
-    Direction d = pad.get_direction();
-    wait(1/cs);
-    if(((d==E)||(d==W))&&(lr==1))  {
-        lr=2;
-    }
-    
-    else if(((d==E)||(d==W))&&(lr==2))  {
-        lr=1;
-    }
-}
-
-void StartScreen::DisplayGMContents(N5110 &lcd)
+void StartScreen::ModeOptions(N5110 &lcd)
 {
     lcd.clear();
-    lcd.drawRect(27,18,30,20,FILL_TRANSPARENT);
-    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.
-    switch (lr) {
+    switch (g_mode) {
     case 1:
         lcd.printString("Joystick",18,1);
-        g_mode = 1;
+        lcd.drawSprite(22,22,13,37,(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);
-        g_mode = 2;
-        lcd.drawSprite(29,19,13,5,(int *)bullet); //Function used to draw the sprite.
+        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))&&(g_mode==1))  {
+        g_mode=2;
+    }
+    
+    else if(((d==E)||(d==W))&&(g_mode==2))  {
+        g_mode=1;
+    }
+}
+
 void StartScreen::game_speed(N5110 &lcd, Gamepad &pad)
 {
     lcd.clear();
@@ -358,11 +373,11 @@
         fps = 12;
         break;
     case 2:
-        lcd.printString("RABBIT",24,3);
+        lcd.printString("HUMAN",28,3);
         fps = 18;
         break;
     case 3:
-        lcd.printString("HUMAN",28,3);
+        lcd.printString("HORSE",28,3);
         fps = 22;
         break;
     case 4: