Snake vs Block Game to be run upon K64F.

Dependencies:   mbed

Revision:
63:205f0ca48473
Parent:
62:ebf6ecf8a6d5
Child:
64:540aa1602372
--- a/GameEngine/SnakevsBlock/SnakevsBlock.cpp	Tue Apr 30 14:53:38 2019 +0000
+++ b/GameEngine/SnakevsBlock/SnakevsBlock.cpp	Wed May 01 22:35:40 2019 +0000
@@ -56,43 +56,60 @@
 
 void SnakevsBlock::read_input(Gamepad &pad, FXOS8700CQ &device, int g_mode)
 {
-    device.get_values();
-    angle = -device.get_roll_angle();
-    //if button A is pressed then reset that particular position to center
-    if (pad.check_event(Gamepad::A_PRESSED) == true)  {
-        garbage = angle;
-    }
-    device.get_values();
+    SnakevsBlock::calculateTilt(pad, device);
+
     switch (g_mode) {
         case 1:
             _d = pad.get_direction(); //Obtains Direction pushed towards on Joystick.
-            _mag = pad.get_mag(); //Obtains Magnitude of Joystick. REMOVE THIS LATER.
             break;
         case 2:
-            angle = -device.get_roll_angle() - garbage;
-            if (angle >= 6) {
+            if (_tiltAngle >= 6) {
                 _d = E;
-                for(int led = 4; led <= 6; led++) {
-                    pad.led(led,1);
-                }
-            } else if (angle <= -6) {
+            } else if (_tiltAngle <= -6) {
                 _d = W;
-                for(int led = 1; led <= 3; led++) {
-                    pad.led(led,1);
-                }
             } else {
                 _d = CENTRE;
-                for(int led = 1; led <= 6; led++) {
-                    pad.led(led,0);
-                }
             }
             break;
     }
+    SnakevsBlock::lightTheLEDS(pad); //This function ligths the LEDS dependent on the direction of travel.
     //printf("%d",gm);
-    //printf("%f",angle);
+    //printf("%f",_tiltAngle);
+    //printf("%f",device.get_roll_angle());
+}
+
+void SnakevsBlock::calculateTilt(Gamepad &pad, FXOS8700CQ &device) //this just reads the angle of tilt required for motion contol and also processes it if A is pressed.
+{
+    device.get_values();
+    _tiltAngle = -device.get_roll_angle();
+    //if button A is pressed then reset that particular position to center
+    if (pad.check_event(Gamepad::A_PRESSED) == true)  {
+        garbage = _tiltAngle;
+    }
+    _tiltAngle = -device.get_roll_angle() - garbage;
+
+    //device.get_values();
+    //printf("%f",_tiltAngle);
     //printf("%f",device.get_roll_angle());
 }
 
+void SnakevsBlock::lightTheLEDS(Gamepad &pad) //This function ligths the LEDS dependent on the direction of travel.
+{
+    if (_d == E)  {
+        for(int led = 4; led <= 6; led++) {
+            pad.led(led,1);
+        }
+    } else if (_d == W)  {
+        for(int led = 1; led <= 3; led++) {
+            pad.led(led,1);
+        }
+    } else {
+        for(int led = 1; led <= 6; led++) {
+            pad.led(led,0);
+        }
+    }
+}
+
 void SnakevsBlock::draw(N5110 &lcd, Gamepad &pad)
 {
     _length = _l._getLength(); //saves the snake length into a private variable.
@@ -114,23 +131,28 @@
     _l.print_length_on_screen(lcd);
 }
 
-int SnakevsBlock::update(N5110 &lcd, Gamepad &pad, SDFileSystem &sd) //Updates objects on screen.
+void SnakevsBlock::update(N5110 &lcd, Gamepad &pad) //Updates objects on screen.
 {
     send_block_number = 0;  //this is for the game to decide wether to remember the number on the block for the current itteration.
     //we dont need to remember if it has already gone past the screen.
-    CheckSnakeFoodCollision(pad); //Function checks for when the snake collides with it's food.
-    CheckSnakeBlockCollision(pad); //Function checks for when the snake collides with any of the blocks.
-    CheckSnakeBlockSidesCollision(pad); //Function checks for when the snake collides with any of the blocks' sides.
+    SnakevsBlock::CheckSnakeFoodCollision(pad); //Function checks for when the snake collides with it's food.
+    SnakevsBlock::CheckSnakeBlockCollision(pad); //Function checks for when the snake collides with any of the blocks.
+    SnakevsBlock::CheckSnakeBlockSidesCollision(pad); //Function checks for when the snake collides with any of the blocks' sides.
     _s.update(_d, b); //_d is the direction of joystick and b controls the motion of a section of the snake relative to obstruction
     _f.update();
     _ff.update();
     _fff.update();
     _b.update(blocknum, blockgap, srn, send_block_number);
-    //_statset.read(sd); //to read the currently stored value.
+
+}
+
+int SnakevsBlock::CheckGameProgression(N5110 &lcd, Gamepad &pad, SDFileSystem &sd)
+{
+    //Function handles level progression and level failure operations by using the class WinLoose.
     if(_length == 0)  {
         _wl.GameOver(lcd,pad);
     }
-    if((pad.check_event(Gamepad::BACK_PRESSED))||(_length == 0)) { //Waits for Back button to be pressed.
+    if((pad.check_event(Gamepad::BACK_PRESSED))||(_length == 0)) { //Waits for Back button to be pressed or the length to reach 0.
         back = 1;
         SnakevsBlock::init();
     } else {
@@ -141,8 +163,10 @@
         _maxLength++;
         level = _wl.LevelComplete(lcd, pad, level);
         _Setstats.write(level, sd);
-        SnakevsBlock::reset();
+        //_statset.read(sd); //to read the currently stored value.
+        SnakevsBlock::reset(); //reset prepares the game for the next level.
     }
+    //_statset.read(sd); //to read the currently stored value.
     return back;
 }
 
@@ -222,18 +246,15 @@
             //the or for the block's y position is due to the fact the exact y co-ordinate might not be collided if the snake's length has increased in the same itteration.
             //printf("snake collision working \n");
             //audio feedback
-            if(blocknum > 0)  {
-                b_pos.y = 0;   //change this to speed y = 0 when length = 10.
-            }
             srn = CheckBlock(block);  //this tells us which of the 5 blocks we are colliding with
-            blocknum = b_number[srn];
+            blocknum = b_number[srn]; //this saves the number inside the colliding block into blocknum.
             if((_length>=10)&&(b_number[srn]>0)) { //this makes the block stop moving down if it's length is more than 10 and still collides.
-                velocity = 0;
-            } else {
+                velocity = 0; //the block and food have to stop as if length of snake is 15 and it reaches 10 one by one, it stays at the same place, as max virtual length
+            } else {          //is 10.
                 velocity = 1;
             }
-            ImplementCollision(pad);
-            SnakevsBlock::_set_velocity();
+            ImplementCollision(pad); //this implements the collision once the conditions are met.
+            SnakevsBlock::_set_velocity(); //sets the block and foods free or frezes them depending on snake level.
         }
     }
 }
@@ -272,7 +293,7 @@
     send_block_number = 1;
     if(blocknum > 0)  {  // to make sure that snake doesn't decrease in _length if number on the block is less than 1;
         _l.MinusLength();
-        pad.tone(1000.0,0.1);
+        pad.tone(432.0,0.1);
         wait(0.04);
     }
     blocknum-=1;
@@ -282,18 +303,26 @@
 {
     //If statements check if the snake sprite has collided with any
     //of the blocks' sides and then stop the snake moving in x axis
+
+    SnakevsBlock::MakeDefaultMotionFree(); //this makes the default motion of the snake freemoving before a collision is checked for, to forget the previous collision.
+
+    for(int i=0; i<=9; i++)  { //i checks for all possible collisions with the snake respective to it's length.
+        SnakevsBlock::check_Collision_Y_axis(i); //checks if the snake and the block are at the same position in y axis.
+    }
+
+}
+
+void SnakevsBlock::MakeDefaultMotionFree()  //this makes the default motion of the snake freemoving before a collision is checked for, to forget the previous collision.
+{
     for(int i=0; i<=9; i++)  {
         b[i] = 1;
     }
-    for(int i=0; i<=9; i++)  { //i checks for all possible collisions with the snake respective to it's length.
-        SnakevsBlock::check_Collision_Y_axis(i); //checks if the snake and the block are at the same position in y axis.
-    }
 }
 
 void SnakevsBlock::check_Collision_Y_axis(int i)  //i is the index of the snake beed and checks for all possible collisions with the snake respective to it's length.
 {
     //This code checks if the snake and the block overlap in the Y axis.
-    for(int b_y_combination=0; b_y_combination<=10; b_y_combination++)  {
+    for(int b_y_combination=0; b_y_combination<=10; b_y_combination++)  { //this carries out the next stage if the Y axis collision criterion is met.
         if (
             (snake_pos[i].y == b_pos.y + b_y_combination) ||
             (snake_pos[i].y + 1 == b_pos.y + b_y_combination) ||
@@ -307,7 +336,7 @@
 
 void SnakevsBlock::check_Collision_X_axis(int i)  //i is the index of the snake beed and checks for all possible collisions with the snake respective to it's length.
 {
-    for(int b_x_combination=2; b_x_combination<=82; b_x_combination+=16)  {
+    for(int b_x_combination=2; b_x_combination<=82; b_x_combination+=16)  { //this carries out the next stage if the X axis collision criterion is met.
 
         SnakevsBlock::checkCollision_EastorWest(b_x_combination, i); //checks if the colliding wall is on east side or west side.
     }